There are three types of loops present in the C language. They are:
We will discuss each loop below.
1. While Loop
Programming Example 1
int main()
{
int i = 1 ; //Initialization
while ( i<= 5 ) //condition
{
printf( " Avishek " ) ;
i++ ; //flow
}
return 0 ;
}
Output
Explanation
Here i is a control variable to control the loop. The condition part will be treated as either true or false. If it is true, then it enters inside the loop. In the flow part, we can increment or decrement the control variable’s value. Here, the condition will be checked 1 time more than printing the value because when the condition gets false, another time condition will be checked.
While loop is also called an entry-controlled loop because here, we can either inside the loop’s body inside the block when the condition is true.
Programming Example 2
Output
Explanation
In this program, we want to print some values from the user some range with the help of a while loop. For this, we use the scanf() function to take input from the user. Then we simply run a while loop to execute the program.
Programming Example 3
Output
Explanation
The above-mentioned program is also an example of the while loop. Here we want to sum up some numbers that are given by the user. As we are doing the same action, we just run the while loop. It just sums the values repeatedly by executing the same statement multiple times.
Do While Loop
Programming Example 4
int main ()
{
int i = 1 ; //Initialisation
do
{
printf( " Avishek " ) ;
i++ ; //flow
} while ( i<= 5 ) ; //condition
return 0 ;
}
Output
Explanation
Do while loop is also called the exit control loop. Because in this loop, we can enter at least one time without checking the condition. After performing one operation & increment the value of the control variable “i”, the condition is in check. Here, the condition will be checked as same as the no of operation is performed, like printing the name Avishek.
Programming Example 5
Output
Explanation
In this program, we want to print some values from the user some range with the help of the Do-while loop. For this, we use the scanf() function to take input from the user. Then we simply run a Do-while loop to execute the program
Programming Example 6
Output
Explanation
The above-mentioned program is also an example of a Do-while loop. Here we want to sum up some numbers that are given by the user. As we are doing the same action, we just run the Do-while loop. It just sums the values repeatedly by executing the same statement multiple times
For Loop
Programming Example 7
int main()
{
int i ;
for ( i = 1 ; i<= 5 ; i++ )
{
printf( " Avishek " ) ;
}
return 0 ;
}
Output
Explanation
In the for loop parenthesis, we are writing three steps that is Initialisation, condition & flow (increment or decrement). It is also called entry controlled loop. At first, it checked the condition. If the condition is true, then it allows for entry into the loop.
Programming Example 8
Output
Explanation
In this program, we want to print some values from the user some range with the help of for loop. For this, we use the scanf () function to take input from the user. Then we simply run a for loop to execute the program
Programming Example 9
Output
Explanation
The above-mentioned program is also an example of for loop. Here we want to sum up some numbers that are given by the user. As we are doing the same action, we just run for a loop. It just sums the values repeatedly by executing the same statement multiple times.
Conclusion
We learn from the above concept that loops are an important concept of C language as it plays an important role in executing the same statement multiple times. Once we write the code of the loop one time, it executes multiple times according to the requirement of the program.