C Programming language-C Loop Control Structures

In some scenarios, we need to execute the certain lines of code several times and C loop execute a block of program statements to a specified number of times. 

Loops

A Loop instructs to do repetitive tasks of certain number of times. This helps to execute a certain number of lines multiple times and this process is known as looping. The statements get executed certain number of times based on the given conditions. Sometimes, the condition satisfies any number of times without stopping the looping statements, then this type of looping is called infinite statements.

Flowchart

                                                                                                                               

C Loop Control Statements

There are few statements in C Loop statements which can change the normal sequence of execution of the loop:


“break” statement

It is used to terminate the loop or switch statements.

Syntax

break;


“continue” statement

It is used to suspend the current loop execution and transfer the control to the next iteration of the loop.

Syntax

continue;


 “goto” statement

It transfers the current program execution to some other part of the program. The label name is given to identify the part of the code to be executed.

Syntax

goto labelName;

 .....

labelName:

statement;


C while loops 

The while loop statement  in allows to repeatedly run the same block of the code until the condition gets satisfied.While loop has one control condition and executes the code as long as the condition gets satisfied. There are two type of loops in C such as:       

  • Entry-controlled loop    
  • Exit-controlled loop                                                                                                                                                                                                                                       

    Entry-controlled loop

    The condition is evaluated before the body of the loop is executed, hence it is called entry controlled loop.

    Syntax

    while(condition)

    {

    . . . . .

    statement;

    . . . . .

    incrementation;

    }


    Flowchart:

    Example Program:

    #include

    void main()

    {

        int n = 5; /*total number of times the loop to be executed */

        int i = 1; /*Initialize with the numebr 1 */

        while (i <= n)

        {

            printf("C while loop statement : %d\n", i);

            i++;

        }

    }

    Output:

    C while loop statement : 1

    C while loop statement : 2

    C while loop statement : 3

    C while loop statement : 4

    C while loop statement : 5

    Exit-controlled loops

    In C, do while loops are similar to while loops in which code block executes at least once before the condition gets evaluated. The block of statement is executed till the condition gets failed.

     Syntax

    do{

    . . . . .

    statement;

    . . . . .

    incrementation;


    } while(condition);


    Flowchart

    Example Program

    #include

     void main()

    {

        int n = 5; /*total number of times the loop to be executed */

        int i = 1; /*Initialize with the numebr 1 */ 

        do

        {

            printf("C do while loop statement : %d\n", i);

            i++;

        }while (i <= n);

    }

    Output

    C do while loop statement : 1

    C do while loop statement : 2

    C do while loop statement : 3

    C do while loop statement : 4

    C do while loop statement : 5

    C for loops

    The for loop is similar to while loop in which it continues to execute the block of code until the condition becomes false. In for loop, the initialization, condition and increment all are defined in the same line. This for loop is also an entry-controlled loop.

    Syntax

    for (initialization; condition; increment)

    {

                   . . . . .

                   statement;

                   . . . . .

    }

    Flowchart


    Example Program

    #include

    void main()

    {

        int n = 5; /*total number of times the loop to be executed */

       for (int i=1; i<=n; i++)

        {

            printf("C for loop statement : %d\n", i);

        }

    }

    Output

    C for loop statement : 2

    C for loop statement : 3

    C for loop statement : 4

    C for loop statement : 5