Newtoprogram Newtoprogram - 1 month ago 7
C Question

For versus while versus do loop statements

Is there a difference between

for
and
while
statements? Is it just syntax?

#include <stdio.h>

void main() {
int cent_temp = 0;
int fah_temp;
while (cent_temp <= 20) {
fah_temp = (9 * cent_temp) / 5 + 32;
printf("%d degrees C = %d degrees F\n", cent_temp, fah_temp);
cent_temp++;
}
}


This means to me....

While the value of
cent_temp
is less than
20
then calculate
fah_temp
. Then increase the value of
cent_temp
by
1
and check it is still less than
20
; then go round the loop again.

Regarding the syntax:

printf("%d degrees C = %d degrees F\n", cent_temp, fah_temp);


This means
%d
means print to the screen, replace with a decimal number the value of
cent_temp
and
%d
means replace with a decimal number the value of
fah_temp
.

#include <stdio.h>

void main() {
int cent_temp;
int fah_temp;
for (cent_temp = 0; cent_temp <= 20; cent_temp++) {
fah_temp = (9 * cent_temp) / 5 + 32;
printf("%2d degrees C = %2d degrees F\n", cent_temp, fah_temp);
}
}


My interpretation of the above is:
for
cent_temp
=
0
repeat while
cent_temp
less than
20
and then execute
cent_temp+1
at the end. So
cent_temp
0
goes into the loop to calculate
fah_temp
and gets printed to the screen. Then
cent_temp
goes up by one then goes round the loop again. Here I've used
%2d
instead of
%d
to signify that it should have 2 spaces for a decimal number (and they line up when executed). Both codes will not execute if
cent_temp > 20
.

Similarly rearranging the statement in a
do
while
loop has a similar effect and doesn't really have an impact on the result.

Does each type of loop have a different application?

Please correct me if I wrong!

Answer

In general, you would use a for loop to iterate over a finite set of values, whereas you'd use a while or do-while loop to iterate while a specific condition or set of conditions is true. In most of C's contemporaries (Basic, Pascal, Fortran, etc.), a for loop can only iterate over a scalar index:

Fortran:

      DO 10 i=1,10
        statements
   10 CONTINUE

Pascal:

for i := 1 to 10 do
begin
  statements
end;

Both of these snippets loop exactly 10 times. The index i is initialized and updated by the loop automagically. I'd have to go back and check, but I'm pretty sure you cannot write to i in the loop body.

C actually blurred the lines between a for and while loop by adding the control expression:

for ( init-expr ; control-expr ; update-expr )
  statement

In C, a for loop can iterate over a scalar just like Fortran or Pascal:

for( i = 0; i < 10; i++ )
{
  do_something_with( i );
}

Or it can iterate over multiple scalars:

for ( i = 0, j = 0; i < 10 && j < 10; i++, j++ )
{
  do_something_with( i, j );
}

Or it can iterate over the contents of a file:

for( c = fgetc( in ); c != EOF; c = fgetc( in ) )
{
  do_something_with( c );
}

Or it can iterate over a linked list:

for( cur = head; cur != NULL; cur = cur->next )
{
  do_something_with( cur );
}

In Fortran and Pascal, those last three loops would have to be expressed as while loops (which I'm not going to do, because I've pretty much exhausted my Fortran and Pascal knowledge already).

The other big difference between a C for loop and those of Fortran or Pascal is that you can write to the loop index (i, j, c, or cur) in the loop body; it's not specially protected in any way.

A while or do-while loop is used to iterate as long as a specific condition or set of conditions is true:

while( control-expr )
  statement
do
  statement
while( control-expr );

In both a for and while loop, the condition is tested before the loop body executes; in a do-while loop, the condition is tested after the loop body executes, so a do-while loop will always execute at least once.

In C, you can use either a for loop or a while loop in many circumstances:

while ( ( c = fgetc( in ) ) != EOF )
  do_something_with( c );

for ( c = fgetc( in ); c != EOF; c = fgetc( in ) )
  do_something_with( c );

Both loops do exactly the same thing; it's just a matter of which one you think more clearly expresses your intent, or which you think would be easier for other people to understand.