Types of Loop in c
3 minute read
Types of Loop in c
A Loop executes the sequence of statements many times until the given condition becomes false. A loop has two parts, a body of a loop and a control statement. The control statement is a conditions part that manage the body part of the loop to execute until the specified condition becomes false. The purpose of the loop is to repeat the same statements number of times.
Types of Loops
Loop is classified into two types:
1. Entry controlled loop
2. Exit controlled loop
In an entry controlled loop, a condition is checked before executing the body part of a loop. It is also called as a pre-checking loop.
In an exit controlled loop, a condition is checked after executing the body part of a loop. It is also called as a post-checking loop.
The control conditions must be well valid and proper defined otherwise the loop will goes into the infinite loop. The loop that does not stop executing and processes the statements number of times is called as an infinite loop. It is also known as an "Endless loop."
1. Entry control loop
There are two entry control loop
1) While loop
A while loop is the most straightforward looping structure.
Syntax
Syntax
while (condition) {
statements;
}
It is an entry-controlled loop. In while loop, a condition is evaluated before processing a body part of the loop. If a condition is true then and only the body of a loop is executed. After the body part of a loop is executed then control again goes back at the beginning, and the condition is checked if it is true, the same process is executed until the condition becomes false. Once the condition becomes wrong, the control goes out of the loop.
After exiting the loop, the control goes to the next statements which are after the loop. The body part of a loop can contain more than one statement. If it has only one statement, then the curly braces are not compulsory.
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int num=1;
while(num<=10) //while loop with condition
{
printf("%d\n",num);
num++; //incrementing operation
}
getch();
}
Output:
1
2
3
4
5
6
7
8
9
10
2) For loop
A for loop is a more efficient loop structure in 'C' programming. The general structure of for loop is as follows:
for (initial value; condition; incrementation or decrementation )
{
statements;
}
- The initial value of the for loop is performed only once.
- The condition is a Boolean expression that tests and compares the counter to a fixed value after each iteration, stopping the for loop when false is returned.
- The incrementation/decrementation increases (or decreases) the counter by a set value.
Following program illustrates the use of a simple for loop:
#include<stdio.h>
int main()
{
int number;
for(number=1;number<=10;number++) //for loop to print 1-10 numbers
{
printf("%d\n",number); //to print the number
}
return 0;
}
Output:
1
2
3
4
5
6
7
8
9
10
2. Exit control loop
Do…while() loop is a exit control loop.
A do-while loop is similar to the while loop except that the condition is always executed after the body of a loop. It is also called an exit-controlled loop.
The basic format of while loop is as follows:
do {
statements
} while (expression);
In the do-while loop, the body of a 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 a loop otherwise control is transferred out of the loop.
Similar to the while loop, once the control goes out of the loop the statements which are immediately after the loop is executed.
The critical difference between the while and do-while loop is that in while loop the while is written at the beginning. In do-while loop, the while condition is written at the end and terminates with a semi-colon (;)
Example
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1; //initializing the variable
do //do-while loop
{
printf("%d\n",num);
num++; //incrementing operation
}while(num<=10);
return 0;
}
Output:
1
2
3
4
5
6
7
8
9
10