When we need to execute the same piece of code multiple times, writing that code again and again is obviously not a good approach. Imagine you want to print numbers from 1 to 10. You could write ten different System.out.println() statements, but what if you wanted to print numbers from 1 to 100?
Writing the same statement 100 times would be inefficient and difficult to maintain.
This is where loops come into the picture.
In Java, the for loop allows us to execute a block of code repeatedly as long as a particular condition remains true.
For example, we can use a for loop to:
-
Print numbers from 1 to 10
-
Print a multiplication table
-
Repeat a calculation multiple times
-
Process a sequence of values
-
Execute a particular block of code a fixed number of times
In this lesson, we will understand how the for loop works from the ground up, including its initialization, condition, increment, execution flow, and some practical examples.
What Is a Loop?
Before directly jumping into the for loop, let's understand the basic idea behind a loop.
Suppose you are asked to print:
1
2
3
4
5
6
7
8
9
10
One way would be to write:
System.out.println(1);
System.out.println(2);
System.out.println(3);
System.out.println(4);
System.out.println(5);
And continue until 10.
But notice something here. The statement itself is not changing:
System.out.println(...)
Only the value being printed is changing.
Instead of manually repeating the same code, we can tell Java:
"Run this piece of code repeatedly, and change the value after every execution."
That's essentially what a loop does.
Java provides different types of loops, and the for loop is particularly useful when we know how many times we want something to repeat or when we have a clear starting point, condition, and increment/decrement operation.
Understanding Increment and Decrement Operators
Before understanding the for loop, it is useful to understand the increment operator because we will use it frequently with loops.
The increment operator is:
++
It increases the value of a variable by 1.
For example:
int a = 4;
a++;
System.out.println(a);
The output will be:
5
Why?
Initially:
a = 4
Then:
a++;
means:
a = a + 1
So the value becomes:
a = 5
Therefore:
System.out.println(a);
prints:
5
Similarly, if we write:
int a = 7;
a++;
System.out.println(a);
The output will be:
8
Decrement Operator
There is also an opposite operation called the decrement operator:
--
It decreases a value by 1.
For example:
int a = 7;
a--;
System.out.println(a);
Output:
6
So, as a simple rule:
a++ → increase a by 1
a-- → decrease a by 1
The increment operator becomes particularly useful in loops because after every iteration, we can increase our counter and move toward the condition becoming false.
What Is the for Loop in Java?
The basic syntax of a for loop looks like this:
for (initialization; condition; increment/decrement) {
// code to execute
}
There are three important parts inside the parentheses:
Initialization
Condition
Increment / Decrement
And the code that we want to execute repeatedly goes inside the curly braces:
for (initialization; condition; increment/decrement) {
// code
}
Let's look at an actual example:
for (int a = 0; a < 10; a++) {
System.out.println(a);
}
At first, this syntax may look a little confusing.
So let's break it down.
1. Initialization
The first part is initialization:
int a = 0;
Here, we are creating a variable called a and giving it an initial value of 0.
You have already seen variable initialization in Java:
int a = 4;
The same concept is being used here.
In our loop:
for (int a = 0; a < 10; a++) {
we are saying:
"Start the variable
awith the value 0."
You can think of a as a counter.
For example, imagine you are counting people entering a room.
You might start counting from:
0
Then:
1
2
3
4
...
The variable a is playing a similar role in our loop.
2. Condition
The second part is the condition:
a < 10
The condition determines whether the loop should execute.
Java checks this condition before executing the code inside the loop.
Our condition says:
"Run the loop while
ais smaller than10."
Initially:
a = 0
Java checks:
0 < 10
This is true.
Therefore, the code inside the loop executes.
After that, a gets incremented.
Now:
a = 1
Java checks again:
1 < 10
Again, true.
So the code executes again.
This continues until the condition becomes false.
3. Increment
The third part is:
a++
This tells Java to increase the value of a by 1 after an iteration.
So the values will move like this:
0 → 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9 → 10
Once a becomes 10, Java checks:
10 < 10
This is false.
Therefore, the loop stops.
How Does a for Loop Actually Execute?
Let's take our complete example:
for (int a = 0; a < 10; a++) {
System.out.println(a);
}
Let's follow it step by step.
Step 1: Initialization
First:
int a = 0;
So:
a = 0
Step 2: Condition Check
Java checks:
0 < 10
This is true.
Therefore:
System.out.println(a);
executes.
Output:
0
Step 3: Increment
Now:
a++;
So:
a = 1
Step 4: Condition Again
Java checks:
1 < 10
True.
So it prints:
1
Then a++ makes:
a = 2
And the same process continues.
Eventually, we reach:
a = 9
The condition:
9 < 10
is true, so 9 gets printed.
Then:
a++
makes:
a = 10
Now Java checks:
10 < 10
This is false.
The loop stops and execution moves outside the for loop.
Therefore, the output is:
0
1
2
3
4
5
6
7
8
9
Notice that 10 is not printed.
What Happens If We Use <= Instead?
Now suppose we change:
a < 10
to:
a <= 10
Our loop becomes:
for (int a = 0; a <= 10; a++) {
System.out.println(a);
}
Now when a becomes 10, Java checks:
10 <= 10
This is true because 10 is equal to 10.
Therefore, 10 is also printed.
The output becomes:
0
1
2
3
4
5
6
7
8
9
10
This small difference between < and <= is very important when working with loops.
a < 10
means:
amust be strictly smaller than 10.
While:
a <= 10
means:
acan be smaller than or equal to 10.
Printing Numbers from 1 to 10
What if we don't want 0 and instead want to print numbers from 1 to 10?
We simply change our initialization:
for (int a = 1; a <= 10; a++) {
System.out.println(a);
}
Now the loop starts with:
a = 1
and continues while:
a <= 10
The output will be:
1
2
3
4
5
6
7
8
9
10
This demonstrates an important point about the for loop:
The initialization determines where the loop starts, the condition determines when it stops, and the increment/decrement determines how the value changes.
A Real-World Example: Counting Items
Imagine you are building a small application for a shop.
You have to display the numbers of items being processed:
Processing item 1
Processing item 2
Processing item 3
...
Processing item 10
Instead of writing ten separate statements, a for loop can handle it:
for (int item = 1; item <= 10; item++) {
System.out.println("Processing item " + item);
}
Here:
int item = 1;
means we start with item number 1.
item <= 10
means we continue until item number 10.
And:
item++
moves us to the next item.
This is one of the basic patterns you will see repeatedly while programming.
Printing a Multiplication Table Using for Loop
Now let's use the for loop for something a little more interesting.
Suppose we want to print the multiplication table of 12.
We already know how to print numbers from 1 to 10:
for (int a = 1; a <= 10; a++) {
System.out.println(a);
}
Instead of printing just a, we can multiply it by 12:
for (int a = 1; a <= 10; a++) {
System.out.println(12 * a);
}
Let's understand what happens.
During the first iteration:
a = 1
So:
12 * 1 = 12
During the second iteration:
a = 2
So:
12 * 2 = 24
Then:
12 * 3 = 36
And this continues until:
12 * 10 = 120
The output becomes:
12
24
36
48
60
72
84
96
108
120
So with only a few lines of code, we have generated the complete multiplication table.
Making the Multiplication Table More Readable
The previous output works, but we can make it easier to understand.
Instead of:
12
24
36
48
we might want:
12 * 1 = 12
12 * 2 = 24
12 * 3 = 36
We can do that using string concatenation.
Our code becomes:
for (int a = 1; a <= 10; a++) {
System.out.println("12 * " + a + " = " + (12 * a));
}
Now let's understand what is happening.
We have:
"12 * "
This is a string.
Then:
a
is our variable.
Then:
" = "
is another string.
Finally:
(12 * a)
performs the multiplication.
For the first iteration, a is 1.
So Java effectively produces:
"12 * " + 1 + " = " + 12
The output becomes:
12 * 1 = 12
For the next iteration, a becomes 2:
12 * 2 = 24
And so on.
The final output is:
12 * 1 = 12
12 * 2 = 24
12 * 3 = 36
12 * 4 = 48
12 * 5 = 60
12 * 6 = 72
12 * 7 = 84
12 * 8 = 96
12 * 9 = 108
12 * 10 = 120
What Is String Concatenation?
String concatenation simply means joining strings and other values together using the + operator.
For example:
String A = "This is ";
String B = "Rohan";
System.out.println(A + B);
Output:
This is Rohan
Here, the + operator joins the two strings.
The same idea is being used in our multiplication table:
System.out.println("12 * " + a + " = " + (12 * a));
We are combining text, a variable, more text, and the result of a calculation.
This is particularly useful when displaying meaningful output to the user.
Changing the Table Is Easy
One nice thing about this approach is that if we want another multiplication table, we don't need to rewrite the loop.
For example, change:
12 * a
to:
19 * a
The code becomes:
for (int a = 1; a <= 10; a++) {
System.out.println("19 * " + a + " = " + (19 * a));
}
Now we get:
19 * 1 = 19
19 * 2 = 38
19 * 3 = 57
19 * 4 = 76
...
19 * 10 = 190
The loop itself hasn't changed.
Only the number being multiplied has changed.
This is one of the main advantages of loops: we write the repetitive logic once and let the program handle the repetition.
Understanding the Three Parts of a for Loop
At this point, you can remember the structure of a for loop using three simple questions.
1. Where should I start?
That's the initialization:
int a = 1;
2. How long should I continue?
That's the condition:
a <= 10
3. How should I move to the next iteration?
That's the increment/decrement:
a++
So:
for (int a = 1; a <= 10; a++) {
// code
}
can be mentally understood as:
Start
aat 1 → keep running whileais at most 10 → increaseaby 1 after every iteration.
Once you understand this pattern, the syntax becomes much easier to read.
A Simple Mental Model
Think of a for loop like climbing stairs.
You start from a particular step.
For example:
Step 1
Then you ask:
"Have I reached the last step?"
If not, you perform your task and move to the next step.
So:
Start
↓
Check condition
↓
Condition true?
↓
Execute code
↓
Increment / Decrement
↓
Check condition again
↓
...
↓
Condition false
↓
Exit loop
This is essentially the execution flow of a for loop.
Common Pattern to Remember
One of the most common for loop patterns in Java is:
for (int i = 0; i < 10; i++) {
// code
}
You will encounter this pattern frequently while learning Java and later when working with arrays, collections, algorithms, and other programming concepts.
For now, the important thing is not to memorize the syntax blindly.
Understand what each part is doing.
for (int i = 0; i < 10; i++)
means:
Start i at 0
↓
Check whether i < 10
↓
Execute the code
↓
Increase i by 1
↓
Check again
Once the condition becomes false, Java exits the loop.
Final Takeaway
The for loop is one of the fundamental looping mechanisms in Java. It allows us to execute the same block of code multiple times without manually writing that code again.
A for loop generally contains three important parts:
for (initialization; condition; increment/decrement) {
// code
}
Initialization decides where the loop starts:
int a = 1;
Condition decides whether the loop should continue:
a <= 10
Increment or decrement changes the value after each iteration:
a++
For example:
for (int a = 1; a <= 10; a++) {
System.out.println(a);
}
prints numbers from 1 to 10.
And we can use the same idea for practical tasks such as generating a multiplication table:
for (int a = 1; a <= 10; a++) {
System.out.println("12 * " + a + " = " + (12 * a));
}
The key idea is simple:
Start somewhere → check a condition → execute the code → change the value → repeat until the condition becomes false.
Once this flow becomes clear, for loops stop looking complicated.
In the next lesson, we can take this concept one step further and explore nested for loops, where one loop is placed inside another loop. This becomes especially useful when working with patterns, tables, matrices, and other problems where repetition happens at multiple levels.