6. Loops

While loops

Here's a while loop that prints looping 3 times.

int i = 0;
System.out.println("outside the loop");
while (i < 3) {
    System.out.println("looping");
    i++;
}
System.out.println("done");

While loops look basically the same in Java as they do in Python. But like with if-statements, the parenthesis around the condition (i < 15) are required, and so are the curly brackets { }.

The i++ line is just shorthand for adding 1 to i. These are all equivalent:

i = i + 1;
i += 1;
i++;

Similarly, we can use i--, i -= 1, or i = i - 1 to subtract 1 from i.

For loops

We can make a for loop that's equivalent to the while loop above.

System.out.println("outside the loop");
for (int i = 0; i < 3; i++) {
    System.out.println("looping");
}
System.out.println("done");

For loops have three parts:

for (create counter; end condition; modify counter)

Note that there are semicolons ; separating each part, not commas.

The three parts

for (int i = 0;

The first part is done only once when the loop begins. It creates a counter variable to track each loop iteration. In this case, our counter variable is named i and is initialized to 0.

i < 3;

The second part is a boolean expression that's checked every time you enter the for loop. We only enter the loop if the expression is true, so this loop will end when i becomes equal to or greater than 3.

i++) {

The third part is executed at the end of every iteration. In this case, we add 1 to i.

Unrolled

for (int i = 0; i < 3; i++) {
    System.out.println("looping");
}

"Unrolled", our for loop does these things:

  1. Creates an int variable i with value 0.
  2. Checks to see if i < 3. It is, so we enter the loop.
  3. Prints out looping.
  4. We've reached the end of the loop, so it adds 1 to i. i now equals 1.
  5. It goes back to the top and checks if i is still less than 3. It still is, so we loop again.
  6. Prints out looping.
  7. Adds 1 to i. i now equals 2.
  8. i < 3 is true, so we reenter the loop.
  9. Prints out looping.
  10. Adds 1 to i. i is now 3.
  11. i < 3 is no longer true, so we don't reenter the loop. We're done.

So in sum, our for loop loops three times and thus prints looping three times.

Exercises

These exercises will get you more comfortable with loops in Java. Some of them may more be challenging. Don't worry about it. Just do your best, google around, and move on if you get stuck.


  1. Debug this program. Make sure to compile after every change you make to get used to what the error messages look like.

    for int i = 0, i < 5, i = i + 1
    System.out.println(i);
  2. Write a while loop that prints the word hello 5 times.

  3. This program

    for (int i = 0, i < 9, i++) {
        if (i%2 == 0) {
            System.out.print(i + " ");
        }
    }
    System.out.println();

    should print out

    1 3 5 7 9 

    But it doesn't. Try to debug it.

  4. This program with a nested for loop

    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            System.out.println(i * j + " ");
        }
        System.out.println();
    }

    should print out this 5 x 5 multiplication table

    1 2 3 4 5 
    2 4 6 8 10 
    3 6 9 12 15 
    4 8 12 16 20 
    5 10 15 20 25

    But it doesn't. Try to debug it.

  5. Write a while loop that prints the numbers 1 through 5 backwards, like this:

    5
    4
    3
    2
    1
  6. Write a for loop that prints the numbers 1 through 10 backwards all on one line, like this:

    10 9 8 7 6 5 4 3 2 1 
  7. Write a loop that prints all even numbers between 0 and 20 (inclusive) on the same line, like this:

    0 2 4 6 8 10 12 14 16 18 20

    Can you do it without using an if-statement?