Presentation is loading. Please wait.

Presentation is loading. Please wait.

PROGRAM FLOWCHART Iteration Statements.

Similar presentations


Presentation on theme: "PROGRAM FLOWCHART Iteration Statements."— Presentation transcript:

1 PROGRAM FLOWCHART Iteration Statements

2 Iteration Statements Loop structures allows a block of statements to be executed repeatedly. A boolean expression will be evaluated within the looping structure. If the structure is true, the block of statements called the loop body will be executed and then the boolean expression is evaluated again. The loop body will continue to execute as long as the expression is true. It will end when the boolean expression is false. 3 kinds of iteration statements: while do..while for loop

3 The while loop while loop is a statement or block of statements that is repeated as long as some condition is satisfied. syntax: while( condition ){ statement1; statement2; . . . } The statements inside the while loop are executed as long as the condition evaluates to true.

4 Example: int x = 0; while (x<10) { System.out.println(x); x++; }
//infinite loop while(true) System.out.println(“hello”); //no loops // statement is not even executed while (false) System.out.println(“hello”);

5 The do…while loop do-while loop is similar to the while-loop
statements inside a do-while loop are executed several times as long as the condition is satisfied The main difference between a while and do-while loop: The statements inside a do-while loop are executed at least once. syntax: do{ statement1; statement2; . . . }while(condition );

6 Example: int x = 0; do { System.out.println(x); x++; }while (x<10);
//infinite loop do{ System.out.println(“hello”); } while (true); //one loop // statement is executed once do{ System.out.println(“hello”); }while (false);

7 The for loop for loop allows execution of the same code a number of times. syntax: for(initialization;condition;counter progression) { statement1; statement2; . . . } Where: initialization -initializes the loop variable. condition - compares the loop variable to some limit value. counter progression – an expression that increment or decrement the loop control variable.

8 Example: int i; for( i = 0; i < 10; i++ ){ System.out.println(i); }
The code shown above is equivalent to the following while loop. int i = 0; while( i < 10 ){ System.out.print(i); i++;


Download ppt "PROGRAM FLOWCHART Iteration Statements."

Similar presentations


Ads by Google