4.2 for loops
Enduring Understanding
Programmers incorporate iteration and selection into code as a way of providing instructions for the computer to process each of the many possible input values.
Learning Objective
Represent iterative processes using a for loop.
Essential Knowledge
There are three parts in a for loop header: the initialization, the Boolean expression, and the increment. The increment statement can also be a decrement statement.
In a for loop, the initialization statement is only executed once before the first Boolean expression evaluation. The variable being initialized is referred to as a loop control variable.
In each iteration of a for loop, the increment statement is executed after the entire loop body is executed and before the Boolean expression is evaluated again.
A for loop can be rewritten into an equivalent while loop and vice versa.
“Off by one” errors occur when the iteration statement loops one time too many or one time too few.
Last updated
Was this helpful?