In this topic we are going to learn about different types of loops available in C language.
What is a loop?
A loop is a statement that allows you to execute a block of code a specified number of times or as many times as specified by the user until the statement turns false.
The different types of loops provided in C are:
- For loop
- While loop
- Do-while loop
Lets discuss them one by one.

For Loop:
In for loop, the initialisation statement is only executed once at the starting of the loop, then the test expression is tested.
If the test expression is satisfied then the body of the loop is printed and the expression is updated as instantiated by the update statement.
If the test expression results false the loop is terminated. The for loop is initialised with the for a keyword.
Syntax Of For Loop:

for(initialisation expression; test expression; update statement) { //body of the loop }
For Example:
//For example: //Example to print 1 to 10 using for loop. #include<stdio.h> int main() { int i; for(i=1;i<=10;i++) { printf("\n%d",i); } return 0; }
In the above example the variable i of integer type is initialised with 1 and the test expression shows if i is less than equals to 10 and the update expression with i++, which states that when i is less than 10 till equals to 10 the value of i will be incremented by 1.
Output:
While Loop in C Programming:
In a while loop, the test expression is evaluated and tested.
If the condition is satisfied to true the body of the loop gets executed where the update statement is initialised in the body of the loop, until the test expression satisfies to false. In the case of the false, the loop gets terminated and the command gets out of the loop.
The while loop is initialized with the while keyword.
Syntax Of While Loop:
while( test expression) { //body of the loop }
For Example Of While Loop:
//For example: //To print numbers from 1 to 10 using while loop #include<stdio.h> int main() { int i=1; while(i<=10) { printf("\n%d",i); i++; } return 0; }
In the above example the test expression inside the while loop shows if i is less than or equals to 10. The loop continues with the update expression where the value of i is incremented by 1 in every stage. We can initialise the value of i, if not initialised the value is automatically initialised with 0. If the loop tends to be false then the loop is terminated.
Output Of While Loop:
do-while Loop In C Programming:
In the do-while loop, the body of the loop is executed once and then the test expression is evaluated. The loop gets terminated if the test expression results in false. The do-while loop is initialised with the do and while keyword.
Syntax For do-while Loop:
do { //body of the loop } while(test expression);
while(test expression);
So while expression is a condition where the control of loops move towards beginning and if condition false then terminate.
Example Of do-while:
//For example: //To print 1 to 10 using do while loop #include<stdio.h> int main() { int i=1; do { printf("\n%d",i); i++; } while(i<=10); return 0; }
In the above expression, the loop at first is executed and then the expression is checked and then with every states of the loop, the value of i is incremented with 1. When the test expression results to false then the loop gets terminated.
Output of do-while:
Difference between while and do-while loop:
the most common question you get is the difference between while loop and do-while loop so let’s discuss it.
While | Do while |
Condition checked then executed | Executed at least once, then condition is checked |
No semicolon in the end of while loop | Semicolon in the end of while loop |
No statement is executed if condition is false | Statement is executed once if condition is false |
While loop is entry controlled | Do while loop is exit controlled |
Here we come to the end of the discussion on loops in C, hope we have cleared all your dobts.
Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about the topic discussed above.