How many times does int i = 0; while (i < 5) i++; run the body?
Loops
while, do-while and for loops, sentinel values, which loop to use, nested loops, numerical errors, break and continue, and the classic loop problems: guessing numbers, GCD, future tuition, palindromes and primes.
How to read this page
Every original slide is shown unchanged. Yellow marks on a slide point to a numbered note: hover or tap a mark to jump to its note, and hover a note to see its marks. Questions tagged Past exam appeared on a real paper; Prediction questions are likely angles, not confirmed. Pick an answer to each question: a wrong pick tells you why it is wrong, so you can try again.
All 50 slides
- 1 Loops
- 2 Motivations
- 3 Opening Problem
- 4 Introducing while Loops
- 5 while § loop (§5.2). §
- 6 while
- 7 animation
- 8 animation
- 9 animation
- 10 animation
- 11 animation
- 12 animation
- 13 animation
- 14 animation
- 15 animation
- 16 Problem: Repeat Addition Until Correct
- 17 Ending a Loop with a Sentinel Value
- 18 Problem: An Advanced Math Learning Tool
- 19 Problem: Guessing Numbers
- 20 Caution
- 21 do-while
- 22 /*(Find the character of an ASCII code) Write a program that receives an ASCII code(an integer between 0 and 127) and displays its character.*/ Scanner input = new Scanner(System.in); // Prompt the user to enter an ASCII code
- 23 for
- 24 animation
- 25 animation
- 26 animation
- 27 animation
- 28 animation
- 29 animation
- 30 animation
- 31 animation
- 32 animation
- 33 animation
- 34 Note
- 35 Note
- 36 Caution
- 37 Caution, cont.
- 38 Which Loop to Use?
- 39 Recommendations
- 40 Nested Loops
- 41 Minimizing Numerical Errors
- 42 Problem: Finding the Greatest Common Divisor
- 43 Problem: Predicting the Future Tuition
- 44 break continue
- 45 break
- 46 continue
- 47 Guessing Number Problem Revisited
- 48 Problem: Checking Palindrome
- 49 Write a program to get a number from user and test whether it is prime or not. An integer greater than 1 is prime if its only positive divisor is 1 or itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not
- 50 Problem: Displaying Prime Numbers
Loops
Notes
Tip while: test first, may run 0 times · do-while: test after, runs at least once · for: counter loops (init; condition; update).
Motivations
Notes
Loops repeat statements instead of copying them 100 times.
while § loop (§5.2). §
Notes
while loop: checks the condition before each iteration.
while
Notes
while flowchart.
Exam angle
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
Problem: Guessing Numbers
Notes
Guessing numbers: higher/lower until correct.
Caution
Notes
Trap while (item != 0) with doubles may never stop because of round-off.
do-while
Notes
do-while: body first, then test: runs at least once.
Exam angle
Which loop always executes its body at least once?
for
Exam angle
How many lines does for (int i = 1; i <= 10; i += 3) print?
- i = 1, 4, 7, 10.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
Note
Notes
The initial and update parts may hold several comma-separated statements.
Caution
Notes
Caution: a semicolon right after for (...) ; makes an empty body.
Exam angle
What is wrong with: for (int i = 0; i < 10; i++); { System.out.println("i is " + i); }
Compare with the model answer
The semicolon gives the loop an empty body; the block runs once afterwards (and i is out of scope there, so it does not even compile).
Which Loop to Use?
Notes
The three loops are equivalent; use for when the count is known, while for unknown counts, do-while when the body must run first.
Recommendations
Notes
Recommendations on choosing loops.
Nested Loops
Notes
Nested loops: the inner loop runs fully for each outer iteration (multiplication table).
Exam angle
for (i = 0; i < 3; i++) for (j = 0; j < 4; j++) print. How many prints?
Minimizing Numerical Errors
Notes
Minimize numerical errors: accumulate small numbers first, use integer counters.
Problem: Finding the Greatest Common Divisor
Notes
GCD by testing k from 2 up while k ≤ n1 and k ≤ n2.
break continue
Notes
break exits the loop; continue skips to the next iteration.
break
Exam angle
Which statement immediately ends the innermost loop?
continue
Notes
continue example: skip 10 and 11 in the sum.
Exam angle
continue in a loop:
Problem: Checking Palindrome
Notes
Palindrome: compare characters from both ends moving inward.
Write a program to get a number from user and test whether it is prime or not. An integer greater than 1 is prime if its only positive divisor is 1 or itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not
Notes
Prime test: divide by 2 … sqrt(n); prime if no divisor found.
Exam angle
Write a loop that checks whether n > 1 is prime.
Compare with the model answer
boolean isPrime = true; for (int d = 2; d <= Math.sqrt(n); d++) { if (n % d == 0) { isPrime = false; break; } }