C Loops

What are Loops in C?

Loops are used to execute a block of code repeatedly until a specified condition becomes false.

They help reduce code repetition and make programs more efficient.

Instead of writing the same statement multiple times, a loop can execute it automatically.

Example

for(int i = 1; i <= 5; i++)
{
printf(“%d\n”, i);
}

Explanation

• for loop repeats the code multiple times.

• i starts from 1.

• The loop continues until i becomes greater than 5.

• i++ increases the value of i by 1 in each iteration.

• The output will display numbers from 1 to 5.

Why Loops are Important?

Loops are important because they:

• Reduce code repetition.

• Save programming time.

• Make programs shorter and easier to maintain.

• Help perform repetitive tasks efficiently.

• Improve code readability.

Types of Loops in C

C provides three types of loops:

• for Loop

• while Loop

• do-while Loop

Each loop is used to execute a block of code repeatedly based on a condition.

for Loop

The for loop is used when the number of iterations is known in advance.

Syntax:

for(initialization; condition; increment/decrement)
{
// code
}

Example

for(int i = 1; i <= 5; i++)
{
printf(“%d\n”, i);
}

Explanation

• Initialization runs only once.

• Condition is checked before every iteration.

• Increment updates the loop variable.

• The loop prints numbers from 1 to 5.

while Loop

The while loop is used when the number of iterations is not known in advance.

The loop continues until the specified condition becomes false.

Syntax:

while(condition)
{
// code
}

Example

int i = 1;

while(i <= 5)
{
printf(“%d\n”, i);
i++;
}

Explanation

• The condition is checked before each iteration.

• The loop executes while the condition is true.

• i++ increases the value of i by 1.

• The loop prints numbers from 1 to 5.

do-while Loop

The do-while loop executes the code at least once before checking the condition.

The condition is checked after the execution of the loop body.

Syntax:

do
{
// code
}
while(condition);

Example

int i = 1;

do
{
printf(“%d\n”, i);
i++;
}
while(i <= 5);

Explanation

• The loop body executes first.

• The condition is checked after execution.

• The loop runs at least one time even if the condition is false.

• The loop prints numbers from 1 to 5.

Comparison of Loops

• for Loop → Used when the number of iterations is known.

• while Loop → Used when the number of iterations is not known.

• do-while Loop → Executes at least once before checking the condition.

Example Program

Program

#include <stdio.h>

int main()
{
int i;

for(i = 1; i <= 5; i++)
{
printf(“%d\n”, i);
}

return 0;
}

Output

1
2
3
4
5

Explanation

• int i; → Declares a loop variable.

• i = 1 → Initializes the loop.

• i <= 5 → Condition that controls the loop.

• i++ → Increases the value of i by 1 after each iteration.

• printf() → Displays the value of i.

• The loop prints numbers from 1 to 5.

Key Points

• Loops are used to execute a block of code repeatedly.

• Loops reduce code repetition and improve efficiency.

• C provides three types of loops: for, while, and do-while.

• for loop is used when the number of iterations is known.

• while loop is used when the condition controls the loop.

• do-while loop executes at least one time.

• Proper use of loops makes programs shorter and easier to maintain.