2. What are flow control statements? if-else while do-while for for each return break continue switch Agenda
3. “Break up the flow of execution by employing decision making, looping, and branching, enabling a program to conditionally execute particular blocks of code” – Java Trails All conditional statements use the truth or falsehood of a conditional expression to determine the execution path. conditional expression : a == b evaluates to true or false Any of the relational operators can be used to produce conditional statement Java doesn’t allow you to use a number as a boolean What are control statements?
4. if(Boolean-expression) statement if(Boolean-expression) statement else statement else is optional The Boolean-expression must produce a boolean result The statement is either a simple statement terminated by a semicolon, or a compound statement, which is a group of simple statements enclosed in braces. if-else
5. int a = 5; int b = 10; if(a > b) System.out.println(“a is bigger”); else System.out.println(“b is bigger”); If-else in Action
7. Write a Java program which compares two integers and prints which one in larger and the incremented or decremented value. Before printing however you should increment in the if block and decrement in the else block.
8. Looping is controlled by while, do-while and for, which are sometimes classified as iteration statements. Statement(s) repeats until the controlling Boolean-expression evaluates to false Iteration
9. while(Boolean-expression) Statement The Boolean-expression is evaluated once at the beginning of the loop and again before each further iteration of the statement int x = 1 while(x <= 100) { System.out.println(x); x++; } while
10. do statement while(Boolean-expression); The statement of the do-while always executes at least once, even if the expression evaluates to false the first time In a while, if the conditional is false the first time the statement never executes. int x = 1; do { System.out.println(x); x++; } while (x <= 5) do-while
12. Write a Java program to print the numbers from 1…100 using a while loop. In the while loop check and print if the number is odd or even.
13. for(initialization; Boolean-expression; step) statement Performs initialization before the first iteration. Then it performs conditional testing At the end of each iteration, some form of “stepping.” Any of the expressions initialization, Boolean-expression or step can be empty The expression is tested before each iteration, and as soon as it evaluates to false, execution will continue at the line following the for statement. At the end of each loop, the step executes. Useful if you already know how many times the statements need to be repeated. foreach will also work with any object that is Iterable. for
14. for(inti = 1 ; i <10 ; i++) { System.out.println(i); } inti = 1; for(; i <10 ;) { System.out.println(i); i++; } for loop in Action
17. New in Java SE 5 Useful for iterating over arrays and collections int a [] = new int[100]; for(inti = 0 ; i < a.length ; i++){ a[i] = i; } for ( int x : a) { System.out.println(x); } for each
19. Write a Java program to populate an integer array with 100 elements. Extend the program to print the numbers which are odd and even in that array using for each loop.
20. branch or change control without any test return, break, continue goto - skip Conditional Branching
21. It specifies what value a method will return (if it doesn’t have a void return value) It causes the current method to exit, returning that value. return
23. break quits the loop without executing the rest of the statements in the loop. for(inti = 1 ; i <=10 ; i++) { if( i % 5 == 0) break; System.out.println(i); } In-case of nested for or while it will break out of the innermost loop where break was encountered == {Home work} break
24. continue stops the execution of the current iteration and goes back to the beginning of the loop to begin the next iteration for (inti = 1; i <= 10 ;i++) { if ( i % 3 == 0) continue; System.out.println(i); } continue
25. The switch statement selects from among pieces of code based on the value of an integral expression switch(integral-selector) { case integral-value1 : statement; break; case integral-value2 : statement; break; case integral-value3 : statement; break; case integral-value4 : statement; break; case integral-value5 : statement; break; default: statement; } Integral-selector is an expression that produces an integer value The switch compares the result of integral-selector to each integral-value. If it finds a match, the corresponding statement (a single statement or multiple statements; braces are not required) executes. If no match occurs, the default statement executes. Each case ends with a break, which causes execution to jump to the end of the switch body. break is optional, if it is missing, the code for the following case statements executes until a break is encountered. switch
26. inti = 3; switch(i) { case 1 : System.out.println(“One”); break; case 2 : System.out.println(“Two”); break; case 3 : System.out.println(“Three”); break; case 4 : System.out.println(“Four”); break; default: System.out.println(“Out of range”); } switch in action
27. Write a Java program to declare a character array Loop through that array and use a switch selector to check for vowels Vowels = {‘a’ , ‘e’,’i’,’o’,’u’} Home work