Download presentation
Presentation is loading. Please wait.
Published byDayna Rice Modified over 9 years ago
1
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1
2
Announcements Facebook Tech Talk tonight, 6pm, SN011 Google info session, 6pm, SN014, tomorrow night 2
3
Questions? 3
4
Today in COMP 110 do-while loops for loops
5
Review: loops Often you need to repeat an action in a program Start Enough sandwiches? Distribute sandwiches No Yes Make sandwich
6
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
7
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
8
Review: while loop start; while (not enough sandwiches) { make a sandwich; } distribute sandwiches; Start Enough sandwiches? Distribute sandwiches No Yes Make sandwich
9
Review: while loop syntax while (boolean expression) { statements; }
10
Review: Using a while loop int n = 1; while (n <= 10) { System.out.println(n); n = n + 1; }
11
while loop Evaluate Boolean expression Execute Body End loop true false
12
while loop First, evaluate Boolean expression If true, go into the body If false, do not go into the body
13
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
14
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
15
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
16
do-while loop syntax do { statements; // loop body } while (boolean expression); If Boolean expression is true, repeat loop body Otherwise, exit loop
17
Using a do-while loop int n = 1; do { System.out.println(n); n = n + 1; } while (n <= 10); Don’t forget the semicolon!
18
Using a do-while loop What if n = 20? int n = 20; do { System.out.println(n); n = n + 1; } while (n <= 10);
19
Infinite loop int n = 1; do { System.out.println(n); // n = n + 1; } while (n <= 10);
20
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
21
for loop Components of a for loop: ◦ Initializing actions ◦ Boolean expression ◦ Loop body ◦ Update actions
22
for loop 22 Evaluate Boolean expression Execute Body End loop true false Execute Initializing actions Execute Update actions
23
for loop syntax for (initializing actions; Boolean expression; update actions) { statements; // loop body }
24
Using a for loop int n; for (n = 1; n <= 10; n++) { System.out.println(n); }
25
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; }
26
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); }
27
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); }
28
Tracing a for loop int n; for (n = 1; n <= 3; n++) { System.out.println(n); } 1 2 3 n = ? n = 1 n = 2 n = 3 n = 4 Execute initializing actions Evaluate Boolean expression Execute body Execute update actions
29
Infinite loop int n; for (n = 1; n <= 10; n = 0) { System.out.println(n); }
30
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
31
Wednesday Spend some more time with loops 31
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.