DEV Community

Cover image for Loops in Java
ABISHEK M
ABISHEK M

Posted on

Loops in Java

1. What is a Loop?

A loop is a control-flow statement used to execute a block of code repeatedly as long as a given condition is true.

For example, if we want to print numbers from 1 to 100, instead of writing 100 print statements, we can use a loop.

Types of Loops in Java

Java provides three main types of loops:

  1. For Loop
  2. While Loop
  3. Do-While Loop

2. For Loop

A for loop is used when we know how many times we want to execute a block of code.

Syntax

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

A for loop contains three important parts:

1. Initialization

  • Initializes the loop variable.
  • Executes only once at the beginning.
  • Example: int i = 1;

2. Condition

  • Checks whether the loop should continue.
  • If true, the loop body executes.
  • If false, the loop stops.

3. Update

  • Increases or decreases the loop variable.
  • Examples: i++, i--, i += 2.

Example

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

Output:

1
2
3
4
5
Enter fullscreen mode Exit fullscreen mode

Execution Flow

  1. Initialization executes once.
  2. Condition is checked.
  3. If the condition is true, the loop body executes.
  4. Update executes.
  5. The condition is checked again.
  6. The loop stops when the condition becomes false.

Important: Initialization executes only once, while the condition and update are checked repeatedly.


3. While Loop

A while loop executes a block of code repeatedly as long as the given condition is true.

It is useful when we don't know exactly how many times the loop needs to execute.

Syntax

initialization;

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

Example

int i = 1;

while (i <= 5) {
    System.out.println(i);
    i++;
}
Enter fullscreen mode Exit fullscreen mode

Output:

1
2
3
4
5
Enter fullscreen mode Exit fullscreen mode

Execution Flow

  1. Initialize the variable.
  2. Check the condition.
  3. If true, execute the loop body.
  4. Update the variable.
  5. Repeat until the condition becomes false.

Important: A while loop checks the condition before executing the code. If the condition is false initially, the loop executes zero times.


4. Do-While Loop

A do-while loop executes the code first and checks the condition afterward.

It executes at least once, even if the condition is false.

Syntax

initialization;

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

Note: A semicolon (;) is required after the while condition.

Example

int i = 1;

do {
    System.out.println(i);
    i++;
} while (i <= 5);
Enter fullscreen mode Exit fullscreen mode

Output:

1
2
3
4
5
Enter fullscreen mode Exit fullscreen mode

Example: Condition is False Initially

int i = 10;

do {
    System.out.println(i);
} while (i <= 5);
Enter fullscreen mode Exit fullscreen mode

Output:

10
Enter fullscreen mode Exit fullscreen mode

Even though the condition is false, the code executes once because the condition is checked after the do block.


5. Difference Between For, While and Do-While Loops

For Loop While Loop Do-While Loop
Used when the number of iterations is known. Used when the number of iterations is not known in advance. Used when the code must execute at least once.
Condition is checked first. Condition is checked first. Condition is checked after execution.
May execute zero times. May execute zero times. Executes at least once.
Initialization, condition and update are usually in one line. Initialization and update are written separately. Initialization and update are written separately.

6. Nested For Loop

A nested loop means placing one loop inside another loop.

  • Outer loop controls the rows.
  • Inner loop controls the columns.
  • The inner loop completes all its iterations for every iteration of the outer loop.

Example

for (int i = 1; i <= 3; i++) {

    for (int j = 1; j <= 2; j++) {
        System.out.println("i = " + i + ", j = " + j);
    }

}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

The outer loop executes 3 times, and the inner loop executes 2 times for each outer iteration.

Total executions = 3 × 2 = 6.


7. Infinite Loop

An infinite loop is a loop that continues executing because its stopping condition is never reached.

Example

int i = 1;

while (i <= 5) {
    System.out.println(i);
}
Enter fullscreen mode Exit fullscreen mode

Here, we forgot to update the value of i.

The value remains 1, so the condition is always true.

Output:

1
1
1
1
...
Enter fullscreen mode Exit fullscreen mode

We can also create an intentional infinite loop:

while (true) {
    System.out.println("Hello Java");
}
Enter fullscreen mode Exit fullscreen mode

8. Important Loop Concepts

1. What is an Iteration?

An iteration means one complete execution of a loop body.

For example:

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

This loop has 3 iterations.

2. What happens if the condition is false initially?

In for and while loops, the body does not execute.

But in a do-while loop, the body executes once before checking the condition.

3. Can we write a loop without initialization?

Yes. Initialization can be done before the loop.

int i = 1;

for (; i <= 3; i++) {
    System.out.println(i);
}
Enter fullscreen mode Exit fullscreen mode

4. Can we write a loop without an update statement?

Yes, but we must update the variable somewhere inside the loop if the condition depends on it.

int i = 1;

for (; i <= 3;) {
    System.out.println(i);
    i++;
}
Enter fullscreen mode Exit fullscreen mode

5. Can we use one loop inside another loop?

Yes. This is called a nested loop.


9. Important Points to Remember

  1. Loops are used to execute code repeatedly.
  2. Java provides three main loops: for, while, and do-while.
  3. A for loop is commonly used when the number of iterations is known.
  4. A while loop checks the condition before executing its body.
  5. A do-while loop executes its body at least once.
  6. Initialization happens before the first condition check.
  7. Update changes the loop variable.
  8. An iteration means one complete execution of the loop body.
  9. A nested loop is a loop inside another loop.
  10. Incorrect conditions or missing updates can cause infinite loops.

Conclusion

Loops are an important part of Java programming. They help us execute repetitive tasks without writing the same code multiple times.

Understanding loops is useful when learning arrays, strings, collections, and other advanced Java concepts.

Top comments (0)