Java Loops

while Loop

A while loop is used to repeat a block of code as long as a given condition is True. It’s useful when the number of repetitions isn’t known ahead of time.

The loop checks the condition before each iteration. If the condition becomes False, the loop stops.

Basic Structure

while (condition) {
      // code block
}
Loading...
Output:

for Loop

The for loop in Java is a control structure used to execute a block of code a known number of times. It is ideal when the number of iterations is fixed or easily defined in advance.

The loop consists of three parts:

  • Initialization: declares and sets the starting value (e.g., int i = 0)
  • Condition: evaluated before each iteration; loop runs as long as this is true (e.g., i < 5)
  • Update: changes the loop variable each time (e.g., i++)

Basic Structure

for (init; condition; update) {
    // code block
}

Here's a basic example that prints numbers 0 through 4:

Loading...
Output:

do-while Loop

The do-while loop is a control structure that guarantees the loop body will run at least once, because the condition is checked after the code block executes.

This is useful when the loop body must execute at least once regardless of the condition, such as in menu systems, user input prompts, or retry attempts.

Basic Structure:

do {
    // code block
} while (condition);
Loading...
Output:

In this example, the numbers 0, 1, and 2 are printed. Even if i started at 5, the loop would still run once before stopping.

do-while vs while

Both while and do-while loops are used to repeat a block of code based on a condition. However, the key difference is when the condition is evaluated.

  • while loop: Checks the condition before the first iteration. If the condition is false initially, the loop body may not run at all.
  • do-while loop: Executes the loop body once before checking the condition. It always runs at least once.

Example using while:

Loading...
Output:

Example using do-while:

Loading...
Output:

In these examples, the while loop doesn't print anything, while the do-while loop prints once even though the condition is false.

break and continue

The break and continue statements are used to control the flow of loops. They give you more flexibility by allowing you to either exit a loop early or skip over part of its execution.

break — Exit the Loop Early

The break statement immediately exits the current loop when a certain condition is met. No further iterations are executed.

Loading...
Output:

This code prints 0, 1, 2. When i == 3, the loop breaks and stops running.

continue — Skip the Current Iteration

The continue statement skips the current loop iteration and moves to the next one. Code after the continue in the same iteration will not run.

Loading...
Output:

This code prints 0, 1, 2, 4. When i == 3, that iteration is skipped.

Nesting & Best Practices

You can place one loop inside another (nested loop), but keep conditions simple to avoid confusion or infinite loops.

Loading...
Output:

Practice Question

What will this print?

A. 01234
B. 0124
C. 1234
Loading...
Output:

Hint: The loop skips printing when i == 3.

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic