DEV Community

Sudhakar V
Sudhakar V

Posted on

P 3- Looping

Looping in Java (In-Depth Guide)

Looping is a fundamental concept in Java (and programming in general) that allows you to execute a block of code repeatedly based on a condition. Java provides several looping mechanisms, each suited for different scenarios.


1. for Loop

The for loop is used when the number of iterations is known beforehand.

Syntax

for (initialization; condition; update) {
    // Code to execute
}
Enter fullscreen mode Exit fullscreen mode

Example

for (int i = 1; i <= 5; i++) {
    System.out.println("Iteration: " + i);
}
Enter fullscreen mode Exit fullscreen mode

Output:

Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Enter fullscreen mode Exit fullscreen mode

Variations

a) Enhanced for Loop (for-each)

Used for iterating over arrays and collections.

int[] numbers = {10, 20, 30, 40};
for (int num : numbers) {
    System.out.println(num);
}
Enter fullscreen mode Exit fullscreen mode

Output:

10
20
30
40
Enter fullscreen mode Exit fullscreen mode

b) Infinite for Loop

for (;;) {
    System.out.println("This runs forever!");
}
Enter fullscreen mode Exit fullscreen mode

2. while Loop

The while loop executes as long as the condition is true. It is used when the number of iterations is not known in advance.

Syntax

while (condition) {
    // Code to execute
}
Enter fullscreen mode Exit fullscreen mode

Example

int count = 1;
while (count <= 5) {
    System.out.println("Count: " + count);
    count++;
}
Enter fullscreen mode Exit fullscreen mode

Output:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Enter fullscreen mode Exit fullscreen mode

Infinite while Loop

while (true) {
    System.out.println("Infinite loop!");
}
Enter fullscreen mode Exit fullscreen mode

3. do-while Loop

Similar to while, but guarantees at least one execution before checking the condition.

Syntax

do {
    // Code to execute
} while (condition);
Enter fullscreen mode Exit fullscreen mode

Example

int x = 5;
do {
    System.out.println("x = " + x);
    x--;
} while (x > 0);
Enter fullscreen mode Exit fullscreen mode

Output:

x = 5
x = 4
x = 3
x = 2
x = 1
Enter fullscreen mode Exit fullscreen mode

4. Nested Loops

A loop inside another loop is called a nested loop.

Example (Printing a Pyramid)

for (int i = 1; i <= 5; i++) {
    for (int j = 1; j <= i; j++) {
        System.out.print("* ");
    }
    System.out.println();
}
Enter fullscreen mode Exit fullscreen mode

Output:

* 
* * 
* * * 
* * * * 
* * * * * 
Enter fullscreen mode Exit fullscreen mode

5. Loop Control Statements

Java provides keywords to alter loop execution:

Keyword Description
break Exits the loop immediately
continue Skips the current iteration and moves to the next
return Exits the entire method

Example: break

for (int i = 1; i <= 10; i++) {
    if (i == 6) {
        break; // Exit loop when i=6
    }
    System.out.println(i);
}
Enter fullscreen mode Exit fullscreen mode

Output:

1
2
3
4
5
Enter fullscreen mode Exit fullscreen mode

Example: continue

for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        continue; // Skip iteration when i=3
    }
    System.out.println(i);
}
Enter fullscreen mode Exit fullscreen mode

Output:

1
2
4
5
Enter fullscreen mode Exit fullscreen mode

6. Best Practices for Loops

  1. Avoid infinite loops unless intentional (e.g., in servers).
  2. Use for when the number of iterations is known.
  3. Use while when the condition is dynamic.
  4. Prefer for-each for collections (cleaner syntax).
  5. Keep loop bodies small for readability.
  6. Use break and continue sparingly (can make code harder to debug).

Conclusion

  • for loop → Best for fixed iterations.
  • while loop → Best for unknown iterations.
  • do-while loop → Ensures at least one execution.
  • Nested loops → Useful for multi-dimensional data.
  • break & continue → Control loop flow.

Mastering loops is essential for efficient iteration in Java programs. 🚀

Top comments (0)