Course contentsShow
← Java
Lesson 102 of 2,8703. Operators and ExpressionsFree lesson

Increment/Decrement in Loop Constructs

See how these operators are commonly used in for loops and while loops for iteration control.

Increment/Decrement in Loop Constructs

What you'll learn: How increment and decrement operators naturally fit into loops to control iteration.

Why These Operators Shine in Loops

You've learned the ++ and -- operators, and now you'll see where they're used most: loop constructs. Loops repeat actions, and these operators provide a clean, concise way to update your loop counter each time around.

The Classic for Loop Pattern

The for loop has three parts: initialization, condition, and update. The increment/decrement operators are perfect for that update step:

for (int i = 0; i < 5; i++) {
    System.out.println(i);  // prints 0, 1, 2, 3, 4
}

Here, i++ increments the counter after each iteration. It's so common that this pattern has become the standard way to count in Java.

You can also count backwards:

for (int i = 10; i > 0; i--) {
    System.out.println(i);  // prints 10, 9, 8, ..., 1
}

While Loops Need Updates Too

With while loops, you manually control the update, but the increment/decrement operators still make it clean:

int count = 0;
while (count < 3) {
    System.out.println(count);
    count++;  // don't forget this, or you'll loop forever!
}

Why This Matters

Think of these operators as a "next step" button. In a for loop, i++ says "move to the next number." Without increment/decrement, you'd write i = i + 1, which is wordier and less idiomatic. The compact syntax keeps your loop headers readable and your intent clear.

Key Takeaway: Increment (++) and decrement (--) operators are the standard, idiomatic way to update loop counters in Java, keeping your iteration logic clean and concise.