Introduction to Loops in C
Loops in C programming language is a conditional concept used for consecutively executing a line or a block of code over and over again until it reaches the value desired by the programmer. In C programming, there are three types of loops, namely For Loop, While Loop and Do While Loop. Loops in C can also be combined with other control statements that include Break statement, Goto statement and Control statement. These loops can be used anywhere in the program, either as entry control or exit control units.
Different Types of Loops
There are 3 different types of Loops in C:
- While Loop
- Do While Loop
- For Loop
While Loop
In this, the condition is evaluated before processing the body of the loop. If the condition is true, then only the body of the loop is executed. Then the control goes back to the beginning after completing the loop once. The statements in the loop will be executed again, and if the condition is true and checked, then this process goes on until the condition becomes false. If the condition is false, the control will go out of the loop. After completion of the loop, the control will go to the statement that is immediately after the loop, and the body can contain more than one statement. The curly braces are not that important if it contains only one statement. If the condition is not true in the while loop, then loop statements won’t get executed.
Syntax:
while (condition) {
statements;
}
Example:
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1;
while(num<=5)
{
printf("%d\n",num);
num++;
}
return 0;
}
Output:
It will print the numbers from 1 to 5 like below.
Do While Loop
In this loop, the statements in the loop need to be executed at least once. After that, it checks the condition. If the condition is true, then it will again have executed the loop; otherwise, it will exit the loop. It is known as an exit controlled loop. It is similar to a while loop, and in this, the condition is always executed after the body of the loop. The while loop is executed only when the condition is true, but sometimes the statement needs to be executed at least once, so for that do-while loop has to be used. The difference between while and do-while loop is that in while loop while is written in the beginning and in do-while, the condition is mentioned at the end, and it ends with a semicolon (;).
Syntax:
do {
statements
} while (expression);
Example:
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1;
do
{
printf ("%d\n",2*num);
num++;
}
while(num<=5);
return 0;
}
Output:
The output of the above program is:
For Loop
It executes the set of statements until the time a particular condition is accomplished. It is known as the Open-ended loop. In For loop, we can have more than one initialization or increment/decrement, separated using a comma operator and one condition as well. For loop is used to evaluate the initialization part first, and then it checks the condition for true or false. If the condition is true, then it executes the set of statements of for loop. After that, it evaluates the increment or decrement condition until the condition becomes false, it repeats the same steps. It will exit the loop when the condition is false.
Syntax:
for (initial value; condition; incrementation or decrementation )
{
statements;
}
Example:
#include<stdio.h>
#include<conio.h>
int main()
{
int number;
for(number=1;number<=5;number++)
{
printf("%d\n",number);
}
return 0;
}
Output:
There are nested For loops as well in which there is the outer For loop and inner loop. In this nested loop, the inner loop is repeated for the times for a given condition of outer loop iteration.

4.5 (9,590 ratings)
View Course
Syntax:
for(initialization; condition; increment/decrement)
{
for(initialization; condition; increment/decrement)
{
statement ;
}
}
Example:
#include<stdio.h>
#include<conio.h>
void main( )
{
int i, j;
for(i = 1; i < 5; i++)
{
printf("\n");
for(j = i; j > 0; j--)
{
printf("%d", j);
}
}
}
Output:
Other Example:
#include <stdio.h>
#include<conio.h>
int main() {
int i, j;
int table = 2;
int max = 5;
for (i = 1; i <= table; i++) {
for (j = 0; j <= max; j++) {
printf("%d x %d = %d\n", i, j, i*j);
}
printf("\n");
}}
Output:
Control Statements
There are some loop control statements that need to be used in loops for different purpose and to achieve the end result. Below are the different statements that are used:
Break statement:
The break statement is used to exit the loop immediately after the execution of a particular statement for a particular condition.
Syntax:
While (Condition)
{ Statement 1; statement 2;
If (Condition)
{ break;}
Statement 3; }
Continue Statement
It generally skips the statements according to the condition. It is used to send the control directly to the condition and to continue the loop process. For a particular condition, it skips the current loop or statements and enters into a new loop or condition.
Syntax:
While (Condition)
{ Statement 1; statement 2;
If (Condition)
{ continue;}
Statement 3; }
Goto statement
It is used to transfer the protocol to a labeled statement.
Example:
#include<stdio.h>
#include<conio.h>
int main()
{
int number;
number=0;
repeat:
printf ("%d\n",number);
number++;
if(number<=5)
goto repeat;
return 0;
}
Output:
Conclusion – Loops in C
Above are the loops that are defined in the C programming language. To select a particular loop for solving the problem or writing the program, the program has to be very careful with the requirements of the client. The program has to analyze the problem, what type of checks are required, like pre and post check. Looping in C or in any programming language is one of the key concepts. There are generally two types that are entry controlled and exit controlled loop. The loops or statement blocks execute a number of times until the condition becomes false. So, it is better to analyze the issue or problem properly and select the loop accordingly for better performance of the program and memory usage.
Recommended Articles
This has been a guide to Loops in C. Here we discuss the introduction along with the different types of loops. You may also have a look at the following articles to learn more –