Index
Loops are used to repeatedly execute a block of code as long as a given condition is true. Loops are very useful when you need to perform repetitive tasks, such as iterating over an array, reading multiple inputs, or executing a set of instructions multiple times. For better understanding see the short below.
Types of loop
There are three types of loops in C:
for
loopwhile
loopdo-while
loop
Each loop type has its own use case, depending on the problem you are solving.
for Loop
The for
loop is used when you know in advance how many times you want to execute a block of code. It consists of three parts: initialization, condition, and increment/decrement, all of which are written in a single line.
Syntax:
for (initialization; condition; increment/decrement) {
// Code to be executed
}
- Initialization: A variable is initialized to a starting value.
- Condition: The loop runs as long as this condition is true.
- Increment/Decrement: This updates the loop variable at the end of each iteration.
Flowchart of for Loop:
Example:
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("Value of i: %d\n", i);
}
return 0;
}
Output:
Value of i: 1
Value of i: 2
Value of i: 3
Value of i: 4
Value of i: 5
- Explanation:
- The loop starts with
i = 1
. - The condition
i <= 5
ensures the loop runs as long asi
is less than or equal to 5. - After each iteration,
i
is incremented by 1 (i++
). - The loop prints the value of
i
from 1 to 5.
- The loop starts with
while Loop
The while
loop is used when the number of iterations is not known in advance. The loop runs as long as the given condition is true. If the condition is false from the start, the loop will not execute at all.
Syntax:
while (condition) {
// Code to be executed
}
Flowchart of while Loop:
Example:
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("Value of i: %d\n", i);
i++; // Increment i by 1
}
return 0;
}
Output:
Value of i: 1
Value of i: 2
Value of i: 3
Value of i: 4
Value of i: 5
- Explanation:
- The loop starts with
i = 1
. - The condition
i <= 5
ensures that the loop runs as long asi
is less than or equal to 5. - Inside the loop,
i
is incremented after each iteration. - The loop prints the value of
i
from 1 to 5.
- The loop starts with
do-while Loop
The do-while
loop is similar to the while
loop, but the key difference is that the do-while
loop guarantees that the block of code is executed at least once, even if the condition is false. This is because the condition is checked after the loop body is executed.
Syntax:
do {
// Code to be executed
} while (condition);
Flowchart of do-while Loop:
Example:
#include <stdio.h>
int main() {
int i = 1;
do {
printf("Value of i: %d\n", i);
i++; // Increment i by 1
} while (i <= 5);
return 0;
}
Output:
Value of i: 1
Value of i: 2
Value of i: 3
Value of i: 4
Value of i: 5
- Explanation:
- The loop starts with
i = 1
. - The code inside the
do
block executes, printing the value ofi
. - After executing the block, the condition
i <= 5
is checked. - The loop will run until
i
is greater than 5. - Even if the condition was false initially, the
do
block would still execute once.
- The loop starts with