Java For Loop
Syntax, Flow & Examples
Java For Loop
The for loop in Java is used to repeat a block of code a certain number of times. It's concise, predictable, and incredibly useful when you know in advance how many times a loop should run.
Syntax of a Java For Loop
for (initialization; condition; update) {
// code block to be executed
}
This syntax has three parts:
- Initialization: Executed once before the loop starts.
- Condition: Checked before each iteration. Loop continues if this is
true
. - Update: Executed after each iteration. Usually increments or decrements a variable.
Basic Example: Print 1 to 5
public class ForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Number: " + i);
}
}
}
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Explanation: The loop starts with i = 1
. As long as i <= 5
, the loop executes and prints the current value. After every iteration, i++
increases i
by 1.
Descending For Loop
for (int i = 5; i > 0; i--) {
System.out.println(i);
}
5
4
3
2
1
Here, we reverse the iteration by starting at 5 and decreasing i
until it becomes less than or equal to 0.
Loop with Custom Step Size
for (int i = 0; i <= 10; i += 2) {
System.out.println("Even: " + i);
}
Even: 0
Even: 2
Even: 4
Even: 6
Even: 8
Even: 10
Instead of incrementing by 1, here we increment by 2 to skip even numbers directly.
Looping Over an Array
One of the most common use cases for a for
loop is iterating over arrays:
String[] fruits = { "Apple", "Banana", "Mango" };
for (int i = 0; i < fruits.length; i++) {
System.out.println(fruits[i]);
}
Apple
Banana
Mango
Infinite For Loop
A for loop without a condition can result in an infinite loop:
for (;;) {
System.out.println("This goes on forever...");
}
This loop runs endlessly since there's no condition to stop it. Use such loops carefully, often in conjunction with a break
statement.
Using Break to Exit a For Loop
for (int i = 1; i <= 10; i++) {
if (i == 6) break;
System.out.println(i);
}
1
2
3
4
5
Once i == 6
, the loop exits. The break
statement is often used to terminate loops early based on a condition.
Using Continue to Skip an Iteration
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
System.out.println(i);
}
1
2
4
5
When i == 3
, the continue
statement skips that iteration and continues with the next one.
Real-Life Analogy
Imagine checking your emails every morning for the next 7 days. A for
loop is like automating this task: "Start on Monday, repeat daily, and stop after Sunday." Simple, predictable, effective — just like a for loop!
Practice Ideas
- Print all odd numbers from 1 to 100
- Find the sum of the first 10 natural numbers
- Print a pattern like a triangle using nested for loops
QUIZ
Question 1:What part of a Java for
loop is executed only once?
Question 2:The condition in a Java for
loop is checked after each iteration.
Question 3:Which of the following can lead to an infinite loop in Java?
Question 4:What will be the output of the following code?
for (int i = 5; i > 0; i--) {
System.out.println(i);
}
for (int i = 5; i > 0; i--) {
System.out.println(i);
}