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

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

description

COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University [email protected]. Used to implement looping. Two key elements test condition body of loop. Test Condition. false. true. While loop. Execute Statements (body of loop). - PowerPoint PPT Presentation

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

Page 1: COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science

COM S 207While-Loop Statement

Instructor: Ying Cai

Department of Computer ScienceIowa State [email protected]

Page 2: COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science

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.

Page 3: COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science

Example

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

output: 0 1 2 3

Page 4: COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science

More Examples

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

System.out.println(sum);

Page 5: COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science

More Examples

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

System.out.println(total);

Page 6: COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science

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

Page 7: COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science

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

Page 8: COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science

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

Page 9: COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science

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

Page 10: COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science

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

Page 11: COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science

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

Page 12: COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science

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)