Loops Tutorials
Programming
Loops in C
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;
}Output
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.
Why Do We Need Loops?
Loops help us:
Reduce code duplication.
Improve readability.
Save development time.
Execute a block of code multiple times.
Process collections of data efficiently.
Example of Loop
#include <stdio.h>
int main()
{
for(int i = 0; i < 3; i++)
{
printf("Hello GfG\n");
}
return 0;
}Output
Hello GfG
Hello GfG
Hello GfGUsing a loop, the same result is achieved with much less code.
Types of Loops in C
C provides three types of loops:
for Loop
while Loop
do-while Loop
for 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.
Syntax
for(initialization; condition; update)
{
// loop body
}Components of for Loop
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++Example of for Loop
#include <stdio.h>
int main()
{
for(int i = 1; i <= 5; i++)
{
printf("%d ", i);
}
return 0;
}Output
1 2 3 4 5while Loop
The 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.
Syntax
while(condition)
{
// loop body
}Example of while Loop
#include <stdio.h>
int main()
{
int i = 1;
while(i <= 5)
{
printf("%d ", i);
i++;
}
return 0;
}Output
1 2 3 4 5How while Loop Works
Initialize 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.
do-while Loop
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.
Syntax
do
{
// loop body
}
while(condition);Example of do-while Loop
#include <stdio.h>
int main()
{
int i = 1;
do
{
printf("%d ", i);
i++;
}
while(i <= 5);
return 0;
}Output
1 2 3 4 5Difference Between while and do-while
In 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.
Infinite Loop
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.
Example Using for Loop
for(;;)
{
printf("Infinite Loop");
}Example Using while Loop
while(1)
{
printf("Infinite Loop");
}Example Using do-while Loop
do
{
printf("Infinite Loop");
}
while(1);Nested Loops
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
Example of Nested Loop
#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;
}Output
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
Loop control statements are used to alter the normal execution of loops.
C provides three loop control statements:
break
continue
goto
break Statement
The break statement immediately terminates the loop.
Example
#include <stdio.h>
int main()
{
for(int i = 0; i < 5; i++)
{
if(i == 3)
{
break;
}
printf("%d ", i);
}
return 0;
}Output
0 1 2continue Statement
The continue statement skips the current iteration and moves to the next iteration of the loop.
Example
#include <stdio.h>
int main()
{
for(int i = 0; i < 5; i++)
{
if(i == 3)
{
continue;
}
printf("%d ", i);
}
return 0;
}Output
0 1 2 4goto Statement
The 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.
Example
#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;
}Output
0 1 2
Jumped to labelChoosing the Right Loop
Use 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.
Summary
Loops are used to execute a block of code repeatedly.
C provides three loops:
for,while, anddo-while.forandwhileare entry-controlled loops.do-whileis 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, andgotomodify the normal flow of loops.Choosing the correct loop improves program readability and efficiency.