Loops and Conditions


The C language contains two main looping constructs:

A for loop could be constructed as follows:
 
        for ( i = 0; i < 100; i++) {
                /* Block of code which is to be repeated */
            :
            :
        }
 
This translates to the following:  Using i as a counter, start at zero, count up to 100 and increment i each pass
through the loop.

A while loop is constructed as follows:
 

    while ( some condtion is true ) {
        /* do something repeatedly */
    }

The brackets after the while statement ( note these things: {} are called braces) contain some conditional statement,
e.g
        while (i < 100) {
            :
            :
            i++;
        }
C treats conditional tests in a slightly unusual way.  Essentially something is true in C if it is not zero.  So the following
will set up an infinitely repeating loop.

     while ( 1 ) {
            :
            :
        }

Conditions are most commonly tested in C using the if statement as follows:

    if ( some condition ) {
        /* do something */
    }