Loops
On this page 8 topics
Overview
Topics
- for
- enhanced for
- while
- do-while
- break
- continue
- nested loops
Learn first
70 min of reading · 5 h of practice (estimate)
Learning outcomes
- Choose
for,while, ordo-whilefor a given task and justify the choice. - Write loops with correct start, condition, and update, avoiding off-by-one and infinite loops.
- Use
breakandcontinueappropriately. - Write nested loops for grid-style problems and estimate how many times the body runs.
Lessons
- 01Why loops: repetition without copy-paste10 min read
- 02The for loop10 min read
- 03while and do-while10 min read
- 04break, continue, and loop control10 min read
- 05Nested loops10 min read
- 06Off-by-one errors and infinite loops10 min read
- 07Demo: number guessing game, stepping through the loop in the debugger10 min read
Lab and exercises
Lab
BEG-013-LAB1 · Number guessing game (mini-project 1 of 3)
Outcomes: 1–4 · Time: ~3 h
- Pick a random target between 1 and 100.
- Allow a limited number of attempts (for example 7); give higher/lower hints.
- Reject input that is not a number or is out of range without losing an attempt.
- Report the number of attempts used at the end.
- Ask "play again?" and replay without restarting the program.
- Step through one game in the debugger.
Acceptance: handles invalid input, reports attempts, replay works.
Exercises (assessment: seven exercises plus the guessing game)
| ID | Outcome | Task |
|---|---|---|
| BEG-013-EX01 | 1 | For each task pick for, while, or do-while and justify: print 1-10; read until "quit"; a menu shown at least once. |
| BEG-013-EX02 | 2 | Print the sum of 1 to 100 with a for loop. |
| BEG-013-EX03 | 2 | This loop never ends. Fix it: int i = 0; while (i < 10) { System.out.println(i); } |
| BEG-013-EX04 | 2 | Print the numbers 10 down to 1 with no off-by-one error. |
| BEG-013-EX05 | 3 | Print 1-20, skipping multiples of 3 and stopping at the first number above 15. |
| BEG-013-EX06 | 4 | Print a 5-row triangle of stars. |
| BEG-013-EX07 | 4 | Print the multiplication table 1-9 and state how many times the body runs. |
Show solutions
- EX01
for(known count);while(unknown count, may run zero times);do-while(at least once). - EX02
int sum = 0; for (int i = 1; i <= 100; i++) sum += i;result 5050. - EX03 Add
i++;inside the body. - EX04
for (int i = 10; i >= 1; i--). - EX05
for (int i = 1; i <= 20; i++) { if (i > 15) break; if (i % 3 == 0) continue; System.out.println(i); } - EX06
for (int r = 1; r <= 5; r++) { for (int c = 1; c <= r; c++) System.out.print("*"); System.out.println(); } - EX07 Nested loops 1..9 by 1..9; the body runs 81 times.
- The guessing game and quiz also cover outcomes 2 and 3 further.
Quiz
Outcomes: O1 choosing a loop · O2 correct loop parts · O3 break/continue · O4 nested loops.
A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.
Practical part
Seven exercises BEG-013-EX01 to EX07 plus the guessing game. Pass: at least six exercises correct and a working guessing game that meets every acceptance point.
Common mistakes
Off-by-one boundaries; modifying the loop variable in the body; forgetting to update the while condition.
Assessment
7 exercises plus the guessing game.
Career relevance
Iteration cost leads into complexity (BEG-039).