Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Repetition structures Overview while statement for statement do while statement.

Similar presentations


Presentation on theme: "1 Repetition structures Overview while statement for statement do while statement."— Presentation transcript:

1 1 Repetition structures Overview while statement for statement do while statement

2 2 Repetition structures while statement »Syntax »Interpretation »Flow diagram for while statement »Example for statement »Syntax »Interpretation »Flow diagram for for statement »Example do while statement »Syntax »Interpretation »Flow diagram for do while statement »Example

3 3 while statement l Syntax while (condition) statement; l Interpretation »If the condition is true, the statement is executed; then the condition is evaluated again »The statement is executed over and over until the condition becomes false »If the condition of a while statement is false initially, the statement is not executed even once »Therefore, we say that a while statement executes its body zero or more times

4 4 while statement flow diagram Flow diagram for while statement statement condition false true

5 5 while statement Example class WhileExample { public static void main(String[] args) { int i; i = 0; while (i < 10) { i++; System.out.println(i + "\t" + Math.pow(i, 2)); }

6 6 Three important points with regard to while statement 1. The condition of the while statement should have atleast one variable which is loop control variable. 2. The loop control variable should have a value ( initialized ) before entering the loop. 3. The loop control variable should be updated inside the body of the loop such that the condition will eventually evaluates to false, otherwise loop will be an infinite loop. public class Forever { public static void main(String[] args) { int i = 0; while (i < 10) System.out.println(”Never stop"); }

7 7 Nested Loops l One of the statements inside the a while could be another while loop – this are then called Nested Loops. public class NestedWhileLoop { public static void main(String[] args) { int i=1,j; while(i<=3) { j=1; while(j<=3) { System.out.print(i+","+j+"\t"); j++; } System.out.println(); i++; }

8 8 For statement l Syntax for (initialization; condition; updation) statements l Interpretation »First initialization statement is executed »Then the condition is evaluated »Repeating the following are done »If the condition is true, –The statements inside the loop are executed –Then the updation statement is executed –Then the condition is checked »If the condition is false, –The control will proceed to the next statement(s) after the for loop

9 9 For statement flow diagram l Flow diagram for ( ) {true } false Initialization Statement Updation Statement Statements inside the for loop condition

10 10 For statement Example class ForExample { public static void main(String[] args) { for (int i = 1; i <= 10; i++) System.out.println(i + "\t" + Math.pow(i,2)); } for statements can also be nested. public class NestedForLoop { public static void main(String[] args) { int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) System.out.print(i+","+j+"\t"); System.out.println(); }

11 11 do while statement syntax l Syntax do { statements } while ( condition ) ; l Interpretation »First the statements inside the do loop are executed regardless of the condition »The statements inside the loop should have an Updation statement of loop control variable »Then the condition is evaluated »If it is true –The statements inside the do loop are executed again –And the condition is checked »If condition is false –The statement that follows the do while statement is executed

12 12 do while statement flow diagram l Flow diagram true false Condition Statements inside the do loop

13 13 do while statement Example import TextIO; class DoExample { static TextIO stdin = new TextIO(System.in); public static void main(String[] args) throws java.io.IOException { int number; do { System.out.println("Enter a Number or 0 to stop execusion"); number = stdin.readInt(); System.out.println("Square of the number is: "+(number*number)); } while (number != 0); }


Download ppt "1 Repetition structures Overview while statement for statement do while statement."

Similar presentations


Ads by Google