
Understanding Looping in Java with Examples

In programming, loops are powerful control structures that allow us to execute a block of code repeatedly until a specific condition is met. Instead of writing repetitive statements, loops help in reducing redundancy, making code concise, readable, and efficient.
Java, being an object-oriented language, supports multiple types of loops to handle different scenarios. In this post, we’ll explore types of loops in Java, their syntax, and practical examples.
Types of Loops in Java
Java provides three main looping constructs:
-
for loop
-
while loop
-
do-while loop
Additionally, Java also provides enhanced for loop (for-each loop), which is useful when working with arrays and collections.
1. The for
Loop
The for
loop is commonly used when the number of iterations is known. It consists of three parts:
-
Initialization: executed once at the beginning.
-
Condition: checked before each iteration.
-
Update/Increment: executed after each iteration.
Syntax:
for(initialization; condition; update) {
// code to be executed
}
Example: Printing numbers from 1 to 5
public class ForLoopExample {
public static void main(String[] args) {
for(int i = 1; i <= 5; i++) {
System.out.println("Number: " + i);
}
}
}
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
2. The while
Loop
The while
loop is used when the number of iterations is not known in advance. The loop continues as long as the condition is true.
Syntax:
while(condition) {
// code to be executed
}
Example: Printing numbers from 1 to 5
public class WhileLoopExample {
public static void main(String[] args) {
int i = 1;
while(i <= 5) {
System.out.println("Number: " + i);
i++;
}
}
}
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
3. The do-while
Loop
The do-while
loop is similar to the while
loop, but the difference is that the code executes at least once, even if the condition is false.
Syntax:
do {
// code to be executed
} while(condition);
Example: Printing numbers from 1 to 5
public class DoWhileExample {
public static void main(String[] args) {
int i = 1;
do {
System.out.println("Number: " + i);
i++;
} while(i <= 5);
}
}
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
4. Enhanced for-each
Loop
The for-each loop (enhanced for
loop) is introduced in Java 5. It is specifically used to traverse arrays or collections, making iteration simpler.
Syntax:
for(type variable : arrayOrCollection) {
// code to be executed
}
Example: Iterating through an array
public class ForEachExample {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
for(int num : numbers) {
System.out.println("Number: " + num);
}
}
}
Output:
Number: 10
Number: 20
Number: 30
Number: 40
Number: 50
Controlling Loops with break
and continue
break
Statement
Used to exit the loop immediately.
for(int i = 1; i <= 10; i++) {
if(i == 5) {
break; // stop when i is 5
}
System.out.println(i);
}
Output:
1
2
3
4
continue
Statement
Skips the current iteration and moves to the next.
for(int i = 1; i <= 5; i++) {
if(i == 3) {
continue; // skip when i is 3
}
System.out.println(i);
}
Output:
1
2
4
5
Conclusion
Looping is a fundamental concept in Java programming that helps in executing repetitive tasks efficiently.
-
Use for loop when you know the number of iterations.
-
Use while loop when the iterations depend on a condition.
-
Use do-while loop if you want the code to run at least once.
-
Use enhanced for loop to simplify array and collection traversal.
By mastering loops, you can write cleaner, more efficient, and scalable Java programs.