⬅ Previous Topic
Java Ternary OperatorNext Topic ⮕
Java For Loop⬅ Previous Topic
Java Ternary OperatorNext Topic ⮕
Java For LoopImagine writing a task 100 times manually. Sounds exhausting, right? Loops in Java are designed to help you automate repetitive actions with control. In this tutorial, we’ll walk through all types of Java loops, when to use them, and how to write and understand their outputs step-by-step.
Java offers three primary looping constructs:
for
loopwhile
loopdo-while
loopLet’s explore each with real-life examples and logic breakdowns.
for
LoopThe for
loop is best when you know how many times you want to repeat a block of code. It’s concise and powerful.
public class ForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Hello Java " + i);
}
}
}
Hello Java 1
Hello Java 2
Hello Java 3
Hello Java 4
Hello Java 5
int i = 1;
→ Initializes the loop variablei <= 5;
→ Checks if the loop should continuei++
→ Increments i
after each iterationThis loop runs 5 times, printing a friendly message each time with the count.
while
LoopUse a while
loop when you want the loop to run as long as a condition remains true — but you may not know in advance how many times.
public class WhileLoopExample {
public static void main(String[] args) {
int i = 1;
while (i <= 3) {
System.out.println("Counter: " + i);
i++;
}
}
}
Counter: 1
Counter: 2
Counter: 3
The loop continues as long as i <= 3
. Notice how you must manually initialize and increment i
— this gives you more control but requires careful handling to avoid infinite loops.
do-while
LoopA do-while
loop guarantees that the block runs at least once — even if the condition is false initially.
public class DoWhileExample {
public static void main(String[] args) {
int i = 1;
do {
System.out.println("Do-While Count: " + i);
i++;
} while (i <= 2);
}
}
Do-While Count: 1
Do-While Count: 2
Unlike the while
loop, this format ensures the loop body executes at least once before checking the condition.
Loops without a termination condition can run forever — either accidentally or by design (like servers listening for requests).
// WARNING: This is an infinite loop
while (true) {
System.out.println("Running endlessly...");
}
Be cautious! Infinite loops can freeze your program if not intentionally used.
Java loops can be further controlled using break
and continue
statements.
break
for (int i = 1; i <= 5; i++) {
if (i == 3) break;
System.out.println(i);
}
1
2
continue
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
System.out.println(i);
}
1
2
4
5
break
exits the loop early; continue
skips the current iteration and moves to the next.
Loops inside loops — useful for multidimensional data or repeated patterns.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
System.out.println("i = " + i + ", j = " + j);
}
}
i = 1, j = 1
i = 1, j = 2
i = 2, j = 1
i = 2, j = 2
i = 3, j = 1
i = 3, j = 2
Loop Type | Best For |
---|---|
for | Known number of repetitions |
while | Unknown repetitions, condition-controlled |
do-while | Always execute at least once |
do-while
loop in Java compared to for
and while
?for
loop in Java is best suited for situations where the number of iterations is known ahead of time.while
loop in Java?for (int i = 1; i <= 5; i++) {
if (i == 3) break;
System.out.println(i);
}
continue
inside a for
loop allows you to skip the current iteration and proceed with the next.⬅ Previous Topic
Java Ternary OperatorNext Topic ⮕
Java For LoopYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.