This site is retired and is no longer updated since November 2021. Please visit our new website at https://www.teamscode.org for up-to-date information!
For Loops

For loops are control structures that allow the computer to perform a task over and over again.

    for (init; test; update statement) {

            body;

    }

First the computer runs the statement in the init statement. The init statement is normally used to initialize the counter used for the test and update; it is executes once at the beginning. Next, it evaluates the test. The test is just a conditional for the continuation of the for loop. If the test returns true, the body executes and at afterward the update executes. After the update statement has finished, the cycle restarts. Once the test evaluates as false, the control passes the loop.

The code above is only an abstract version of the explanation. Once you replace the placeholders with real code, the for loop will run.

A simple non-abstract example is using a for loop to print all the numbers from 1 to 10:

    for (int i = 1; i <= 10; i++) {

            System.out.println(i);

    }

    System.out.println("Finished!");

This code will provide the output:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    Finished!

Let’s try to follow along. First the for loop initializes integer i at 1. I then prints out i, increments by 1 (I used i++ to increment but i += 1 works). It then checks whether the condition is false; if it isn’t, the loop repeats. This continues till i reaches 10, thus making the condition false, then it finally exits and prints “Finished!”.

When making a for loop, you can use pre-existing variables. However, if you are using a pre-existing variable, you can’t re-declare the variable. The code below will result in the same output as above.

    int i;

    for (i = 0; i <= 10; i++) {

        System.out.println(i);

    }

    System.out.println("Finished!");

The variable inside the test can change. It acts like any regular variable; this means all regular operators work just as well.

For example this line of code will update the variable inside the for loop:

    for (int i = 1; i < 50; i++) {

        i = i * 2;

        System.out.println(i);

    }

This will result in the following output:

    2

    6

    14

    30

    62

This occurs because the i variable is updated in the for loop as well. This chart is shows the cycle and the value of i. The first value of i in every cycle is printed out.

Cycle i
0 1
1 2
3
2 6
7
3 14
15
4 30
31
5 62
63

The for loop terminates after the 5th cycle, because 63 is larger than 50. But this piece of code shows that the variables in the expression can be changed to deviate from the how you might expect.

Lesson Quiz

1. How many times do the below loops repeat?

    for (int i = 0; i < 5; i++) {



    }
a. 4
b. 5
c. 6
d. 7
    for (int i = 0; i <= 10; i++) {

        i++;

    }
a. 4
b. 5
c. 6
d. 7

2. What does the below code block output?

    for (int i = 1; i <= 4; i++) {

        System.out.print((i * i) + " ");

    }
a. 1 2 3 4
b. 1 4 9
c. 1 2 3
d. 1 4 9 16

Written by Jason Zhang

Notice any mistakes? Please email us at learn@teamscode.com so that we can fix any inaccuracies.