← Back to Home

for Loop

The for loop in Java is a control flow statement used to execute a block of code repeatedly for a fixed number of iterations. It is most commonly used when the number of iterations is known in advance. This is a fundamental concept for logic building, algorithms, and interviews.

What Is a for Loop?

  • Repeats a block of code
  • Combines initialization, condition, and update in one line
  • Best suited for count-controlled loops

Basic Syntax

for (initialization; condition; update) {
    // code to execute
}
          

Breakdown of for Loop Components

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

1. Initialization

  • Executed once at the start
  • Used to initialize loop variable
int i = 1;
          

2. Condition

  • Evaluated before every iteration
  • Loop continues while condition is true
i <= 5
          

3. Update (Increment / Decrement)

  • Executed after each iteration
  • Updates loop variable
i++
          

Execution Flow of for Loop

  1. Initialization
  2. Condition check
  3. Loop body execution
  4. Update
  5. Repeat steps 2–4 until condition becomes false

Simple Example

for (int i = 1; i <= 3; i++) {
    System.out.println("Java");
}
          

Output:

Java
Java
Java
          

for Loop with Decrement

for (int i = 5; i >= 1; i--) {
    System.out.println(i);
}
          

Multiple Initialization & Update

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

Infinite for Loop

for (;;) {
    System.out.println("Infinite loop");
}
          

Why it matters: All three parts are optional, but semicolons are mandatory.

for Loop with break and continue

Using break

for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        break;
    }
    System.out.println(i);
}
          

Using continue

for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        continue;
    }
    System.out.println(i);
}
          

Nested for Loop

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

Use case: Matrix operations, patterns, tables.

Common Use Cases

  • Iterating fixed ranges
  • Processing arrays using index
  • Generating patterns
  • Looping counters

Common Beginner Mistakes

  • Off-by-one errors (< vs <=)
  • Infinite loops due to wrong condition
  • Modifying loop variable inside loop body
  • Overcomplicating loop conditions

Interview-Ready Answers

Short Answer

The for loop is used to execute a block of code repeatedly when the number of iterations is known.

Detailed Answer

In Java, the for loop consists of initialization, condition, and update expressions. It evaluates the condition before each iteration and executes the loop body until the condition becomes false, making it ideal for count-controlled iterations.

Key Takeaway

The for loop provides compact, readable, and controlled iteration. Mastering its flow and edge cases is essential for writing efficient Java logic.