- Loops repeat statements instead of copying them 100 times.
- Opening problem: print a string 100 times.
- while (count < 100) { …; count++; }
- while loop: checks the condition before each iteration.
- while flowchart.
🎯 How this can come up · 1 question
How many times does int i = 0; while (i < 5) i++; run the body?
Correct: 5.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Repeat a question until the answer is correct.
- Sentinel value: a special input (e.g., 0) that ends the loop.
- Advanced math tool: ten questions, count correct answers.
- Guessing numbers: higher/lower until correct.
- Caution: don’t compare floating-point values for equality in loop conditions.
- do-while: body first, then test: runs at least once.
🎯 How this can come up · 1 question
Which loop always executes its body at least once?
Correct: do-while.
- Exercise: read an ASCII code and print its character.
- for (initial-action; continuation-condition; action-after-each-iteration).
🎯 How this can come up · 1 question
How many lines does for (int i = 1; i <= 10; i += 3) print?
i = 1, 4, 7, 10.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- The initial and update parts may hold several comma-separated statements.
- If the condition is omitted it is implicitly true: for ( ; ; ) is infinite.
- Caution: a semicolon right after for (...) ; makes an empty body.
🎯 How this can come up · 1 question
What is wrong with: for (int i = 0; i < 10; i++); { System.out.println("i is " + i); }
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).
- Same caution for while.
- 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 on choosing loops.
- Nested loops: the inner loop runs fully for each outer iteration (multiplication table).
🎯 How this can come up · 1 question
for (i = 0; i < 3; i++) for (j = 0; j < 4; j++) print. How many prints?
Correct: 12.
- Minimize numerical errors: accumulate small numbers first, use integer counters.
- GCD by testing k from 2 up while k ≤ n1 and k ≤ n2.
- Future tuition: years until tuition doubles at 7% a year.
- break exits the loop; continue skips to the next iteration.
- break example: stop when sum > 100.
🎯 How this can come up · 1 question
Which statement immediately ends the innermost loop?
Correct: break.
- continue example: skip 10 and 11 in the sum.
🎯 How this can come up · 1 question
continue in a loop:
Correct: Skips the rest of the current iteration.
- Guessing number with break.
- Palindrome: compare characters from both ends moving inward.
- Prime test: divide by 2 … sqrt(n); prime if no divisor found.
🎯 How this can come up · 1 question
Write a loop that checks whether n > 1 is prime.
boolean isPrime = true; for (int d = 2; d <= Math.sqrt(n); d++) { if (n % d == 0) { isPrime = false; break; } }
- Display the first 50 primes, 10 per line.
No slides match this filter.