COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1
Announcements Facebook Tech Talk tonight, 6pm, SN011 Google info session, 6pm, SN014, tomorrow night 2
Questions? 3
Today in COMP 110 do-while loops for loops
Review: loops Often you need to repeat an action in a program Start Enough sandwiches? Distribute sandwiches No Yes Make sandwich
Review: loops Loop: part of a program that repeats Body: statements being repeated Iteration: each repetition of body Stopping condition Start Enough sandwiches? Distribute sandwiches No Yes Make sandwich
Review: Types of Loops while ◦ Safest choice ◦ Not always most elegant do-while ◦ Loop iterates AT LEAST once for ◦ Similar to while, but often more convenient syntax 7
Review: while loop start; while (not enough sandwiches) { make a sandwich; } distribute sandwiches; Start Enough sandwiches? Distribute sandwiches No Yes Make sandwich
Review: while loop syntax while (boolean expression) { statements; }
Review: Using a while loop int n = 1; while (n <= 10) { System.out.println(n); n = n + 1; }
while loop Evaluate Boolean expression Execute Body End loop true false
while loop First, evaluate Boolean expression If true, go into the body If false, do not go into the body
Types of Loops while ◦ Safest choice ◦ Not always most elegant do-while ◦ Loop iterates AT LEAST once for ◦ Similar to while, but often more convenient syntax 13
do-while loop Similar to while loop At least one iteration of the loop ◦ First execute the loop body ◦ Then evaluate the Boolean expression ◦ If true, execute the body again ◦ If false, quit the body
do-while loop 15 Evaluate Boolean expression Execute Body End loop true false Execute Body From outside the loop Evaluate Boolean expression End loop false Execute Body From outside the loop true Equivalent
do-while loop syntax do { statements; // loop body } while (boolean expression); If Boolean expression is true, repeat loop body Otherwise, exit loop
Using a do-while loop int n = 1; do { System.out.println(n); n = n + 1; } while (n <= 10); Don’t forget the semicolon!
Using a do-while loop What if n = 20? int n = 20; do { System.out.println(n); n = n + 1; } while (n <= 10);
Infinite loop int n = 1; do { System.out.println(n); // n = n + 1; } while (n <= 10);
Types of Loops while ◦ Safest choice ◦ Not always most elegant do-while ◦ Loop iterates AT LEAST once for ◦ Similar to while, but often more convenient syntax 20
for loop Components of a for loop: ◦ Initializing actions ◦ Boolean expression ◦ Loop body ◦ Update actions
for loop 22 Evaluate Boolean expression Execute Body End loop true false Execute Initializing actions Execute Update actions
for loop syntax for (initializing actions; Boolean expression; update actions) { statements; // loop body }
Using a for loop int n; for (n = 1; n <= 10; n++) { System.out.println(n); }
while loops and for loops int n; for (n = 1; n <= 10; n++) { System.out.println(n); } int n = 1; while (n <= 10) { System.out.println(n); n = n + 1; }
for loop: initializing actions Only done ONCE at the beginning of the loop, before the Boolean expression is tested int n; for (n = 1; n <= 10; n++) { System.out.println(n); }
for loop: update actions Executed at the end of every iteration ◦ After the loop body, but before the next Boolean expression evaluation int n; for (n = 1; n <= 10; n++) { System.out.println(n); }
Tracing a for loop int n; for (n = 1; n <= 3; n++) { System.out.println(n); } n = ? n = 1 n = 2 n = 3 n = 4 Execute initializing actions Evaluate Boolean expression Execute body Execute update actions
Infinite loop int n; for (n = 1; n <= 10; n = 0) { System.out.println(n); }
Types of Loops while ◦ Safest choice ◦ Not always most elegant do-while ◦ Loop iterates AT LEAST once for ◦ Similar to while, but often more convenient syntax 30
Wednesday Spend some more time with loops 31