25
while and do-while Statements March 23, 2008 ICS102: while & do-while 1

while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

while and do-while Statements

March 23, 2008 ICS102: while & do-while 1

Page 2: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

Outline

Introduction

while Loop

do-while Loop

March 23, 2008 ICS102: while & do-while 2

Page 3: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

- Introduction

Loops in Java are similar to those in other high-level languages

Java has three types of loop statements: The whileThe do-whileThe for

The code that is repeated in a loop is called the body of the loop

Each repetition of the loop body is called an iteration of the loop

March 23, 2008 ICS102: while & do-while 3

Page 4: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

- while loop

A while statement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression

The Boolean expression is checked before the loop body is executed

When false, the loop body is not executed at all

Before the execution of each following iteration of the loop body, the Boolean expression is checked again

If true, the loop body is executed again

If false, the loop statement ends

The loop body can consist of a single statement, or multiple statements enclosed in a pair of braces ({ })

March 23, 2008 ICS102: while & do-while 4

Page 5: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

-- while Loop Syntax

while ( <boolean expression> )

<statement> //only one statement

ORwhile ( <boolean expression> ) {

<statement> //many

}

while ( number <= 100 ) {

sum = sum + number;

number = number + 1;

}

Boolean ExpressionBoolean Expression

Statement(loop body)Statement

(loop body)

These statements are executed as long as number is less than or equal to 100.

These statements are executed as long as number is less than or equal to 100.

March 23, 2008 ICS102: while & do-while 5

Page 6: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

-- while Loop Control flow

int sum = 0, number = 1int sum = 0, number = 1

number <= 100 ?number <= 100 ?

falsesum = sum + number;

number = number + 1;

sum = sum + number;

number = number + 1;

true

March 23, 2008 ICS102: while & do-while 6

Page 7: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

-do-while Loop

A do-while statement is used to execute a portion of code (i.e., the loop body), and then repeat it based on the evaluation of a Boolean expression

The loop body is executed at least once

The Boolean expression is checked after the loop body is executed

The Boolean expression is checked after each iteration of the loop body

If true, the loop body is executed againIf false, the loop statement endsDon't forget to put a semicolon after the Boolean expression

Like the while statement, the loop body can consist of a single statement, or multiple statements enclosed in a pair of braces ({ })

March 23, 2008 ICS102: while & do-while 7

Page 8: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

-- do-while Loop Syntax

do {

<statement>

} while (<boolean expression>);

do {

sum += number;

number++;

} while (sum <= 1000000) ;

Boolean ExpressionBoolean Expression

Statement(loop body)Statement

(loop body)

These statements are executed as long as sum is less than or equal to 1,000,000.

These statements are executed as long as sum is less than or equal to 1,000,000.

March 23, 2008 ICS102: while & do-while 8

Page 9: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

-- do-while Loop Control Flow

int sum = 0, number = 1int sum = 0, number = 1

sum += number;

number++;

sum += number;

number++;

sum <= 1000000 ?sum <= 1000000 ? true

falseMarch 23, 2008 ICS102: while & do-while 9

Page 10: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

THE END

March 23, 2008 ICS102: while & do-while 10

Page 11: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

Examples

March 23, 2008 ICS102: while & do-while 11

Page 12: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

Questions

1. Write a Java program which computes the sum of all the odd numbers between 0 and 100.

2. Write a Java program which reads 20 numbers using a scanner and computes their average.

3. Write a Java program which reads unknown number of integers using a scanner and counts the number of odd numbers and the number of even numbers. Assume the input integers are all positive. Use a negative number as a sentinel.

March 23, 2008 ICS102: while & do-while 12

Page 13: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

Solution using while loop

March 23, 2008 ICS102: while & do-while 13

Page 14: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

Q1 Solution

Write a Java program which computes the sum of all the odd numbers between 0 and 100.

int n =1;int sum = 0;while (n < 100) {sum += n;n = n + 2;

}System.out.println(“The sum is “ + sum);

March 23, 2008 ICS102: while & do-while 14

Page 15: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

Q2 Solution

Write a Java program which reads 20 numbers using a scanner and computes their average.

