Flow ControlDhrubojyotiKayal
What are flow control statements?if-elsewhiledo-whileforfor eachreturnbreakcontinueswitchAgenda
“Break up the flow of execution by employing decision making, looping, and branching, enabling a program to conditionally execute particular blocks of code” – Java TrailsAll conditional statements use the truth or falsehood of a conditional expression to determine the execution path. conditional expression : a == b evaluates to true or falseAny of the relational operators can be used to produce conditional statementJava doesn’t allow you to use a number as a booleanWhat are control statements?
if(Boolean-expression) 		statement if(Boolean-expression) 		statement 	else 		statementelse 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
int a = 5;int b = 10;if(a > b)System.out.println(“a is bigger”);elseSystem.out.println(“b is bigger”);If-else in Action
Exercise
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.
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
while(Boolean-expression) StatementThe Boolean-expression is evaluated once at the beginning of the loop and again before each further iteration of the statementint x = 1while(x <= 100) {System.out.println(x);   x++;}while
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
Exercise
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.
for(initialization; Boolean-expression; step) 		statement Performs initialization before the first iteration.Then it performs conditional testingAt 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
for(inti = 1 ; i <10 ; i++) {System.out.println(i);}inti = 1;for(; i <10 ;) {System.out.println(i);i++;}for loop in Action
Exercise
Rewrite the exercise with while loop, replacing it with a for loop.
New in Java SE 5Useful for iterating over arrays and collectionsint a [] = new int[100];for(inti = 0 ; i < a.length ; i++){	a[i] = i;}for ( int x : a) {System.out.println(x);}for each
Exercise
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.
branch or change control without any testreturn, break, continue goto - skipConditional Branching
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
public intgetLarger(int a, int b) {	if(a > b)		return a;	else		return b;}return in action
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
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
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 valueThe 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
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
Write a Java program to declare a character arrayLoop through that array and use a switch selector to check for vowelsVowels = {‘a’ , ‘e’,’i’,’o’,’u’}Home work
Q&A

07 flow control

  • 1.
  • 2.
    What are flowcontrol statements?if-elsewhiledo-whileforfor eachreturnbreakcontinueswitchAgenda
  • 3.
    “Break up theflow of execution by employing decision making, looping, and branching, enabling a program to conditionally execute particular blocks of code” – Java TrailsAll conditional statements use the truth or falsehood of a conditional expression to determine the execution path. conditional expression : a == b evaluates to true or falseAny of the relational operators can be used to produce conditional statementJava doesn’t allow you to use a number as a booleanWhat are control statements?
  • 4.
    if(Boolean-expression) statement if(Boolean-expression) statement else statementelse 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”);elseSystem.out.println(“b is bigger”);If-else in Action
  • 6.
  • 7.
    Write a Javaprogram 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 controlledby 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) StatementThe Boolean-expressionis evaluated once at the beginning of the loop and again before each further iteration of the statementint x = 1while(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
  • 11.
  • 12.
    Write a Javaprogram 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 testingAt 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
  • 15.
  • 16.
    Rewrite the exercisewith while loop, replacing it with a for loop.
  • 17.
    New in JavaSE 5Useful for iterating over arrays and collectionsint a [] = new int[100];for(inti = 0 ; i < a.length ; i++){ a[i] = i;}for ( int x : a) {System.out.println(x);}for each
  • 18.
  • 19.
    Write a Javaprogram 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 changecontrol without any testreturn, break, continue goto - skipConditional Branching
  • 21.
    It specifies whatvalue a method will return (if it doesn’t have a void return value) It causes the current method to exit, returning that value. return
  • 22.
    public intgetLarger(int a,int b) { if(a > b) return a; else return b;}return in action
  • 23.
    break quits theloop 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 theexecution 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 statementselects 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 valueThe 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 Javaprogram to declare a character arrayLoop through that array and use a switch selector to check for vowelsVowels = {‘a’ , ‘e’,’i’,’o’,’u’}Home work
  • 28.