Skip to lesson

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.

The cycle of a for loop: start with i = 1 once, check i <= 10, if true run the body (print i), then the update i++, and go back to the check. When the check is false, leave the loop.
The start runs only once. The check runs before every round, and the update after every round.

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.

Syntax
for (start; condition; update) {
body
}

Example 1 · The 7 times table

TableOfSeven.javaJava
public class TableOfSeven {    public static void main(String[] args) {        for (int i = 1; i <= 10; i++) {            System.out.println("7 x " + i + " = " + (7 * i));        }    }}
Output
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 = 1 is the start, i <= 10 the condition and i++ the update (add 1, as in Increment and decrement (opens in a new tab)).
  • i goes from 1 to 10, so the body runs 10 times, one row per round.
  • i is an ordinary int, so the body can use it: (7 * i) is in brackets, so Java multiplies first and then joins the answer to the text.
Always put braces around the body, even for one line. Without them, only the next single line repeats.

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

public class ThreeRounds {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
System.out.println("Round " + i);
}
System.out.println("Loop over");
}
}

Variables

i
1

Output

(nothing printed yet)

Line 3Start: int i = 1 makes the counter. This part runs only once, before everything else.

Step 1 of 9

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--

Countdown.javaJava
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");    }}
Output
5
4
3
2
1
Lift off

What this code does

  • The loop starts at 5 and i-- takes away 1 each round.
  • When i becomes 0, 0 >= 1 is false and the loop stops.
  • Lift off is outside the braces, so it prints once, after the loop.

Example 3 · Even numbers with i += 2

EvenNumbers.javaJava
public class EvenNumbers {    public static void main(String[] args) {        for (int i = 2; i <= 10; i += 2) {            System.out.print(i + " ");        }        System.out.println();    }}
Output
2 4 6 8 10

What this code does

  • i += 2 adds 2 each round, so i is 2, 4, 6, 8 and 10. After 10 it becomes 12, and 12 <= 10 is false.
  • print keeps the numbers on one line; the last println() 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.

Java
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 4

To 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