Can anyone tell me the difference bet for loop and while loop
execution?
____________________
for (i=0 ; i<10 ; i++)
; /* Do nothing*/
print i;
___________________
i=0;
while (i<10) {
i++;
}
print i;
In C89, the only relevant differences between
expression_0;
while (condition) {
expression_1;
expression_2;
expression_3;
expression_n;
}
and:
for (expression_0; condition; expression_n) {
expression_1;
expression_2;
expression_3;
}
are:
1. In the first case, condition cannot be empty, in the second case it can
and if it is it is considered to be 1 (always true).
2. If a continue statement is used in the for loop, expression_n is evalued
before checking condition and resuming the loop from expression_1 if it is
true.
In C99 expression_0 can be a variable declaration, and the variable so
declared is only visible within the loop body. There are also other
differences, but unless you want to do pointless things such as re-defining
an enum in the loop body, they won't show up.
(Personally, I consider while (condition) a shorthand for for (;condition
rather than vice versa...)