
Java is a programming language that has been widely used in the software industry for a long time, and it remains popular due to the security it offers for applications built with it.
Many programs need to perform the same task multiple times. A social media application may need to process thousands of posts, notifications, or comments. An e-commerce platform may need to display hundreds of products in a catalog. A banking application may need to process multiple transactions. Writing the same code repeatedly would be inefficient and difficult to maintain.
To solve this problem, Java provides loops. A loop allows a block of code to execute repeatedly until a specified condition becomes false.
In this chapter, you will learn how to use loops in Java to repeat tasks efficiently using while, do-while, and for loops.
A loop is a control structure that repeatedly executes a block of code as long as a condition remains true.
Without loops:
System.out.println("Hello");
System.out.println("Hello");
System.out.println("Hello");
System.out.println("Hello");
System.out.println("Hello");
With a loop:
for(int i = 1; i <= 5; i++) {
System.out.println("Hello");
}
Both programs produce the same output, but the loop version is shorter, cleaner, and easier to maintain.
A while loop executes a block of code as long as its condition remains true.
Syntax
while(condition) {
// code to repeat
}
Example
int count = 1;
while(count <= 5) {
System.out.println(count);
count++;
}
Output
1 2 3 4 5
Explanation
count starts with the value 1.count <= 5.count++ increases the value by one.A do-while loop is similar to a while loop, but the code block executes at least once before the condition is checked.
Syntax
do {
// code to repeat
}
while(condition);
Example
int count = 1;
do {
System.out.println(count);
count++;
}
while(count <= 5);
Output
1 2 3 4 5
Explanation
The code inside the loop executes first, then Java checks the condition. Because of this, a do-while loop always runs at least once.
The for loop is commonly used when the number of iterations is known in advance.
Syntax
for(initialization; condition; update) {
// code to repeat
}
Example
for(int i = 1; i <= 5; i++) {
System.out.println(i);
}
Output
1 2 3 4 5
Explanation
for(int i = 1; i <= 5; i++)
int i = 1 → initialization (runs once)i <= 5 → condition (checked before every iteration)i++ → update (runs after every iteration)The loop continues until the condition becomes false.
Example
for(int i = 2; i <= 10; i += 2) {
System.out.println(i);
}
Output
2 4 6 8 10
Explanation
i += 2 increases the value by 2 after each iteration, producing only even numbers.
A loop can be placed inside another loop.
This is known as a nested loop.
Example
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
Nested loops are commonly used when working with patterns, tables, and multidimensional arrays.
The break statement immediately terminates a loop.
Example
for(int i = 1; i <= 10; i++) {
if(i == 5) {
break;
}
System.out.println(i);
}
Output
1 2 3 4
When i becomes 5, the loop stops completely.
The continue statement skips the current iteration and moves to the next iteration.
Example
for(int i = 1; i <= 5; i++) {
if(i == 3) {
continue;
}
System.out.println(i);
}
Output
1 2 4 5
When i becomes 3, that iteration is skipped.
Example
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
for(int i = 1; i <= number; i++) {
System.out.println(i);
}
}
}
Input
5
Output
1 2 3 4 5
Explanation
System.out.println().Wrong
int i = 1;
while(i <= 5) {
System.out.println(i);
}
Problem
The value of i never changes, causing an infinite loop.
Correct
int i = 1;
while(i <= 5) {
System.out.println(i);
i++;
}
Wrong
for(int i = 1; i >= 5; i++)
The condition is false from the beginning, so the loop never executes.
Correct
for(int i = 1; i <= 5; i++)
while loops check the condition before execution.do-while loops execute at least once.for loops are commonly used when the number of iterations is known.break terminates a loop immediately.continue skips the current iteration.