Skip to content

The for Loop#

We introduced the while-loop, because it is easier in the beginning. But the much more common loop you might be using is the for-loop. Let's take a look:

for (int i = 0; i < 10; i++) {
    println("this is the most common loop!");
}

That is a mouthful. Let's break it down into a few separate steps:

  • for: tells us, that a for loop is beginning here.
  • ( ... ): in these parentheses, we'll have three statements that control the loop (more on that later).
  • { ... }: And finally, there's another block of code that gets executed, just like with if/else or while.

So, what are the three statements 🐚🐚🐚?#

With the while-loop, we have seen a pretty common pattern: We usually had some kind of counter, which we initialized before our loop, which we checked before each loop and which we then changed somewhere in that loop.

These are our three statements!

The for-loop rolls all these three things into its parentheses, so they are in a single place. These three statements are separated by a semi-colon ; each.

  • int i = 0: We initialize a counter variable. i in this case is a very common name for that. Think "iteration". This first statement only happens once before that loop starts running. This is therefore known as the "initialization expression".
  • i < 10: Here, we compare our counter. This happens at the beginning of each single iteration of our loop. As long as this comparison remains true✅, this loop keeps running. That's why it is also known as the "termination expression".
  • i++: Here we change our counter. This one happens at the end of every single iteration. We're calling this the "increment expression".

Any Three Statements

This is how these three statements are most often used, and I would encourage you to normally only use them like that. It is however possible to write any three statements you can think of in there, and they will be run in the same way once before all loop iterations, each time in the beginning and each time at the end of a loop. As long as the "termination expression" doesn't return false, this loop keeps going.

🚫 Anti-Example

for (; getMoreData(); ) { /* some code here */}
This would be a valid for-Loop. Very very rarely is this the best way to write it, though.

One thing that does come up, though is this one:

for ( ; ; ) {
    // some code here
}

That's not a typo. There really is nothing in the parentheses. That's one way to write an intentional endless loop. This loop would never quit on its own.

Loops are interchangeable#

while and for can do the exact same things. For is often more concise to write, again, because it keeps all that counting stuff in the same place. Try both loops — sometimes one feels "more correct" than the other.

Both of these will produce the exact same result.

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

Counting in Different Ways#

Counting Differently

If I needed you to print the numbers 5, 10, 15, 20, 25, would you be able to do this?

Hint

You get to choose how much you want your iterator to change

Solution

Here are two different ways to do this, there are many other ways of achieving this, too.

for (int i = 5; i <= 25; i += 5) {
    println(i);
}
for (int i = 0; i <= 5; i += 1) {
    println(i * 5 + 5);
}

Float also works

So far, all our loops were using int, but of course any numeric type works well. And anything else that we can somehow compare, can also work.

for (float angle = 0; angle < TWO_PI; angle += 0.01) {
    // this would go from 0 to 6.28... in increments of 0.01.
}

Relevant excerpt from Learning Processing#

(the section starts at 3:00:10 and runs through 3:04:48, the video should start and stop on these automatically.)