Introduction to Loops in C
Loops in C programming language are a conditional concept used to execute a line or block of code consecutively. In C programming, there are three loops: For Loop, While Loop, and Do While Loop. Loops in C can also be combined with other control statements such as the Break statement, Goto statement, and Control statement. These loops can be used anywhere in the program, in either 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
1. While Loop
In this, the condition is evaluated before processing the loop’s body. Only the loop’s body is executed if the condition is true. 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, this process goes on until the condition becomes false. The control will go out of the loop if the condition is false. After completion of the loop, the control will go to the statement immediately after the loop, and the body can contain more than one statement. The curly braces are not that important if it has 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.
2. Do While Loop
In this loop, the statements the loop need to be executed at least once. After that, it checks the condition. If the condition is true, it will again have executed the loop; otherwise, it will exit it. It is known as an exit-controlled loop. It is similar to a while loop, and the condition is always executed after the body of the loop. The while loop is performed only when the condition is true, but sometimes the statement must be conducted at least once, so the do-while loop has to be used. The difference between while and do-while loop is that in the while loop, while is written in the beginning, and do-while, the condition is mentioned at the end and 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:
3. 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. For loop is used to evaluate the initialization part first, checking the condition for true or false. If the condition is true, it executes the 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 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.
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (41 Courses, 29 Projects, 4 Quizzes)
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
Some loop control statements need to be used in loops for different purposes 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 executing a particular statement for a specific 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 client’s requirements. The program has to analyze the situation and what type of checks are required, like pre and post-check. Looping in C or 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 several 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 is a guide to Loops in C. Here we have discussed the introduction along with the different types of loops. You may also have a look at the following articles to learn more –
3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses