iterations repetition statements while and do while loops 1
TRANSCRIPT
Iterations
Repetition Statementswhile and do while loops
1
Outline
• while statements• Example of while statement• Other repetition statements– do loop– for loop
5-2
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.
3
5-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-5
The while Statement• A while statement has the following syntax:
while(condition) { statement; }
• 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
Body
5-6
Logic of a while Loop
statement
true false
conditionevaluated
5-7
The while Statement• An example of a while statement:
int count = 1;while (count <= 5){ System.out.println(count); count= count +1;}
• 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
Body
5-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
Example1
• What is the output of the program?// hello.javapublic class Hello1{ public static void main(String[] arguments) { System.out.println(“hello”); System.out.println(“hello”); System.out.println(“hello”); System.out.println(“hello”); System.out.println(“hello”) ; System.out.println(“hello”); System.out.println(“hello”); System.out.println(“hello”); System.out.println(“hello”); System.out.println(“hello”); }}//end of program
Prints hello 10 times
9
Example2
• What is the output of the program?// hello.javapublic 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
10
Example3
• What is the output of the program?// hello3.javapublic 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
11
Example4
• What is the output of the program?// hello4.javaImport java.util.Randompublic 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
12
Example5
• What is the output of the program?// hello5.javapublic 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
13
Example6
• What is the output of the program?// sum1.javapublic 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
14
Example7
• What is the output of the program?// factorial.javapublic 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
15
5-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
5-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
5-18
Nested Loops
• Similar to nested if statements, loops can be nested as well
• The body of a loop can contain another loop.
5-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++;}
5-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
5-21
Logic of a do-while Loop
true
statement
false
5-22
The do Statement• An example of a do loop:
• The body of a do loop will execute for 5 times
int count = 0;do{ count++; System.out.println (count);} while (count < 5);
5-23
Comparing while and do
statement
true false
conditionevaluated
The while Loop
true
conditionevaluated
statement
false
The do Loop
Summary
• Iterations using – while (condition) {statements}– do {statements} while(condition)
24