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 program.
- while loop
- do-while loop
- for loop
- nested loop
for loop
for loop has 3 parts in parameters.
- initialization
- condition
- increment or decrement
for loop is more control loop among all. We can mention condition and increment or decrement with initialization.
This loop has a counter variable.
In C program we can only initialized the value of counter variable, we are not allowed to declare the variable, deceleration of variable is allowed in C99 and C11 mode.

Syntax:
for(initialization ; condition ; increment or decrement)
{
block_of_code;
}
Example:
#include<stdio.h>
void main()
{
int num;
for(num=1; num<=5; num++)
{
print("%d ITVoyagersn", num);
}
}
Output:
1 ITVoyagers
2 ITVoyagers
3 ITVoyagers
4 ITVoyagers
5 ITVoyagers
Explanation
In above program we have declared a int variable num and in for loop we initialized value 1 to num variable. When for loop will start it’s iteration it will check if condition is valid, in this case yes it is valid, so for loop will start it’s first iteration.
Iteration 1 :
When value of num is 1 for loop will check if the condition is true.
Now since the condition is true for loop will start executing block of code.
First it will print “1 ITVoyagers”
Now before starting next iteration for loop will upgrade the counter, in this case it will increment the counter value by 1.
Iteration 2 :
Now value of num is 2, for loop will check if the condition is true.
Now since the condition is true for loop will start executing block of code.
First it will print “2 ITVoyagers”
Now before starting next iteration for loop will upgrade the counter, in this case it will increment the counter value by 1.
Iteration 3 :
Now value of num is 3, for loop will check if the condition is true.
Now since the condition is true for loop will start executing block of code.
First it will print “3 ITVoyagers”
Now before starting next iteration for loop will upgrade the counter, in this case it will increment the counter value by 1.
.
.
.
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 quiz on for loop
Nested loop
As name says nesting one loop in another loop is called as nested loop.
Example:
#include<stdio.h>
void main()
{
int num1 = 1, num2 = 1;
do
{
while(num>5)
{
printf("%d itvoyagers.inn", num2);
num2++;
}
print("%d ITVoyagersn", num1);
}
while(num1>5);
}
Output:
1 itvoyagers.in
2 itvoyagers.in
3 itvoyagers.in
4 itvoyagers.in
5 itvoyagers.in
1 ITVoyagers
Explanation
We have while loop nested in do-while loop.
Iteration 1 :
When value of num1 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 run inner while loop
Inner Iteration 1 :
When value of num2 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 num2.
Inner Iteration 2 :
Now value of num2 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 num2.
.
.
.
This will go on till 5th iteration. In 5th iteration value of num2 will increment to 6.
Inner Iteration 6 :
Now value of num2 is 6.
Since condition is not valid while loop will stop iteration and compiler will get out of the loop
Now compiler is out of inner loop i.e. while loop.
Next 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.
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.