Scanner kb = new Scanner(System.in);int cnt = 0;double x;double sum = 0;While (cnt < 20) {x = kb.nextDouble();sum += x;cnt++;

}System.out.println(“The Average is “ + sum/cnt);

March 23, 2008 ICS102: while & do-while 15

Page 16: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

Q3 Solution

Write a Java program which reads unknown number of integers using a scanner and counts the number of odd numbers and the count of even numbers. Assume the input integers are all positive. Use any negative number as a sentinel.

Scanner kb = new Scanner(System.in);int even_cnt = 0;int odd_cnt = 0;double x = kb.nextInt();while (x > 0) {

if ( mod(x,2) == 0)even_cnt++;

elseodd_cnt++;

x = kb.nextInt();}System.out.println(“Even numbers are = “ + even_count);System.out.println(“Odd numbers are = “ + odd_count);

March 23, 2008 ICS102: while & do-while 16

Page 17: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

Solution using do-while loop

March 23, 2008 ICS102: while & do-while 17

Page 18: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

Q1 Solution

Write a Java program which computes the sum of all the odd numbers between 0 and 100.

int n = 1;int sum = 0;do {

sum += n;n = n + 2;} While ( n < 100)

System.out.println(“The sum is “ + sum);

March 23, 2008 ICS102: while & do-while 18

Page 19: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

Q2 Solution

Write a Java program which reads 20 numbers using a scanner and computes their average.

Scanner kb = new Scanner(System.in);int cnt = 0;double x;double sum = 0;do {System.out.println(“Enter a number”);x = kb.nextDouble();sum += x;cnt++;

} while (cnt < 20);System.out.println(“The Average is “ + sum/cnt);

March 23, 2008 ICS102: while & do-while 19

Page 20: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

Q3 Solution

Write a Java program which reads unknown number of integers using a scanner and counts the number of odd numbers and the count of even numbers. Assume the input integers are all positive. Use any negative number as a sentinel.

Scanner kb = new Scanner(System.in);int even_cnt = 0;int odd_cnt = 0;double x = kb.nextInt();if (x > 0) {

do {if ( mod(x,2) == 0)

even_cnt++;else

odd_cnt++; x = kb.nextInt();

} while ( x > 0)}System.out.println(“Even numbers are = “ + even_count);System.out.println(“Odd numbers are = “ + odd_count);

March 23, 2008 ICS102: while & do-while 20

Page 21: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

Additional Slides

March 23, 2008 ICS102: while & do-while 21

Page 22: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

while Loop Pitfall - 1

int product = 0;

while ( product < 500000 ) {

product = product * 5;

}

11

Infinite LoopsBoth loops will not terminate because the boolean expressions will never become false.

Infinite LoopsBoth loops will not terminate because the boolean expressions will never become false.int count = 1;

while ( count != 10 ) {

count = count + 2;

}

22

March 23, 2008 ICS102: while & do-while Chapter 7 - 22

Page 23: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

while Loop Pitfall - 2

double count = 0.0;

while ( count != 1.0 ) {

count = count + 1.0/3.0;

}

11

Using Real NumbersLoop 2 terminates, but Loop 1 does not because only an approximation of a real number can be stored in a computer memory.

Using Real NumbersLoop 2 terminates, but Loop 1 does not because only an approximation of a real number can be stored in a computer memory.double count = 0.0;

while ( count <= 1.0 ) {

count = count + 1.0/3.0;

}

22

March 23, 2008 ICS102: while & do-while Chapter 7 - 23

Page 24: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

while Loop Pitfall - 3

Goal: Execute the loop body 10 times.count = 1;

while (count < 10) {

. . .

count++;

}

11 count = 1;

while (count <= 10) {

. . .

count++;

}

22

count = 0;

while (count <= 10) {

. . .

count++;

}

33 count = 0;

while (count < 10) {

. . .

count++;

}

44

11 33and exhibit off-by-one error.March 23, 2008 ICS102: while & do-while Chapter 7 - 24

Page 25: while and do-while Statements · - while loop A whilestatement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean

Checklist for Repetition Control

1. Watch out for the off-by-one error (OBOE).2. Make sure the loop body contains a statement that

will eventually cause the loop to terminate.3. Make sure the loop repeats exactly the correct

number of times.

March 23, 2008 ICS102: while & do-while Chapter 7 - 25