Java Loops

A loop runs a block of code as long as a condition holds. Loops save time, cut down on errors, and make code easier to read.

While Loop

It repeats a block as long as its condition is true. The condition gets checked before each pass, so if it's false at the start, the body never runs.

int i = 0;
while (i < 5) {
    System.out.println(i);
    i++;
}
  • Output: 0 1 2 3 4 (one per line)

  • Important: Don't forget i++. If you never change the variable used in the condition, the loop never ends. You can use a variable other than i, as long as you increase or decrease it to match the condition.

Do/While Loop

A variant of while. It runs the body once first, then checks the condition, then repeats as long as the condition is true.

int i = 0;
do {
    System.out.println(i);
    i++;
} while (i < 5);
  • Key Difference: A do/while always runs at least once, even if the condition is false from the start. A while loop would skip the body entirely.

int i = 10;
do {
    System.out.println("i is " + i);
    i++;
} while (i < 5);
  • Output: i is 10 (printed once). This is useful when something must happen at least once, such as showing a menu or asking for user input.

For Loop

Use it when you know exactly how many times you want to loop.

for (statement 1; statement 2; statement 3) {
    // code block to be executed
}
  • Statement 1: Runs once before the code block executes.

  • Statement 2: Condition for running the code block.

  • Statement 3: Runs every time after the code block has been executed.

Examples
  • Prints 0 to 4:

  for (int i = 0; i < 5; i++) {
      System.out.println(i);
  }
  • Prints even values from 0 to 10:

  for (int i = 0; i <= 10; i = i + 2) {
      System.out.println(i);
  }
  • Sum of the numbers 1 to 5:

  int sum = 0;
  for (int i = 1; i <= 5; i++) {
      sum = sum + i;
  }
  System.out.println("Sum is " + sum);
  • Output: Sum is 15

Nested Loops

A loop placed inside another loop. The inner loop runs one full time for every single iteration of the outer loop.

// Outer loop
for (int i = 1; i <= 2; i++) {
    System.out.println("Outer: " + i); // executes 2 times
    // Inner loop
    for (int j = 1; j <= 3; j++) {
        System.out.println(" Inner: " + j); // executes 6 times (2 * 3)
    }
}
Multiplication Table (1 to 3)
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        System.out.print(i * j + " ");
    }
    System.out.println();
}
  • Output:

  1 2 3 
  2 4 6 
  3 6 9 
  • The print() inside the inner loop keeps each row on one line. The empty println() after the inner loop starts a new row.

For-each Loop

Used exclusively to loop through the elements of an array or other data structures.

for (type variableName : arrayName) {
    // code block to be executed
}
  • Example (outputs every element in the array):

  String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
  for (String i : cars) {
      System.out.println(i);
  }
Quick Comparison

Loop

Checks Condition

Best For

while

Before the body

Repeating while a condition holds

do/while

After the body

Something that must run at least once

for

Before the body

A known number of repetitions

for-each

Goes through every element

Arrays and other data structures

Exam Key Points
  • Endless Loop: Forgetting to update the loop variable in a while loop causes an infinite loop.

  • Execution Guarantee: A do/while runs at least once; a while loop may run zero times.

  • For Loop Flow: Statement 1 runs once, Statement 2 is checked each round, and Statement 3 runs after each pass.

  • Nested Counts: Total inner iterations = outer count ×\times inner count (2×3=62 \times 3 = 6).

  • For-each Scope: Restricted to arrays and data structures.