Repetition Statements while and do while loops Iterations Repetition Statements while and do while loops
Outline while statements Example of while statement Other repetition statements do loop for loop
Online material Chapter one of the book is available online under the following link: http://introcs.cs.princeton.edu/java/home/chapter1.pdf Please read pages 46 – 83. The exercises in this book are bit challenging. Do as much as you can.
Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements, they are controlled by boolean expressions Java has three kinds of repetition statements: the while loop the do loop the for loop
The while Statement A while statement has the following syntax: while(condition) { statement; } Body If the condition is true, the statement is executed Then the condition is evaluated again, and if it is still true, the statement is executed again The statement is executed repeatedly until the condition becomes false
Logic of a while Loop condition evaluated false statement true
The while Statement An example of a while statement: int count = 1; while (count <= 5) { System.out.println(count); count= count +1; } Body If the condition of a while loop is false initially, the statement is never executed Therefore, the body of a while loop will execute zero or more times
The while Statement Let's look at some examples of loop processing A loop can be used to maintain a running sum A sentinel value is a special input value that represents the end of input A loop can also be used for input validation, making a program more robust
Example1 What is the output of the program? Prints hello 10 times // hello.java public class Hello1 { public static void main(String[] arguments) System.out.println(“hello”); System.out.println(“hello”) ; } //end of program Prints hello 10 times
Example2 What is the output of the program? Prints hello 10 times // hello.java public class Hello2 { public static void main(String[] arguments) int i =1; while(i<=10) System.out.println(“hello”); } //end of program Prints hello 10 times
Example3 What is the output of the program? // hello3.java public class Hello2 { public static void main(String[] arguments) int x = Integer.parseInt(args[0]); int i =1; while(i<=x) System.out.println(“hello”); } //end of program
Example4 What is the output of the program? // hello4.java Import java.util.Random public class Hello3 { public static void main(String[] arguments) Random generator = new Random(); while(i<=10) { int x = generator.nextInt(100); System.out.println(x); } //end of program
Example5 What is the output of the program? // hello5.java public class Hello2 { public static void main(String[] arguments) int x = Integer.parseInt(args[0]); int i =1; while(i<=x) System.out.println(“hello”); } //end of program
Example6 What is the output of the program? // sum1.java public class sum1 { public static void main(String[] arguments) int i =1; int sum =0; while(i<=100) sum = sum+i ; } System.out.println(sum); //end of program
Example7 What is the output of the program? // factorial.java public class sum1 { public static void main(String[] arguments) int i =1; int factorial =1; while(i<=100) fact= fact*i ; } System.out.println(factorial); //end of program
Infinite Loops To exit the loop The body of a while loop has to make the control condition false Otherwise, the loop will on go on for ever. This is called an infinite loop. Press Control-C to stop the execution of an infinite loop. This is a common logical error
Infinite Loops An example of an infinite loop: int i = 0; while (i<=0) { System.out.println (i); i = i - 1; } This loop will continue executing for ever. Control-C to stop the execution
Nested Loops Similar to nested if statements, loops can be nested as well The body of a loop can contain another loop.
Nested Loops What is the output of the following program? Int count1 = 1; Int count2; while (count1 <= 10) { System.out.println(“hello"); count2 = 1; while (count2 <= 20) System.out.println(“lahcen"); count2++; } count1++;
The do-while Statement A do-while statement (also called a do loop) has the following syntax: do{ statement; }while (condition ) The statement is executed once initially, and then the condition is evaluated The statement is executed repeatedly until the condition becomes false
Logic of a do-while Loop statement true condition evaluated false
The do Statement An example of a do loop: int count = 0; do{ count++; System.out.println (count); } while (count < 5); The body of a do loop will execute for 5 times
Comparing while and do statement true false condition evaluated The while Loop true condition evaluated statement false The do Loop
Summary Iterations using while (condition) {statements} do {statements} while(condition)