Loops

Loops are used in programming to repeat a specific block until some end condition is met. There are three loops in C programming:

  1. for loop
  2. while loop
  3. do…while loop

for Loop

Syntax of for Loop :

 for (init; condition; increment)
 {
    // block of statement.
 }

Example :

    #include <stdio.h>

    int main()
    {
    int i;

    for(i = 0; i < 10 ; i++)
    {
    printf("%d ",i);
    }
    return 0;
    }

 

Output :

 1 2 3 4 5 6 7 8 9 10

Explanation :

init – Initializes the variable at the beginning of the loop to some value. This value is the starting point of the loop.

condition – Decides whether the loop will continue running or not. While this condition is true, the loop will continue running.

increment – The part of the loop that changes the value of the variable created in the variable declaration part of the loop. The increment statement is the part of the loop which will eventually stop the loop from running.

 

while loop

while loop statement in C programming language repeatedly executes a target statement as long as a given condition is true.

Syntax :

    while( condition )
    {
    statement(s);
    }

 

Example :

    #include <stdio.h>
    int main ()
    {
    // local variable definition
    int a = 1;

    // while loop execution
    while( a < 5 )
    {
    //loops comes inside this body, until condition is true
    printf("Value of a: %d\n", a);
    a++;
    }

    return 0;
    }

 

Output :

 Value of a: 1
 Value of a: 2
 Value of a: 3
 Value of a: 4

 

do-while loop

do-while loop is similar to a while loop, except that a do-while loop is guaranteed to execute at least one time.The conditional expression appears at the end of the loop, so the statement(s) in the loop execute once before the condition is tested.

Syntax :

    do
    {
    statement(s);
    } while( condition );

 

Example :

    #include <stdio.h>
    int main ()
    {
    // declared local operand (variable)
    int a = 1;

    // do-while loop
    do
    {
    printf("value of a: %d\n", a);
    a = a + 1;
    } while( a < 5 );

    return 0;
    }

 

Output :

 value of a: 1
 value of a: 2
 value of a: 3
 value of a: 4

One more Example where condition is false :

    #include <stdio.h>
    int main ()
    {
    // declared local operand (variable)
    int a = 1;

    //here, Condition is false. a is not equals to zero
    do
    {
    printf("value of a: %d\n", a);
    a = a + 1;
    } while( a == 0 );

    return 0;
    }

 

Output :

 value of a: 1

 

break, continue, goto

break keyword

break statement neglect the statement after it and exit compound statement. in the loop and transfer the control outside the loop

Break it’s sole purpose to passes control out of the compound statement i.e. Loop, Condition, Method or Procedures.

Example :

    while(a)
    {
    while(b)
    {
    if(b == 10)
    {
    break;
    }
    }
    // break will bring us here.
    }

 

continue keyword

Similar,To break statement continue statement also neglect the statement after it in the loop and send control back to starting point of loop for next iteration instead of outside the loop.

Example :

    #include <stdio.h>
    int main ()
    {
    int a = 10;
    while(a < 20)
    {
    if( a == 15)
    {
    // skip the iteration
    a = a + 1;
    continue;
    }

    printf("value of a: %d\n", a);
    a++;
    }
    return 0;
    }

 

Output :

 value of a: 10
 value of a: 11
 value of a: 12
 value of a: 13
 value of a: 14
 value of a: 16
 value of a: 17
 value of a: 18
 value of a: 19

goto

  • goto statement transfer the control to the label specified with the goto statement
  • label is any name give to particular part in program
  • label is followed with a colon (:)

Syntax :

    label1:
    -
    -
    goto label1;

 

Example :

    #include <stdio.h>
    int main()
    {
    int i, j;

    for ( i = 0; i < 10; i++ )
    {
    printf( "Outer loop executing. i = %d\n", i );
    for ( j = 0; j < 3; j++ )
    {
    printf(" Inner loop executing. j = %d\n", j );
    if ( i == 5 )
    {
    goto stop;
    }
    }
    }

    // This message does not print.
    printf( "Loop exited. i = %d\n", i );

    stop:
    printf( "Jumped to stop. i = %d\n", i );
    }

 

Output :

    Outer loop executing. i = 0
     Inner loop executing. j = 0
     Inner loop executing. j = 1
     Inner loop executing. j = 2
    Outer loop executing. i = 1
     Inner loop executing. j = 0
     Inner loop executing. j = 1
     Inner loop executing. j = 2
    Outer loop executing. i = 2
     Inner loop executing. j = 0
     Inner loop executing. j = 1
     Inner loop executing. j = 2
    Outer loop executing. i = 3
     Inner loop executing. j = 0
     Inner loop executing. j = 1
     Inner loop executing. j = 2
    Outer loop executing. i = 4
     Inner loop executing. j = 0
     Inner loop executing. j = 1
     Inner loop executing. j = 2
    Outer loop executing. i = 5
     Inner loop executing. j = 0
    Jumped to stop. i = 5