Java For Loop: Counter-Controlled Iteration & Nested Patterns

The for loop is a counter-controlled iteration statement widely favored when the exact number of execution repetitions is known prior to loop entry.

Syntax and Execution Flow

// For Loop Syntax:
for (initialization; boolean_condition; update_step) {
    // Loop body statements
}

Step-by-Step Execution Lifecycle:

  1. Step 1 (Initialization): Executes exactly once upon loop entry (e.g. int i = 1).
  2. Step 2 (Condition Evaluation): Evaluates boolean_condition. If true, proceeds to body. If false, loop terminates immediately.
  3. Step 3 (Body Execution): Executes all statements enclosed within the block.
  4. Step 4 (Update Step): Increments or updates the loop variable (e.g. i++).
  5. Step 5: Jumps back to Step 2.

Nested For Loops: Star Pyramid Pattern Program

Nested for loops (a loop residing inside an outer loop) are a staple in university practical exams for generating coordinate grids and geometric shapes:

public class PyramidPattern {
    public static void main(String[] args) {
        int rows = 5;

        // Outer loop controls number of rows
        for (int i = 1; i <= rows; i++) {
            // Inner loop 1: Prints leading spaces for centering
            for (int space = 1; space <= rows - i; space++) {
                System.out.print(" ");
            }
            // Inner loop 2: Prints asterisks
            for (int star = 1; star <= (2 * i - 1); star++) {
                System.out.print("*");
            }
            // Move to next line
            System.out.println();
        }
    }
}

Output:

    *
   ***
  *****
 *******
*********

University Exam Corner (BCA / B.Sc CS)

Exam Tracing Problem: "Can multiple variables be initialized and updated in a single for loop header?"

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

Answer: Yes! Java permits multiple variables of the same type separated by commas in the initialization section, and multiple update statements separated by commas in the step section.

Next Step

Now let us explore the cleaner Enhanced For-Each Loop for traversing arrays and collections. Proceed to: Java For-Each Loop →

Frequently Asked Questions (FAQ)

Q1. What are the 3 component expressions in a Java for loop header?
The header contains: 1. Initialization (runs once at start), 2. Boolean Test Condition (evaluated before every cycle), and 3. Update / Step Expression (executes at the end of every cycle).
Q2. Can any of the three expressions in a for loop be omitted?
Yes! All three expressions are optional in Java. Writing 'for (;;) { ... }' creates a valid infinite loop equivalent to 'while (true)'.
Q3. What is the scope of a variable declared inside the for loop initialization header?
A variable declared inside the for header (e.g. for (int i = 0; ...)) has block scope restricted exclusively to the for loop. It does not exist on the call stack outside the loop body.

Explore Related Java Tutorials

Accelerate your Java mastery by studying these tightly connected core tutorials:

Enhanced For-Each Loop →

Cleaner iteration over arrays and collections.

Break & Continue →

Interrupting loop cycles.

Java Arrays →

Iterating array elements.

Practice & Dry Runs →

Nested loop pattern challenges.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!