1. Introduction
Imagine you have a task: print “Hello!” to the screen ten times. Sure, you could write ten lines with System.out.println("Hello!"); — but now that you know the while loop, you can handle this without any trouble.
But there’s another loop — for — which repeats the needed action the required number of times. It can even count by itself!
Here’s a typical example:
for (int i = 0; i < 10; i++)
{
System.out.println("Hello!");
}
What does this code do? It prints the word “Hello!” exactly 10 times. No copy-paste and no wrist pain.
2. Syntax of the for loop
The for loop consists of three main parts that are written in parentheses after the for keyword:
for (initialization; condition; update)
{
// Loop body
}
for loop
Let’s break down each element in detail:
- Initialization — usually the creation of a counter variable that tracks the number of repetitions (int i = 0).
- Condition — checked before each iteration. If it’s true, the loop body executes. If it’s false, the loop ends. For example, i < 10.
- Update — an operator that runs after each iteration. It usually increments or decrements the counter (i++).
You can draw a diagram:
3. Comparing for and while
It may be clearer if I say that for is just another way to write a while loop.
We had a while loop:
int i = 0; // Initialize loop variable
while(i<10) // Check the loop continuation condition
{
System.out.println(i); // Useful action (loop body)
i++; // Increment the counter
}
We can rewrite it as a for loop by simply moving all the actions that control the loop logic:
for (int i = 1; i <= 10; i++) // Initialization; Condition check; Increment counter
{
System.out.println(i); // Useful action (loop body)
}
Steps performed by both loops:
- Initialization int i = 0; is executed once before the loop.
- Then the condition is checked i <= 10
- Then the useful action of the loop System.out.println(i);
- Then the counter is incremented i++
- Then it goes back to step 2.
4. Examples of using the for loop
The simplest example
Let’s print the numbers from 1 to 5:
for (int i = 1; i <= 5; i++)
{
System.out.println(i);
}
Output:
1
2
3
4
5
Note: if you wrote i < 5, the last output would be 4.
Countdown
Want to feel like an astronaut? Let’s do a countdown!
for (int i = 5; i > 0; i--)
{
System.out.println(i);
}
System.out.println("Let's go!");
Output:
5
4
3
2
1
Let's go!
Loop step not equal to 1
You can change the counter not only by 1 but by any other number:
for (int i = 0; i <= 10; i += 2)
{
System.out.println(i);
}
// Will print: 0 2 4 6 8 10
5. Variations of the for loop
Multiple variables in a loop
Sometimes it’s convenient to track two variables at once. For example, let’s print numbers right-to-left and left-to-right:
for (int left = 1, right = 10; left <= 10; left++, right--)
{
System.out.println(left + " " + right);
}
Output:
1 10
2 9
3 8
...
10 1
Infinite for loop
If the case is really tough (for example, your project is a perpetual motion machine in IT), you can make an “endless” for loop:
for (;;) // no initialization, no condition, no update
{
System.out.println("Work, work, and work again!");
}
Warning: such a loop will end only if the body contains break, return, or throw. Don’t use it without a reason, otherwise you might awaken the ancient demon — “Process is not responding”.
GO TO FULL VERSION