Table of Contents
Loops in C
In programming there will be some statements or group of statements which we want to execute continuously for more than one time, this is where loops comes in the picture.
Loop allow us to continuously execute particular statement or block of statement to more than one time.
Loop will check for the condition, and loop will continuously execute particular statement or block of statement until the condition get false or invalid.
Following are the types of loops in C programming.
- while loop
- do-while loop
- for loop
- nested loop
while loop
while loop will execute the block of code till given condition is true, once the given condition is false it will stop.
while loop will check condition before executing its block of code, so once the condition is false while loop will not execute its block of code

Syntax:
while(conditional_statement){
block_of_code
}
Example:
#include<stdio.h>
void main()
{
int num = 1;
while(num<=5)
{
print("%d ITVoyagersn", num);
num++;
}
}
Output:
1 ITVoyagers
2 ITVoyagers
3 ITVoyagers
4 ITVoyagers
5 ITVoyagers
Explanation
In above program we have declared a int variable num and the value of num is 1. Now we want while loop to run block of code until and unless value of the num variable is less than or equal to 5.
In while loop first statement is print statement which will print value of num and ITVoyagers next to it.
On second line we have incremented the value of num by 1 with the help of incremental operator.
Iteration 1 :
When value of num is 1 while loop will check if the condition is true.
Now since the condition is true while loop will start executing block of code.
First it will print “1 ITVoyagers”
Next it will increment the value of num.
Iteration 2 :
Now value of num is 2 while loop will check if the condition is true.
Now since the condition is true while loop will start executing block of code.
First it will print “2 ITVoyagers”
Next it will increment the value of num.
Iteration 3 :
Now value of num is 3 while loop will check if the condition is true.
Now since the condition is true while loop will start executing block of code.
First it will print “3 ITVoyagers”
Next it will increment the value of num.
.
.
.
This will go on till 5th iteration. In 5th iteration value of num will increment to 6.
Iteration 6 :
Now value of num is 6.
Since condition is not valid while loop will stop iteration and compiler will get out of the loop
Try this fun game
do-while loop
As name says, first it will execute its block of code and then it will check for condition.
Unlike while loop, do-while loop will check condition after executing its block of code, so do-while loop will run the its block of code at least once.

Syntax:
do
{
block_of_code
}
while(conditional_statement);
Example:
#include<stdio.h>
void main()
{
int num = 1;
do
{
print("%d ITVoyagersn", num);
}
while(num>5);
}
Output:
1 ITVoyagers
Explanation
We have set condition that if the value of num is greater than 5 block then of code will get executed.
Iteration 1 :
When value of num is 1 do-while loop will first execute its block of code and then it will check if condition is true or not.
First it will print “1 ITVoyagers”
Now it will check if condition is true or not. Since the condition is false compiler will get out of do-while loop.
Try this fun quiz
CHECKOUT OTHER RELATED TOPICS
CHECKOUT OTHER QUIZZES
Quizzes on Operator |
Easy Quiz on Operators in C part 1 |
Easy quiz on operators in C part 2 |
Easy quiz on operators in C part 3 |
Quizzes on Loops |
Easy quiz on loops in C part 1 |
Easy quiz on loops in C part 2 |
Easy quiz on loops in C part 3 |
Quizzes on Decision Making Statement |
Easy Quiz On Decision Making Statements Part 1 |
Easy Quiz On Decision Making Statements Part 2 |
Quizzes on String |
Easy quiz on String in C programming part 1 |
Quizzes on Array |
Easy quiz on Array in C programming – part 1 |
5 – Best Java program to convert decimal to binary |
New Quizzes Coming soon.. |
PRACTICALS IN C PROGRAM
Programs to understand the basic data types and I/O.
Programs on Operators and Expressions.
Programs on decision statements.
Programs on looping.
Programs to understand the basic data types and I/O.
Programs on arrays.
Programs on functions.
Programs on structures and unions.
Programs on pointers.
Programs on string manipulations.
