Looping is a technique in which a set of statements are repeatedly executed until the condition specified becomes false. So looping is based on a condition. A loop is a repeated (iterated) sequence of statements. The repetition continues while a condition is true. When condition becomes false, the loop ends. Then control passes to the statements following the loop.
Looping Statements in C executes the sequence of statements many times until the stated condition becomes false. A loop in a program consists of two parts, one part is known as body of the loop and the other part is known as control statement. The control statement consists of certain conditions which are checked during execution and depending on the condition the statements within the body of the loop are executed repeatedly.
Categories of loops:
There are three types of loops available in C:
· For loop
· While loop
· Do…While loop
Loops can be classified as Entry Controlled loop and Exit Controlled loop. In case of entry controlled loop, the condition is tested first. The statements will be executed only if the condition specified is true. After execution the loop statements, again the condition is tested and the statements will be executed until the condition specified becomes false. The loops for and while are entry controlled loops.
In case of an exit controlled loops, the loop statements are executed first, then the condition will be tested. If the condition is true, again the statements will be executed until the condition becomes false. The loop do…while is exit controlled loop.
For Loop:
It is an entry controlled loop. Generally, for loop is used to execute a set of statements a specific number of times.
Syntax:
for(initialization; test condition; modifier expression)
{
body of the loop;
}
Initialization: The initialization part that controls the loop through a variable is called a loop index or control variable. The control variable is initialized first with a simple assignment statement. For example, i=0. The initial value is performed only once.
Test condition: The value of the control variable is tested using this test condition. For each repetition this is performed. This is a relational expression, such as i<10 which could determine the exit from the loop. If this expression is true, then the body of the loop is executed, and if false, the control comes out of the loop.
Modifier expression: At the end of the loop the control variable is modified and the updated value of the control variable is again tested. If the condition is satisfied, the loop continues, if not control exits.
Programming Example:
C program to print first 100 natural numbers.
Output:
While Loop:
The simplest of all the looping statements in C is the while loop. The while loop is an Entry Controlled loop.
In a while loop, the condition is checked before executing the body of the loop. If condition is true only then the body of the loop is executed otherwise not. After execution of the body of the loop, the control again goes back to test condition. The condition is checked, if it is true the body of the loop is executed once again. This process is repeated until the test condition becomes false. The loop will be continued until the test condition is true and terminates as soon as test condition becomes false. Once the control goes out of the loop the statements which are immediately after the loop is executed.
Syntax:
while (test condition)
{
body of the loop
}
Programming Example:
Program to print alphabet a to z.
Output:
Do-While Loop
It is similar to the while loop except that it tests the condition at the end of the loop body. It is an exit controlled loop statement. In do…while loop, the condition is always checked after the body of the loop.
In some cases, we have to execute a body of the loop at least once even if the condition is false. The while and for do not support such action. This type of operation can be achieved by using the do while statement.
In do…while loop, the body of the loop is always executed at least once. After the body is executed, then it checks the condition. If the condition is true, then it will again execute the body of the loop otherwise control is transferred out of the loop. Once the control goes out of the loop the statements which are immediately after the loop is executed.
Syntax:
do
{
body of the loop
}
while (condition) ;
On reaching the do statement, the control proceeds to execute the body of the loop. When the while statement is reached, the condition is evaluated. If the condition is true, control is transferred to the beginning of the do loop and the program continues to execute the body of the loop. Otherwise control is transferred out of the loop.
Programming Example:
Program to print integers between 0 and 10 using do-while loop
Output:
Break Statement:
Break statement terminates the loop or switch statement. Sometimes it becomes necessary to come out of the loop even before loop condition becomes false, and then break statement is used. Instead of exiting an entire program, use the break statement to exit the current loop or section of code.
Break statement is used inside loop and switch statements. It cause immediate exit from that loop in which it appears and it is generally written with condition. It is written with the keyword as break.
When break statement is encountered loop is terminated and control is transferred to the statement, immediately after loop. The break statement is usually associated with if statement.
Syntax: break;
Example:
for(i=1;i<=10;i++)
{
printf(“t%d”,i);
if(i==5)
break;
}
Output: 1 2 3 4 5
Here, although the for loop is to be executed 10 times, the break statement will terminate the loop when the control variable i becomes 5
Continue Statement:
This statement is used to go to the top of the loop from anywhere within the loop during its execution. In certain circumstances, it requires to skip a part of the body of the loop and restart from the beginning part of the loop. In such cases, continue statement is used.
Continue statement is used for continuing next iteration of loop after skipping some statement of loop. When it encountered control automatically passes through the beginning of the loop.
The continue statement can be used only inside a looping statement. It is usually associated with if statement. It is useful when we want to continue the program without executing any part of the program.
The difference between break and continue is, when the break encountered loop is terminated and it transfer to the next statement and when continue is encounter control come back to the beginning position.
Syntax: continue;
for(initialization; test condition; modifier expression)
{
…………………..
if(condition)
continue;
……….
}
Example:
for(i=1;i<=6;i++)
{
if(i==4)
continue;
printf(“t%d”,i);
}
Output:1 2 3 5 6
In the above example, only i=1,2,3,5,6 will be printed. When i is equal to 4, control returns to the beginning of the loop.
FAQ:
- What is Loop in C language?
- What is loop in C with example?
- What are the 3 types of loops in C?
- What is Loop in C and its types? Explain.
- What is for loop in with example?
- What is the syntax of the for loop?
- What is while loop in C with example?
- What is looping statements in C? Explain any one.
- How are loops classified in C?
- How many types of loops are there in C?
- What is a do-while loop?
- What does a do-while loop do in C?