Repetition Increment Decrement Increment Decrement num Toppings num
















- Slides: 16

Repetition

Increment Decrement ++ --

Increment Decrement num. Toppings = num. Toppings + 1; total. Cost = total. Cost - 1; equivalent to num. Toppings++; total. Cost--; and also to ++num. Toppings; and also to --total. Cost; $1 off Coupon

Repetition

condition evaluated true statements false while ( condition ) { statements; }

while loop count int count = 1; while (count <= 4) { System. out. println("count = " + count); count++; } System. out. println("after: "+ count); 1 2 3 5 4 count = 1 count = 2 count = 3 count = 4 after: 5

while loop count If the condition is false int count = 10; when first checked while (count <= 4) The loop body is skipped { System. out. println("count = " + count); count++; } System. out. println("after: "+ count); 10 after: 10

while loop INFINITE LOOP!!! Condition will int count = 1; while (count <= 4) never be true! { System. out. println("count = " + count); count--; } System. out. println("after: "+ count); count -3 -1 -2 1 0 count. . . = = 1 0 -1 -2

Guided Worked Examples

Counter controlled loops Used when we know ahead of time how many times the loop will execute // computes 1 + 2 + 3 + … + n System. out. println("Enter a number: "); int n = keyboard. next. Int(); int count = 1; int sum = 0; while (count <= n) shorthand for { sum = sum + count sum += count; count++; } System. out. println("1 + 2 + … "+ n " = " + sum);

Sentinel controlled loops Terminate when a designated value (the sentinel) is entered. // compute average quiz scores int score, count = 0, sum = 0; double average; System. out. print("Enter a quiz score (-1 to quit): "); score = scan. next. Int(); priming read What happens if we while (score != -1) forget the priming read? { count++; sum += score; System. out. print("Enter a quiz score (-1 to quit): "); update read score = scan. next. Int(); } If we forget the average = sum / count; update read?

Sentinel controlled loops Terminate when a designated value (the sentinel) is entered. // compute average quiz scores int score, count = 0, sum = 0; double average; System. out. print("Enter a quiz score (-1 to quit): "); score = scan. next. Int(); while (score != -1) { count++; sum += score; System. out. print("Enter a quiz score (-1 to quit): "); score = scan. next. Int(); } average = sum / count; How to we assign average a double value?

Sentinel controlled loops Terminate when a designated value (the sentinel) is entered. // compute average quiz scores int score, count = 0, sum = 0; double average; System. out. print("Enter a quiz score (-1 to quit): "); score = scan. next. Int(); while (score != -1) { count++; sum += score; System. out. print("Enter a quiz score (-1 to quit): "); score = scan. next. Int(); } average = (double) sum / count; Use a cast

Sentinel controlled loops Terminate when a designated value (the sentinel) is entered. // compute average quiz scores int score, count = 0, sum = 0; double average; System. out. print("Enter a quiz score (-1 to quit): "); score = scan. next. Int(); What happens if user enters -1 while (score != -1) the first time? { count++; sum += score; System. out. print("Enter a quiz score (-1 to quit): "); score = scan. next. Int(); } average = (double) sum / count; Divide by zero! How can we prevent this?

Sentinel controlled loops Terminate when a designated value (the sentinel) is entered. // compute average quiz scores int score, count = 0, sum = 0; double average; System. out. print("Enter a quiz score (-1 to quit): "); score = scan. next. Int(); while (score != -1) { count++; sum += score; System. out. print("Enter a quiz score (-1 to quit): "); score = scan. next. Int(); } if (count > 0) average = (double) sum / count; With an if-else average = 0. 0;

Sentinel Controlled Loop Example (#6)