Introduction
The for loop is used only when the number of iterations is predetermined, for example, 10 iterations or 100 iterations.
Program/Example
The general format for the for loop is
for (initializing; continuation condition; update) simple or compound statement For example, for (i = 0; i < 5; i++) { printf("value of i"); }
Explanation
-
The for loop has four components; three are given in parentheses and one in the loop body.
-
All three components between the parentheses are optional.
-
The initialization part is executed first and only once.
-
The condition is evaluated before the loop body is executed. If the condition is false then the loop body is not executed.
-
The update part is executed only after the loop body is executed and is generally used for updating the loop variables.
-
The absence of a condition is taken as true.
-
It is the responsibility of the programmer to make sure the condition is false after certain iterations.
No comments:
Post a Comment