The for loop
22 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A for loop repeats lines a set number of times. It keeps the start, the stopping check and the step to the next round together on one line, so you can read the whole loop at a glance.
Start, stop, move on
You want to print 1 to 10 without writing ten lines. Counting out loud, you start at 1, check before each number that you have not passed 10, and then move to the next one.
A for loop keeps these three parts, where to start, when to stop and how to move on, together on one line. The picture shows the cycle it follows.
Writing a for loop
To print the 7 times table, a counter must run from 1 to 10. Write the start, the condition and the update in round brackets after for, separated by semicolons; the lines that repeat, the body, go in curly braces.
Example 1 · The 7 times table
public class TableOfSeven { public static void main(String[] args) { for (int i = 1; i <= 10; i++) { System.out.println("7 x " + i + " = " + (7 * i)); } }}7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70
What this code does
int i = 1is the start,i <= 10the condition andi++the update (add 1, as in Increment and decrement (opens in a new tab)).igoes from 1 to 10, so the body runs 10 times, one row per round.iis an ordinaryint, so the body can use it:(7 * i)is in brackets, so Java multiplies first and then joins the answer to the text.
A for loop, round by round
How many times will the body run, and what is i when the loop stops? Step through this small loop and watch the order: start once, then check, body, update, check, body, update… until a check is false.
Trace the code
Run a for loop one step at a time
Variables
- i
- 1
Output
(nothing printed yet)Line 3Start: int i = 1 makes the counter. This part runs only once, before everything else.
Counting down and in steps
A rocket launch counts 5, 4, 3, 2, 1, and even roll numbers go 2, 4, 6. Change the three parts to fit the count: start high and use i-- to count down, or i += 2 to jump (compound assignment (opens in a new tab)).
Example 2 · Countdown with i--
public class Countdown { public static void main(String[] args) { for (int i = 5; i >= 1; i--) { System.out.println(i); } System.out.println("Lift off"); }}5 4 3 2 1 Lift off
What this code does
- The loop starts at 5 and
i--takes away 1 each round. - When
ibecomes 0,0 >= 1is false and the loop stops. Lift offis outside the braces, so it prints once, after the loop.
Example 3 · Even numbers with i += 2
public class EvenNumbers { public static void main(String[] args) { for (int i = 2; i <= 10; i += 2) { System.out.print(i + " "); } System.out.println(); }}2 4 6 8 10
What this code does
i += 2adds 2 each round, soiis 2, 4, 6, 8 and 10. After 10 it becomes 12, and12 <= 10is false.printkeeps the numbers on one line; the lastprintln()ends that line.
Any step works the same way: for (int i = 10; i <= 50; i += 10) gives 10 20 30 40 50.
Where the counter lives
You print i after the loop, and Java says error: cannot find symbol. A variable made in the start part exists only inside the loop: that is its scope.
for (int i = 1; i <= 3; i++) { System.out.println(i);}System.out.println(i); // error: cannot find symbol
int n;for (n = 1; n <= 3; n++) { System.out.println(n);}System.out.println(n); // fine: prints 4To use the value after the loop, declare the variable before the loop, like n, and only give it its first value in the start part.
Next step
Check your understanding
9 quick questions