Lecture Note: 8
for loop
In a program, for loop is generally used when number of iteration are known in
advance. The body of the loop can be single statement or multiple statements. Its
syntax for writing is:
Syntax:-
36 *Under revision
for(exp1;exp2;exp3)
{
Statement;
}
Or
for(initialized counter; test counter; update counter)
{
Statement;
}
Here exp1 is an initialization expression, exp2 is test expression or condition and
exp3 is an update expression. Expression 1 is executed only once when loop
started and used to initialize the loop variables. Condition expression generally
uses relational and logical operators. And updation part executed only when after
body of the loop is executed.
Example:-
void main()
{
int i;
for(i=1;i<10;i++)
{
37 *Under revision
Printf(“ %d ”, i);
}
}
Output:-1 2 3 4 5 6 7 8 9