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:-
- Initialize the variable n=5, sum=0.
- Computes the square of n or (n*n) and adds it to sum.
- Tests the value of n using if statement whether it is equal to 10 or not.
- If it is equal to 10,then the program prints the results.
- 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:
- Entry controlled loop
- 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.
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.
In general, a looping process include the following four steps.
- Initialization of Condition Variable.
- Execution of the statement in the loop.
- Test for a specified value of the condition variable for execution of the loop.
- Increment or decrement the conditional variable.
C provides the following loops:
- The while loop.
- The do loop.
- 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.
- Counter- controlled loops.
- Sentinel- controlled loops.
- 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.
- 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.
We use a control variable known as Counter. It must be initialized, tested or modified for desired loop operations.
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.