Top Banner
Iterations Repetition Statements while and do while loops 1
24

Iterations Repetition Statements while and do while loops 1.

Jan 05, 2016

Download

Documents

Janel Allen
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Iterations Repetition Statements while and do while loops 1.

Iterations

Repetition Statementswhile and do while loops

1

Page 2: Iterations Repetition Statements while and do while loops 1.

Outline

• while statements• Example of while statement• Other repetition statements– do loop– for loop

5-2

Page 3: Iterations Repetition Statements while and do while loops 1.

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

Page 4: Iterations Repetition Statements while and do while loops 1.

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

Page 5: Iterations Repetition Statements while and do while loops 1.

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

Page 6: Iterations Repetition Statements while and do while loops 1.

5-6

Logic of a while Loop

statement

true false

conditionevaluated

Page 7: Iterations Repetition Statements while and do while loops 1.

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

Page 8: Iterations Repetition Statements while and do while loops 1.

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

Page 9: Iterations Repetition Statements while and do while loops 1.

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

Page 10: Iterations Repetition Statements while and do while loops 1.

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

Page 11: Iterations Repetition Statements while and do while loops 1.

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

Page 12: Iterations Repetition Statements while and do while loops 1.

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

Page 13: Iterations Repetition Statements while and do while loops 1.

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

Page 14: Iterations Repetition Statements while and do while loops 1.

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

Page 15: Iterations Repetition Statements while and do while loops 1.

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

Page 16: Iterations Repetition Statements while and do while loops 1.

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

Page 17: Iterations Repetition Statements while and do while loops 1.

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

Page 18: Iterations Repetition Statements while and do while loops 1.

5-18

Nested Loops

• Similar to nested if statements, loops can be nested as well

• The body of a loop can contain another loop.

Page 19: Iterations Repetition Statements while and do while loops 1.

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

Page 20: Iterations Repetition Statements while and do while loops 1.

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

Page 21: Iterations Repetition Statements while and do while loops 1.

5-21

Logic of a do-while Loop

true

statement

false

Page 22: Iterations Repetition Statements while and do while loops 1.

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);

Page 23: Iterations Repetition Statements while and do while loops 1.

5-23

Comparing while and do

statement

true false

conditionevaluated

The while Loop

true

conditionevaluated

statement

false

The do Loop

Page 24: Iterations Repetition Statements while and do while loops 1.

Summary

• Iterations using – while (condition) {statements}– do {statements} while(condition)

24