Execution Cycle of a C Three-Expression For Loop
This article outlines the standard execution cycle of a
three-expression for loop in the C programming language. It
breaks down the precise sequence in which the loop initializes control
variables, evaluates the termination condition, executes the loop body,
and processes the post-iteration expression.
In C, the three-expression for loop uses the following
standard syntax:
for (expression_1; expression_2; expression_3) {
/* loop body */
}The execution cycle follows a strict, repeatable five-step sequence:
1. Initialization
(expression_1)
The initialization expression executes exactly once before any other part of the loop runs. It is typically used to declare or assign starting values to loop counter variables. Once executed, the loop immediately transitions to the condition step.
2. Condition Evaluation
(expression_2)
The controlling expression is evaluated before each potential run of the loop body. In C, scalar values evaluate to true if they are non-zero, and false if they are zero.
- If
expression_2evaluates to false (0), the cycle terminates immediately, and program control transfers to the statement following the loop body. - If
expression_2evaluates to true (non-zero), the cycle proceeds to execute the loop body. - If
expression_2is left blank, C treats it as non-zero (always true), creating an infinite loop unless broken internally.
3. Execution of the Loop Body
The block of statements contained within the loop body executes sequentially.
- If a
breakstatement is encountered, the loop immediately terminates, bypassing the remaining body and the update step. - If a
continuestatement is encountered, the remainder of the loop body is skipped, and execution jumps directly to the update step.
4. Update / Iteration
(expression_3)
After the loop body executes its final statement (or encounters a
continue), the third expression executes. This expression
is typically used to increment, decrement, or otherwise modify the
control variables initialized in the first step.
5. Cycle Repetition
Following the evaluation of expression_3, program
control returns directly to Step 2 to evaluate
expression_2 again. The initialization step
(expression_1) is never revisited. This sequence—evaluating
the condition, executing the body, and running the update
expression—repeats continuously until the condition evaluates to false
or an explicit termination command is executed.