Loop Initialization, Condition, and Update
You'll learn the three essential parts that make a for loop work: where it starts, when it stops, and how it moves forward.
The Three Parts of a For Loop
A for loop is like a recipe with three clear instructions given upfront. Every for loop has three distinct parts that work together:
- Initialization – Where you start counting
- Condition – When to stop looping
- Update – How to move to the next step
Think of it like running laps around a track: you start at lap 1 (initialization), you keep running until you complete lap 10 (condition), and after each lap you add 1 to your count (update).
How They Work Together
In a typical for loop, these three parts appear in a specific format:
for (initialization; condition; update) {
// code to repeat
}
Initialization runs once at the very beginning. It sets up your loop control variable:
i = 0
Condition is checked before each iteration. If it's true, the loop continues; if false, the loop stops:
i < 5
Update runs after each iteration's code block executes. It modifies the loop control variable:
i = i + 1
Together, they create a compact way to repeat code a specific number of times. The initialization happens once, then the loop repeatedly checks the condition, runs the code block, and performs the update—until the condition becomes false.
Key Takeaway: Every for loop has three core components: initialization (start), condition (when to stop), and update (how to progress)—making loops predictable and controlled.