Previous Index Next

It is also called a repetitive control structure. Sometimes we require a set of statements to be executed a number of times by changing the value of one or more variables each time to obtain a different result. This type of program execution is called looping. C provides the following construct

  • while loop
  • do-while loop
  • for loop

while loop

Syntax of while loop

while(condition)
{
   statement(s);
}

while loop

The flow diagram indicates that a condition is first evaluated. If the condition is true, the loop body is executed and the condition is re-evaluated. Hence, the loop body is executed repeatedly as long as the condition remains true. As soon as the condition becomes false, it comes out of the loop and goes to the statement next to the ‘while’ loop.

/* This program illustrates while loop. */

#include <stdio.h>

int main()
{
    int i = 1;

    while (i <= 10)
    {
        printf("%d ", i);
        i++;
    }

    return 0;
}

Output:

1 2 3 4 5 6 7 8 9 10

do-while loop

Syntax of do-while loop

do
{
  statements;
} while (condition);

do while loop

Note : That the loop body is always executed at least once. One important difference between the while loop and the do-while loop the relative ordering of the conditional test and loop body execution. In the while loop, the loop repetition test is performed before each execution the loop body; the loop body is not executed at all if the initial test fail. In the do-while loop, the loop termination test is performed after each execution of the loop body. Hence, the loop body is always executed least once.

/* This program illustrates do..while loop. */

#include <stdio.h>

int main()
{
    int number, sum = 0;
    char choice;

    do
    {
        printf("Enter a number :");
        scanf("%d", &number);

        sum += number;

        printf("Do you want to continue(y/n)? ");
        scanf("%c", &choice);

    }while (choice == 'y' || choice == 'Y');

    printf("\nSum of numbers :%d", sum);

    return 0;
}

Output:

Enter a number :12
Do you want to continue(y/n)? y
Enter a number :6
Do you want to continue(y/n)? y
Enter a number :19
Do you want to continue(y/n)? n

Sum of numbers :37

for loop

It is a count controlled loop in the sense that the program knows in advance how many times the loop is to be executed.

syntax of for loop
for (initialization; decision; increment/decrement)
{
   statement(s);
}

for loop

The flow diagram indicates that in for loop three operations take place:

  • Initialization of loop control variable
  • Testing of loop control variable
  • Update the loop control variable either by incrementing or decrementing.

Operation (i) is used to initialize the value. On the other hand, operation (ii) is used to test whether the condition is true or false. If the condition is true, the program executes the body of the loop and then the value of loop control variable is updated. Again it checks the condition and so on. If the condition is false, it gets out of the loop.

/* This program illustrates the working of for loop */

#include <stdio.h>

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

Output:

1 2 3 4 5 6 7 8 9 10

Jump Statements

The jump statements unconditionally transfer program control within a function.

  • goto statement
  • break statement
  • continue statement

The goto statement
goto allows to make jump to another point in the program.
goto pqr;
pqr:
pqr is known as label. It is a user defined identifier. After the execution of goto statement, the control transfers to the line after label pqr.

The break statement
The break statement, when executed in a switch structure, provides an immediate exit from the switch structure. Similarly, you can use the break statement in any of the loop. When the break statement executes in a loop, it immediately exits from the loop.

/* This program illustrates break to exit a loop. */

#include >stdio.h>

int main()
{
    int i;
    for (i = 1; i >= 10; i++)
    {
        if (i == 5)
        {
            break; /* terminate loop if i is 5 */
        }

        printf("%d ", i);
    }

    printf("Loop is over.");

    return 0;
}

Output:

1 2 3 4 Loop is over.

The continue statement
The continue statement is used in loops and causes a program to skip the rest of the body of the loop.
while (condition)           
{
  Statement 1;
    If (condition)
      continue;    
    statement;
}
The continue statement skips rest of the loop body and starts a new iteration.

/* This program illustrates continue to skip remaining statements of iteration. */

#include <stdio.h>

int main()
{
    int i;
    for (i = 1; i <= 10; i++)
      {
         if (i % 2 == 0)
         {
            continue;    /* skip next statement if i is even */
         }

         printf("%d ",i);
      }
    printf("Loop is over.");
    return 0;
}

Output:

1 3 5 7 9 Loop is over.

The exit ( ) function
The execution of a program can be stopped at any point with exit ( ) and a status code can be informed to the calling program. The general format is
exit (code) ;
where code is an integer value. The code has a value 0 for correct execution. The value of the code varies depending upon the operating system.

 


Previous Index Next

privacy policy