The document discusses different types of control flow statements in Java including conditional statements (if-else, switch-case), loops (while, do-while, for), and provides examples of each. It explains how if-else statements evaluate conditions and execute code accordingly. Switch-case can be used as an alternative to if-else when multiple decisions depend on a variable's value. While and do-while loops execute code repeatedly as long as a condition remains true, and for loops initialize a counter variable, check its condition, and increment/decrement it with each iteration.
Download as PPTX, PDF, TXT or read online on Scribd
100%(1)100% found this document useful (1 vote)
66 views
Lecture 5
The document discusses different types of control flow statements in Java including conditional statements (if-else, switch-case), loops (while, do-while, for), and provides examples of each. It explains how if-else statements evaluate conditions and execute code accordingly. Switch-case can be used as an alternative to if-else when multiple decisions depend on a variable's value. While and do-while loops execute code repeatedly as long as a condition remains true, and for loops initialize a counter variable, check its condition, and increment/decrement it with each iteration.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21
Gab
All application development environment
provide decision making process called control flow statements that directs the execution of application. It facilitates the programmer to examine the existing condition and decide suitable flow of action. The if-else statement tests the result of a condition, and perform appropriate action based on result. if(condition 1){ action 1; } else if(condition 2){ action 2; }else{ action 3; } class CheckNumber{ public static void main(String args[]){ int num = 10; if (num % 2 == 0){ System.out.println(num + “ is even number ); } else { System.out.println(num + “ is odd number ); } } } switch-case can be used as an alternative for if-else condition. If the programmer has to make number of decisions, and all the decisions depend on the value of variable, then switch-case is used instead of if-else condition. switch(expression){ case 1 : action1 statement; break; case 2: action2 statement; break; default: default statement; } int month = 1; String name; switch (month) { case 1: name = “January”; break; case 2: name=”February”; break; default: name=”Invalid Month”; } The while loop executes till condition is specified. It executes the steps mentioned in the loop only if the condition is true. This is useful where programmer doesn't know in advance that how many times the loop will be executed. while (condition) { action statements: … } int i = 1; while(i<=5){ i++; System.out.println(“value of i is : “+i); } The do-while loops execute certain statements till the specified condition is true. This loop ensures that the loop body is executed atleast once. int j = 8; do{ j = j+2; System.out.println(“value of j is : “ +j); }while(j>=10 && j<=50){ j = j+5; System.out.println(“value of j is : “ +j); } int j = 8; do{ j = j+2; System.out.println(“value of j is : “ +j); }while(j>=10 && j<=50){ j = j+5; System.out.println(“value of j is : “ +j); } All loops have some common feature: a counter variable that is initialized before the loop begins. The for loop provides the feature that, initialized the variable before loop begins, a condition for counter variable and counter upto which loop lasts. Syntax of for loop: for(initialization statements; condition; increment/decrement statements){ action statements; . . } for(int i=1;i<=10;i++){ System.out.println(“Value of i is “ +i); } for(int i=1;i<=10;i++){ System.out.println(“Value of i is “ +i); } 1) Write a program called CheckPassFail which prints "PASS" if the int variable "mark" is more than or equal to 50; or prints "FAIL" otherwise. The program shall always print “DONE” before exiting.
2) 2) Exercise CheckOddEven (if-else): Write a program called
CheckOddEven which prints "Odd Number" if the int variable “number” is odd, or “Even Number” otherwise. The program shall always print “BYE!” before exiting.
3) Exercise PrintDayInWord (nested-if, switch-case): Write a program called
PrintDayInWord which prints “Sunday”, “Monday”, ... “Saturday” if the int variable "day" is 0, 1, ..., 6, respectively. Otherwise, it shall print “Not a valid day”. Use (a) a "nested-if" statement; (b) a "switch-case" statement. http://latest-technology-guide.blogspot.com/2013/03/java-control-structure- looping-structure-with-example.html