Presentation is loading. Please wait.

Presentation is loading. Please wait.

Repetition Statements while and do while loops

Similar presentations


Presentation on theme: "Repetition Statements while and do while loops"— Presentation transcript:

1 Repetition Statements while and do while loops
Iterations Repetition Statements while and do while loops

2 Outline while statements Example of while statement
Other repetition statements do loop for loop

3 Online material Chapter one of the book is available online under the following link: Please read pages 46 – 83. The exercises in this book are bit challenging. Do as much as you can.

4 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

5 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

6 Logic of a while Loop condition evaluated false statement true

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 Nested Loops Similar to nested if statements, loops can be nested as well The body of a loop can contain another loop.

19 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++;

20 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

21 Logic of a do-while Loop
statement true condition evaluated false

22 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

23 Comparing while and do statement true false condition evaluated
The while Loop true condition evaluated statement false The do Loop

24 Summary Iterations using while (condition) {statements}
do {statements} while(condition)


Download ppt "Repetition Statements while and do while loops"

Similar presentations


Ads by Google