Answer:
Make a comparison in terms of their functions of the for loop, while loop and do loop
For
|
While
|
Do while
|
(i) For is an entry
controlled loop statement.
|
(i) The while is an
entry controlled loop statement.
|
(i) The do while is an
exit controlled loop statement.
|
(ii) The basic formate
of the for statement is
for (initialization,
test-condition, increment);
{
body of the loop
}
|
(ii) The basci format
of the while statement is while (test-condition)
{
body of the loop
};
|
(ii) The basic formate
of the do-while statement is
do {
body of the loop
}
while
(test-condition);
|
(iii) First
initialization then test condition and then increment the control variable.
|
(iii) Test condition
is evaluated and if the condition is true, then body of the loop is executed.
|
(iii) To evaluate the
body of the loop first and end the loop the test condition in the while
statement is evaluated.
|
(iv) Example:
for(x=0; x<=9; x++)
{
printf(“%d”,x);
}
printf(“\n”);
|
(iv) example:
sum=0;
n=1;
while (n<= 10)
{
sum = sum+nxn
n=n+1;
}
printf (“sum=%d”,sum);
|
(iv) Example:
do
{
printf(Enter a
number);
number = getnum();
}
while(number>0);
|