Broad Network


Iteration Statements in the C Computer Language

Part 14 of Complete C Course

Foreword: Iteration Statements in the C Computer Language

By: Chrysanthus Date Published: 3 Jun 2024

Introduction

Iteration statements can also be called, loop statements. In programming, iterating means repeating some part of the code, to obtain different results. This involves looping.

A loop is a statement whose body can be executed again and again. The body of the loop has a number of statements. A loop needs an initial state or initial statement, from which the loop will execute the first time, before repeating. The initial state or initial statement is called the clause for the loop. Repeating, means all the statements in the body of the loop are re-executed, in order, again and again. In order for the loop to repeat after the first pass or any pass, there has to be a statement that will cause it to repeat. In order for a loop to stop repeating, there has to be a condition that will cause the loop not to repeat.

Do-while Loop Syntax
The syntax for the do-while loop statement is:

    //initial statement (clause), here
    do {
        //statements
        //cause for next iteration
    } while (condition);

This construct should be read as follows: Considering the initial statement, do all the statements in the loop, while the condition allows it. The initial statement ends with a semicolon. The do (long) statement itself, also ends with a semicolon. Note that “while” here, is a reserved word.

There are three main loops in C: the do-while loop, the while-loop and the for-loop. This section of the chapter, explains the do-while loop, the while-loop and the for-loop.

do-while Loop Example
Using the above syntax, an example of a do-while loop coding, is in the following program:

    #include <stdio.h>

    int main(int argc, char *argv[])
    {
        int myInt = 0;  //clause
        do {
            printf("%i ", myInt);
            myInt++;
        } while (myInt < 5);

        printf("\n");
        
        return 0;
    }

The output is:

    0 1 2 3 4

The complete construct begins with “int myInt = 0;” and ends at “while (myInt < 5);”. There are two simple statements in the braces. The first statement in the braces, prints out the value of the integer, myInt. The second statement increments myInt, by adding 1 to it. The condition is “while (myInt < 5)”. So, while myInt is less than 5, the block (compound statement), also called the body, is re-executed. Note: The statement before the return statement, prints a new line at the output. The printf statement in the loop body of the code, does not print a new line.

This loop has just one main simple statement, which is to print the value of myInt. The second simple statement is to cause the next iteration. The curly brackets can have more than one main simple statement. The following do-while loop has two main simple statements. The first one adds 2 to myInt, and the second one prints the result of the addition:

    #include <stdio.h>

    int main(int argc, char *argv[])
    {
        int myInt = 0;  //clause
        do {
            myInt = myInt + 2;
            printf("%i ", myInt);
            myInt++;
        } while (myInt < 13);

        printf("\n");
        
        return 0;
    }

The output is:

    2 5 8 11 14

This output needs explanation. First of all, note that the while-condition has been changed to “while (myInt < 13)”.

When myInt is 0, 2 is added to it, and myInt becomes 2. Two is printed. The increment adds 1 to myInt and it becomes 3, at the beginning of the next pass. In the next iteration (pass) myInt is 3. Two is added to it again and it becomes 5. Five is printed. The increment adds 1 to myInt and it becomes 6. In the next iteration myInt is 6. 2 is added to it again and it becomes 8. The increment adds 1 to myInt and it becomes 9. In the next iteration myInt is 9. 2 is added to it again and it becomes 11. The increment adds 1 to myInt and it becomes 12. In the next iteration myInt is 12. 2 is added to it again and it becomes 14. Fourteen is printed. The increment adds 1 to myint and it becomes 15.

After each iteration, the while condition is checked. At this point, that the while condition is checked, the myInt is 15, above 13, after 14 has been printed. The condition results in false, and the repetition of the block, stops. The iteration that printed 14, began with myInt = 12, which is less than 13. By the time all the statements of the loop body had been executed, the  myInt was 15, greater than 13, so the while-loop condition failed, and the loop’s body (block) could not be entered again.

while-loop

The syntax for the while-loop is:

    //initial statement here
    while (condition) {
        //statements
        //cause for next iteration
    }

The main difference between the do-while loop and the while-loop is, that for the while-loop, the condition is checked first, before the block is executed. With the do-while loop, the block is executed first, before the condition is checked. Note that the while-loop statement, does not end with a semicolon.

The following program repeats the first program above, but with a while-loop:

    #include <stdio.h>

    int main(int argc, char *argv[])
    {
        int myInt = 0;  //clause
        while (myInt < 5) {
            printf("%i ", myInt);
            myInt++;
        }

        printf("\n");
        
        return 0;
    }

The output is the same as for the first program above, that is:

    0 1 2 3 4

The following program repeats the second program above, but with a while-loop:

    #include <stdio.h>

    int main(int argc, char *argv[])
    {
        int myInt = 0;  //clause
        while (myInt < 13) {
            myInt = myInt + 2;
            printf("%i ", myInt);
            myInt++;
        }

        printf("\n");
        
        return 0;
    }

The output is the same as for the second program above, that is:

    2 5 8 11 14

for-loop

The common syntax for the for-loop is:

    for (clause; while-condition; increment) {
        //statements
    }

The for-loop is a concise, though more restrictive form, of the do-while loop. The for-loop has a pair of parentheses and a block. Here, the initial statement (clause) has been removed from outside and above the construct, and put into the parentheses. The while-condition, is the second statement in the parentheses. The cause-for-next-iteration (increment) is the last (third) statement in the parentheses.

The following program repeats the first program above, but with a for-loop:

    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
        for (int myInt = 0; myInt < 5; myInt++) {
            printf("%i ", myInt);
        }
        printf("\n");

        return 0;
    }

The output is the same as for the first program above, that is:

    0 1 2 3 4

There is no semicolon after the increment statement (last statement) in the parentheses. Note that the for-loop long statement, does not also end with a semicolon. That is, the block of the for-loop does not end with a semicolon.



Related Links

More Related Links

Cousins

BACK NEXT

Comments