Loading content...
Loading content...
In C programming, loops are used to execute a block of code repeatedly until a specified condition becomes false.
Without loops, we would have to write the same statement again and again.
For example, to print a message three times without a loop:
#include <stdio.h>
int main()
{
printf("Hello GfG\n");
printf("Hello GfG\n");
printf("Hello GfG\n");
return 0;
}Hello GfG
Hello GfG
Hello GfGWriting the same statement multiple times is not practical. Loops solve this problem by allowing us to repeat code efficiently.
Loops help us:
Reduce code duplication.
Improve readability.
Save development time.
Execute a block of code multiple times.
Process collections of data efficiently.
#include <stdio.h>
int main()
{
for(int i = 0; i < 3; i++)
{
printf("Hello GfG\n");
}
return 0;
}Hello GfG
Hello GfG
Hello GfGUsing a loop, the same result is achieved with much less code.
C provides three types of loops:
for Loop
while Loop
do-while Loop
The for loop is an entry-controlled loop. This means the condition is checked before executing the loop body.
It is commonly used when the number of iterations is known in advance.
for(initialization; condition; update)
{
// loop body
}Initialization
Initializes the loop variable.
int i = 0;Condition
Checks whether the loop should continue.
i < 5Update
Updates the loop variable after each iteration.
i++#include <stdio.h>
int main()
{
for(int i = 1; i <= 5; i++)
{
printf("%d ", i);
}
return 0;
}1 2 3 4 5The while loop is also an entry-controlled loop.
The condition is checked before executing the loop body.
It is generally used when the number of iterations is not known beforehand.
while(condition)
{
// loop body
}#include <stdio.h>
int main()
{
int i = 1;
while(i <= 5)
{
printf("%d ", i);
i++;
}
return 0;
}1 2 3 4 5Initialize the loop variable.
Check the condition.
Execute the loop body if the condition is true.
Update the loop variable.
Repeat until the condition becomes false.
The do-while loop is an exit-controlled loop.
Unlike for and while, the condition is checked after executing the loop body.
Therefore, the loop body executes at least one time, even if the condition is false.
do
{
// loop body
}
while(condition);#include <stdio.h>
int main()
{
int i = 1;
do
{
printf("%d ", i);
i++;
}
while(i <= 5);
return 0;
}1 2 3 4 5In a while loop, the condition is checked before execution.
In a do-while loop, the condition is checked after execution.
Because of this, a do-while loop always executes at least once.
An infinite loop is a loop that never terminates because its condition never becomes false.
Such loops continue running forever until the program is manually stopped.
for(;;)
{
printf("Infinite Loop");
}while(1)
{
printf("Infinite Loop");
}do
{
printf("Infinite Loop");
}
while(1);A nested loop is a loop inside another loop.
The inner loop executes completely for every iteration of the outer loop.
Nested loops are commonly used for:
Pattern printing
Matrix operations
Working with two-dimensional arrays
#include <stdio.h>
int main()
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 2; j++)
{
printf("i = %d, j = %d\n", i, j);
}
}
return 0;
}textCopy
i = 0, j = 0
i = 0, j = 1
i = 1, j = 0
i = 1, j = 1
i = 2, j = 0
i = 2, j = 1Loop control statements are used to alter the normal execution of loops.
C provides three loop control statements:
break
continue
goto
The break statement immediately terminates the loop.
#include <stdio.h>
int main()
{
for(int i = 0; i < 5; i++)
{
if(i == 3)
{
break;
}
printf("%d ", i);
}
return 0;
}0 1 2The continue statement skips the current iteration and moves to the next iteration of the loop.
#include <stdio.h>
int main()
{
for(int i = 0; i < 5; i++)
{
if(i == 3)
{
continue;
}
printf("%d ", i);
}
return 0;
}0 1 2 4The goto statement transfers program control to a labeled statement.
Although supported in C, it should be used carefully because excessive use can make code difficult to understand.
#include <stdio.h>
int main()
{
int i;
for(i = 0; i < 5; i++)
{
if(i == 3)
{
goto skip;
}
printf("%d ", i);
}
skip:
printf("\nJumped to label");
return 0;
}0 1 2
Jumped to labelUse a for loop when the number of iterations is known.
Use a while loop when the number of iterations is unknown and depends on a condition.
Use a do-while loop when the loop body must execute at least once.
Loops are used to execute a block of code repeatedly.
C provides three loops: for, while, and do-while.
for and while are entry-controlled loops.
do-while is an exit-controlled loop.
Infinite loops run forever because their condition never becomes false.
Nested loops are loops placed inside other loops.
Loop control statements such as break, continue, and goto modify the normal flow of loops.
Choosing the correct loop improves program readability and efficiency.