110 likes | 177 Views
Iteration. Overview while statement for statement do while statement. while statement. It is very common in programming to have a statement that needs to be executed more than once.
E N D
Iteration Overview • while statement • forstatement • dowhile statement
while statement • It is very common in programming to have a statement that needs to be executed more than once. • 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: • The Syntax of the while statement is as follows: while (condition) statement; • 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 awhilestatement executes its body zero or more times
whilestatement flow diagram false condition true statement • Flow diagram for while statement
whilestatement 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)); } } }
Important points about whilestatement • The condition of the while statement usually involves at least one variable called loop control variable. • The loop control variable should have a value ( initialized ) before entering the loop. • 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"); } }
for statement • 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 • 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
For statement flow diagram • Flow diagram for ( ) { true } false Initialization Statement Updation Statement condition Statements inside the for loop
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)); } }
do while statement syntax • Some times it is more appropriate to perform the test of a loop at the end. For this Java provides the do-while statement. • The Syntax is: do { statements } while ( condition ) ; • 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
do while statement flow diagram • Flow diagram true false Statements inside the do loop Condition
do while statement Example import java.io.*; class DoExample { public static void main(String[] args) throws 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); } }