




- 1Java OOP Introduction
- 2Java Class
- 3Java Class Constructor
- 4Java Class Objects
- 5Java Access Modifiers
- 6Java Static Variables in Classes
- 7Java Static Methods Explained
- 8Java Static Blocks
- 9Java final Variables
- 10Java final Methods
- 11Java final class
- 12Inheritance in Java
- 13Java Method Overriding
- 14Java Abstraction in OOP
- 15Interfaces in Java
- 16Polymorphism in Java
- 17Encapsulation in Java
- 18Java Nested Classes
- 19Java Nested Static Class
- 20Java Anonymous Class
- 21Java Singleton Class
- 22Java Enums
- 23Reflection in Java


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.
For Loop Basic Example: Print 1 to 5
This example shows how to use a for
loop in Java to print numbers from 1 to 5.
Here’s how it works:
int i = 1
sets the starting value ofi
to 1.i <= 5
is the condition that keeps the loop running whilei
is less than or equal to 5.i++
increases the value ofi
by 1 in each loop.
Each time the loop runs, it prints the current value of i
.
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
A descending for
loop counts down instead of up. In this example, the loop starts with i = 5
and decreases i
by 1 each time using i--
. The loop continues as long as i
is greater than 0. This is useful when you need to process things in reverse order.
public class DescendingLoop {
public static void main(String[] args) {
for (int i = 5; i > 0; i--) {
System.out.println(i);
}
}
}
5
4
3
2
1
For Loop with Custom Step Size
You can control how much the loop counter increases on each iteration using a custom step size. This is done by changing the increment part of the for
loop. Instead of increasing by 1 (which is common), here we increase i
by 2 in each step.
This is useful when you want to skip values, like printing only even numbers. The loop starts from 0 and goes up to 10, increasing by 2 each time — so it prints 0, 2, 4, and so on.
public class EvenNumbers {
public static void main(String[] args) {
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
A for
loop is often used in Java to go through each item in an array. Arrays store multiple values, and using a loop helps you access each value one by one.
In this example, we have an array of fruits. The loop starts at index 0
(the first item) and runs until it reaches the end of the array. At each step, it prints the current fruit using System.out.println()
.
public class ArrayLoopExample {
public static void main(String[] args) {
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 usually has three parts: initialization, condition, and update. But if you leave all three parts empty, like for (;;)
, there’s no condition to stop the loop. This creates an infinite loop that keeps running forever—unless you break it manually or stop the program.
Infinite loops are sometimes used for programs that need to keep running, like servers or background tasks. But be careful, because if you don't include a way to exit the loop, it can cause your program to hang.
public class InfiniteLoopExample {
public static void main(String[] args) {
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
The break
statement is used to immediately stop a loop when a certain condition is met. It is helpful when you want to exit early and skip the rest of the loop.
In this example, the loop starts from 1 and goes up to 10. But when i
becomes 6, the break
statement stops the loop, so numbers from 6 to 10 are not printed.
public class BreakExample {
public static void main(String[] args) {
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
The continue
statement in Java is used inside loops to skip the current iteration and move to the next one. It tells the loop to stop executing the remaining code for the current cycle and jump directly to the next cycle.
In this example, the loop prints numbers from 1 to 5. However, when i
is equal to 3, the continue
statement is triggered, so System.out.println(i)
is skipped for that iteration.
public class ContinueExample {
public static void main(String[] args) {
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);
}