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

  1. for loop
  2. while loop
  3. do-while loop
  4. enhanced for loop (for-each)

2. for Loop

  1. Executes a block a specific number of times
  2. 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

  1. 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

  1. Repeats block while condition is true
  2. 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

  1. Executes block at least once, then checks condition
  2. 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)

  1. Simplifies iteration over arrays or collections
  2. 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

  1. Loop that never ends
  2. Can be intentional or accidental

Example – Infinite while loop:


while(true) {
System.out.println("Infinite loop");
}
  1. 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

  1. break – Exits the loop immediately
  2. 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

  1. Loops allow repetition of code
  2. Types: for, while, do-while, enhanced for
  3. Use nested loops for multidimensional iteration
  4. break and continue control loop flow
  5. Choose loop based on condition check requirement