UMBC CS 201, Fall 98
UMBC CMSC 201 & 201H Fall '98 CSEE | 201 | 201 F'98 | lectures | news | help

Loop Conversion

Any for loop can be converted into an equivalent while loop.

For example, the for loop

for (i = 0; i < 10; i++) 
{ 
    printf("The value of i is %d\n",i);
}

is equivalent to the while loop

i = 0;
while (i < 10) 
{    
    printf("The value of i is %d\n",i);
    i++;
}

So, which should I use?

Any for loop?

Let <LCV> stands for "loop control variable":

for (<initialize LCV>; <continuation_test>; 
     <update LCV>) 
{
    <body of loop>
}

is equivalent to

<initialize LCV>
while (<continuation_test>) 
{
    <body of loop>
    <update LCV>
}


CSEE | 201 | 201 F'98 | lectures | news | help

Saturday, 12-Sep-1998 16:49:04 EDT