what is loop?

loops are used to execute a set of statements repeatedly until a particular condition is satisfied.

A loop statement allows us to execute a statement or group of statements multiple times.


As per the above diagram, if the Test Condition is true, then the loop is executed, and if it is false then the execution breaks out of the loop. After the loop is successfully executed the execution again starts from the Loop entry and again checks for the Test condition, and this keeps on repeating.
The sequence of statements to be executed is kept inside the curly braces { } known as the Loop body. After every execution of the loop body, condition is verified, and if it is found to be true the loop body is executed again. When the condition check returns false, the loop body is not executed, and execution breaks out of the loop.


Why use loops in C language? :-

Suppose that you have to print table of 2, then you need to write 10 lines of code. By using the loop statement, you can do it by 2 or 3 lines of code only.

Types of C Loops :-

There are 3 types of Loop in C language
  • while loop
  • for loop
  • do while loop

  • while loop :-

    it iterates the code until condition is false. Here, condition is given before the code. So code may be executed 0 or more times.
    It is completed in 3 steps.
  • Variable initialization.(e.g int x = 0;)
  • condition(e.g while(x <= 10))
  • Variable increment or decrement ( x++ or x-- or x = x + 2 )

  • Syntax :
    variable initialization;
    while(condition)
    {
    statements;
    variable increment or decrement;
    }

    void main( )
    {
    int x;
    x = 1;
    while(x <= 10)
    {
    printf("%d\t", x);
    /* below statement means, do x = x+1, increment x by 1*/
    x++;
    }
    }

    Output :-

    1 2 3 4 5 6 7 8 9 10

    do while loop :-

    In some situations it is necessary to execute body of the loop before testing the condition. Such situations can be handled with the help of do-while loop. do statement evaluates the body of the loop first and at the end, the condition is checked using while statement. It means that the body of the loop will be executed at least once, even though the starting condition inside while is initialized to be false.

    Syntax :-
    do
    {
    .....
    .....
    }
    while(condition)

    void main()
    {
    int a, i;
    a = 5;
    i = 1;
    do
    {
    printf("%d\t", a*i);
    i++;
    }
    while(i <= 10);
    }

    Output :-

    5 10 15 20 25 30 35 40 45 50

    for loop :-

    for loop is used to execute a set of statements repeatedly until a particular condition is satisfied. We can say it is an open ended loop.

    for(initialization; condition; increment/decrement)
    {
    statement-block;
    }

    Here, initialization, condition and increment/decrement is given before the code. So code may be executed 0 or more times.

    In for loop we have exactly two semicolons, one after initialization and second after the condition. In this loop we can have more than one initialization or increment/decrement, separated using comma operator. But it can have only one condition.

    The for loop is executed as follows:
  • It first evaluates the initialization code.
  • Then it checks the condition expression.
  • If it is true, it executes the for-loop body.
  • Then it evaluate the increment/decrement condition and again follows from step 2.
  • When the condition expression becomes false, it exits the loop.

  • void main( )
    {
    int x;
    for(x = 1; x <= 10; x++)
    {
    printf("%d\t", x);
    }
    }

    Output :-

    1 2 3 4 5 6 7 8 9 10

    Nested for loop :-

    We can also have nested for loops, i.e one for loop inside another for loop.

    for(initialization; condition; increment/decrement)
    {
    for(initialization; condition; increment/decrement)
    {
    statement ;
    }
    }

    void main( )
    {
    int i, j;
    /* first for loop */
    for(i = 1; i < 5; i++)
    {
    printf("\n");
    /* second for loop inside the first */
    for(j = i; j > 0; j--)
    {
    printf("%d", j);
    }
    }
    }

    output :-

    1
    21
    321
    4321
    54321





    Previous Next

    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here


    Ittutorial