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 while statement l It is very common in programming to have a statement that needs to be executed more than once. l To avoid repeating the same statement in a program, java provide three repetition (loop) statements that may be used. These are while, for and do-while. while Statement: l The Syntax of the while statement is as follows: while (condition) statement; l Interpretation »First evaluate the condition. If it 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 is false initially, the statement is not executed even once »Therefore, we say that a while statement executes its body zero or more times

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

4 4 while statement Example The following example prints a table of squares for integers from 1 to 10. 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)); }

5 5 Important points about while statement 1. The condition of the while statement usually involves at least one variable called 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"); }

6 6 for statement l Because most loops involves three steps, namely, initialization, checking the condition and updating the control variable, Java provides the for statement which summarizes them on one line. The Syntax is: for (initialization; condition; updating) statements l Interpretation »First initialization statement is executed »Then the condition is evaluated »If the condition is true, –The statements inside the loop are executed –Then the updating statement is executed –Then the condition is checked again, etc. »If the condition is false, –The control will proceed to the next statement(s) after the for loop

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

8 8 for statement Example The following example prints a table of squares for integers from 1 to 10 using for loop. class ForExample { public static void main(String[] args) { for (int i = 1; i <= 10; i++) System.out.println(i + "\t" + Math.pow(i,2)); }

9 9 do while statement syntax l Some times it is more appropriate to perform the test of a loop at the end. For this Java provides the do-while statement. l The Syntax is: do { statements } while ( condition ) ; l Interpretation »First the statements inside the do loop are executed regardless of the condition »Then the condition is evaluated »If it is true –The statements inside the do loop are executed again –And the condition is checked again, etc. »If condition is false –The statement that follows the do while statement is executed

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

11 11 do while statement Example import java.io.*; class DoExample { public static void main(String[] args) throws java.io.IOException { BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) ); int num; do { System.out.println("Enter a Number or 0 to stop execusion"); String input= stdin.readLine(); num = Integer.parseInt(input); System.out.println("Squared number is:"+(num*num)); } while (num != 0); }


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

Similar presentations


Ads by Google