COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State...

12
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University [email protected]

Transcript of COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State...

COM S 207While-Loop Statement

Instructor: Ying Cai

Department of Computer ScienceIowa State [email protected]

Used to implement looping

Two key elements test condition body of loop

Test Condition

Execute Statements(body of loop)

true

false

while (condition){ Statement1; Statement2; :::} // end while

While loop

body of loop

As long as the condition is true, you execute all statements in the body of the loop. When condition is false, you exit the loop.

Key Point: Some statement(s) in the body of the loop must eventually make the condition false.

Example

int K = 0;while (K < 4){ System.out.print(K + “ “); K++;}

output: 0 1 2 3

More Examples

int i = 0;int sum = 0;while (i < 4){ sum = sum + i; i++;}

System.out.println(sum);

More Examples

int i = 2;int p = 1;int total = 1;while (p < 4){ total = i * total; p++;}

System.out.println(total);

More Examples

i = 0; sum = 0;while (sum < 10){ i++; sum = sum + i; Print i and sum;}

i = 0; sum = 0;while (sum < 10){ i++; sum = sum - i; Print i and sum;}

i = 0; sum = 0;while (sum < 0){ i++; sum = sum + i; Print i and sum;}

i = 0; sum = 0;while (sum >= 10){ i++; sum = sum + i; Print i and sum;}

Hand-Tracing Loops

int n = 1729;int sum = 0;while (n > 0){ int digit = n % 10; sum = sum + digit; n = n / 10;}

N Sum digit

1729 0

Common Error 1: Infinite Loop

int year = 1;while (year <= 20){ double interest = balance * rate /100.0; balance = balance + interest; } // end while

Forgetting to update the variable that eventually makes the loop condition false

Common Error 1: Infinite Loop

Forgetting to update the variable that eventually makes the loop condition false

int year = 1;while (year > 0){ double interest = balance * rate /100.0; balance = balance + interest; year++;} // end while

Common Error 2: Off-by-One Error or Boundary Condition Error

Questions: 1)Should year start at 0 or 1? 2)Should you test for (balance <TARGET ) or (balance <= TARGET)

int year = 0;while (balance < TARGET){ year++; double interest = balance * rate /100.0;} // end while

System.out.println(“the interest doubled after ” + year + “years.”);

Exercise

Write a Java program that prompts the user for a positive integers value and outputs the numbers (1 2 3 4 …) up to the number that is inputted.

Scanner in = new Scanner(System.in);System.out.println(“Enter in a positive number: “);

int lastNumber = in.nextInt();int cntr = 1;System.out.println(“ “);

while (cntr <= lastNumber){ System.out.print(cntr + “ “); cntr++;}

Write Loops that computes

1) the sum of all integers between 2 and 100 (inclusive)

2) the sum of all EVEN integers between 2 and 100 (exclusive)

3) the sum of all SQUARES of integers between -300 and +300 (inclusive)

4) the average of all ODD integers between 1 and 100 (inclusive)