Java Looping Statements – Complete Guide with For, While, and Do-While Loops
Learn all types of loops in Java, including for, while, do-while, nested loops, and best practices with examples for beginners and advanced usage.
Looping Statements in Java – Complete Detailed Tutorial
Loops in Java allow you to execute a block of code repeatedly as long as a condition is true.
They help in reducing code repetition and performing iterative tasks efficiently.
1. Types of Loops in Java
- for loop
- while loop
- do-while loop
- enhanced for loop (for-each)
2. for Loop
- Executes a block a specific number of times
- Syntax:
for(initialization; condition; increment/decrement) {
// statements
}
Example – Print numbers 1 to 5:
for(int i = 1; i <= 5; i++) {
System.out.println(i);
}
Output:
1
2
3
4
5
Nested for Loop Example
- Used to iterate inside another loop
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= 2; j++) {
System.out.println("i = " + i + ", j = " + j);
}
}
Output:
i = 1, j = 1
i = 1, j = 2
i = 2, j = 1
i = 2, j = 2
i = 3, j = 1
i = 3, j = 2
3. while Loop
- Repeats block while condition is true
- Syntax:
while(condition) {
// statements
}
Example – Print numbers 1 to 5:
int i = 1;
while(i <= 5) {
System.out.println(i);
i++;
}
Output:
1
2
3
4
5
4. do-while Loop
- Executes block at least once, then checks condition
- Syntax:
do {
// statements
} while(condition);
Example – Print numbers 1 to 5:
int i = 1;
do {
System.out.println(i);
i++;
} while(i <= 5);
Output:
1
2
3
4
5
5. Enhanced for Loop (for-each)
- Simplifies iteration over arrays or collections
- Syntax:
for(type var : array) {
// statements
}
Example – Array iteration:
int[] numbers = {10, 20, 30, 40};
for(int num : numbers) {
System.out.println(num);
}
Output:
10
20
30
40
6. Infinite Loops
- Loop that never ends
- Can be intentional or accidental
Example – Infinite while loop:
while(true) {
System.out.println("Infinite loop");
}
- Use break to exit an infinite loop
int i = 1;
while(true) {
if(i > 5) break;
System.out.println(i);
i++;
}
7. Loop Control Statements
- break – Exits the loop immediately
- continue – Skips current iteration and moves to next
Example – break and continue:
for(int i = 1; i <= 5; i++) {
if(i == 3) continue; // skip 3
if(i == 5) break; // exit at 5
System.out.println(i);
}
Output:
1
2
4
8. Example Program – Looping Statements
public class LoopDemo {
public static void main(String[] args) {
// for loop
for(int i = 1; i <= 5; i++) {
System.out.println("For loop: " + i);
}
// while loop
int j = 1;
while(j <= 5) {
System.out.println("While loop: " + j);
j++;
}
// do-while loop
int k = 1;
do {
System.out.println("Do-While loop: " + k);
k++;
} while(k <= 5);
// enhanced for loop
int[] arr = {10, 20, 30};
for(int num : arr) {
System.out.println("Array element: " + num);
}
}
}
Sample Output:
For loop: 1
For loop: 2
For loop: 3
For loop: 4
For loop: 5
While loop: 1
While loop: 2
While loop: 3
While loop: 4
While loop: 5
Do-While loop: 1
Do-While loop: 2
Do-While loop: 3
Do-While loop: 4
Do-While loop: 5
Array element: 10
Array element: 20
Array element: 30
9. Summary
- Loops allow repetition of code
- Types: for, while, do-while, enhanced for
- Use nested loops for multidimensional iteration
- break and continue control loop flow
- Choose loop based on condition check requirement