Loops in C


It is possible to execute a part of program repeatedly by using a counter and goto keyword and control statements like “if” statement. Here we have to initialize and increment the counter and test its value at proper place in the program for the completion of loop.

Example: Say we want to calculate the sum of squares of all integer between 5 to 10, we can write the program using if statement as shown below.

The program does the following things:-

  1. Initialize the variable n=5, sum=0.
  2. Computes the square of n or (n*n) and adds it to sum.
  3. Tests the value of n using if statement whether it is equal to 10 or not.
  4. If it is equal to 10,then the program prints the results.
  5. If n is less than 10, then n is incremented by one and the control goes back to compute the sum again.

The program evaluate the statement sum=sum +n*n 5 times as n is starts from 5 and ends at 10.

In above program, loops is executed 5 times. This number can be increased or decreased easily by changing the relational expression appropriately in the statement if(n==10).

In such programs where the exact number of repetitions are known, there are more convenient methods of looping in C that enables us to write concise program, containing repetitive processes without using goto statements. In looping, a sequence of statements are executed repeatedly until some condition of termination of the loop are satisfied. A program loop consist of two segments, one is known as body of the loop, second is known as control statement.x The control statement test certain condition and then directs the repeated execution of statement contained in the body of the loop.

Depending on the position of the control statement in the loop, a control structure can be classified as follow:

  1. Entry controlled loop
  2. Exit controlled loop

Entry controlled loop: - It is also known as Pre-Test loops. In Entry controlled loop the control conditions are tested before the start of loop execution. If the control conditions are not satisfied then the body of the loop will not be executed.

Entry Controlled Loop

Exit controlled loop: - It is also known as Post-Test loops. In Exit controlled loop the control condition are tested at the end of the body of the loop. So that the body is executed unconditionally for the first time.

Exit Controlled Loop

In general, a looping process include the following four steps.

  1. Initialization of Condition Variable.
  2. Execution of the statement in the loop.
  3. Test for a specified value of the condition variable for execution of the loop.
  4. Increment or decrement the conditional variable.

C provides the following loops:

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

There are two types of Controlled loops based on the nature of controlled variable & the value to be assigned to it for testing of control expression.

  1. Counter- controlled loops.
  2. Sentinel- controlled loops.
  1. Counter- controlled loops:- It is also Known as definite repetition loop .When we know in advance how many times the loop will be executed or repeated we use a counter controlled loop.
  2. We use a control variable known as Counter. It must be initialized, tested or modified for desired loop operations.

  3. Sentinel- controlled loops:- It is also Known as Indefinite repetition loop. When we do not know in advance, how many times the loop will be executed or repeated.
  4. A special value called a sentinel value is used to change the loop control expression from true to false. The controlled variable is known as Sentinel variable or Flag variable. The Sentinel value or Flag value is used to indicate the End of data entry by a special value like -5, 333 etc.

Do You Know?