CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

The equivalence of bounded and unbounded loops

We should now be able to see that the for, while, and do ... while control flow statements are each closely related.

To fully understand this, however, we need to accept (for now), that the three "pieces" of the for construct, are not always initialization, condition, modification.

More generally, the three pieces may be C expressions - for the moment we'll consider these as C statements which, if they produce a value, the value is often ignored.

The following loops are actually equivalent:


for( expression1 ; expression2 ; expression3 ) {
    statement1;
    ....
}



expression1;                                         
while(expression2) {
    statement1;
    ....
    expression3;
}

In both cases, we're expecting expression2 to produce a Boolean value, either true or false, as we need that truth value to determine if our loops should "keep going".

You should think about these carefully, perhaps perform some experiments, to determine where control flow really goes when we introduce break and continue statements.

 


CITS2002 Systems Programming, Lecture 2, p16, 25th July 2023.