Exercise 1 of 5
Your First Loop
A for loop repeats code. It has three parts:
for (int i = 0; i < 3; i++) {
IO.println(i);
}
// Prints: 0, 1, 2
int i = 0 — start at 0
i < 3 — keep going as long as i is less than 3
i++ — increase i by 1 after each pass
Change the 3 to a 5 so the numbers 0 to 4 get printed.
Starter code
void main() {
for (int i = 0; i < 3; i++) {
IO.println(i);
}
}
Background
The three parts of a for loop
for (init; condition; update) — the three parts run at different times:
1. init — once at the very start. 2. condition — checked before every pass. 3. update — after every pass.
All three parts are optional. for (;;) is an infinite loop (just like while(true)).
Exercise 2 of 5
Loop: Start Value
You can change the start value of the loop:
for (int i = 5; ...) // starts at 5
Change the start value so the numbers 1 to 5 get printed.
Change only one number.
Starter code
void main() {
for (int i = 0; i <= 5; i++) {
IO.println(i);
}
}
Background
Why start at 0?
In computer science you almost always start counting at 0 — because array indexes are offsets from the start. The first element sits 0 positions away from the beginning.
This "off-by-one" thing (being wrong by exactly 1) is one of the most common bugs in software development.
Exercise 3 of 5
Countdown
Loops can also run backwards:
for (int i = 10; i >= 1; i--) {
// i-- means: decrease i by 1
}
The loop is almost done. Replace ??? with the right expression so it counts down from 5 to 1.
Starter code
void main() {
for (int i = 5; i >= 1; ???) {
IO.println(i);
}
}
Background
Increment and decrement
i++ is short for i = i + 1. There are two flavors: i++ (post-increment) and ++i (pre-increment). The difference: int a = i++; stores the old value first and then increases i. int a = ++i; increases i first and then stores it.
In a for loop it makes no difference, because the resulting value isn't used.
Exercise 4 of 5
Loop with a Condition
You can use an if inside a loop — that's how you filter values.
The loop runs from 1 to 10. Fill in the condition of the if so only even numbers get printed.
Tip: a number is even if i % 2 == 0 (no remainder when divided by 2).
Starter code
void main() {
for (int i = 1; i <= 10; i++) {
if (???) {
IO.println(i);
}
}
}
Background
The modulo operator %
% gives you the remainder of a division. 7 % 3 = 1 (7 divided by 3 = 2, remainder 1). That's how you check divisibility: x % n == 0 means "x is divisible by n".
Common uses: checking even/odd, "wrapping" values around (e.g. index % array.length to cycle through an array), or doing clock math.
Exercise 5 of 5
While Loop
A while loop runs as long as the condition is true:
while (condition) {
// code
}
The loop should start at 1 and stop at 3. Fill in the condition.
Starter code
void main() {
int i = 1;
while (???) {
IO.println(i);
i++;
}
}
Background
for vs. while — when to use which?
Use for when you know in advance how many times the loop should run (e.g. "5 times" or "for every element"). Use while when you don't — e.g. "read lines until the end of the file" or "repeat until the user types 'exit'".
There's also do-while: like while, but the body runs at least once, because the condition is only checked at the end.