Why we are using loops ?
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 https://www.youtube.com/shorts/qNZyBM5ZwkY
There are four types of loops in C:
for
loopwhile
loopdo-while
loop- Nested loop
Each loop type has its own use case, depending on the problem you are solving.
1. 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
2. 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
3. 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
4. Nested Loop
A nested loop in C is when one loop is placed inside another loop. The inner loop will execute all of its iterations for every single iteration of the outer loop. Nested loops are useful when dealing with tasks like working with matrices, printing patterns, or handling multi-dimensional arrays
Syntax:
for (initialization; condition; increment/decrement) { // Outer loop
for (initialization; condition; increment/decrement) { // Inner loop
// Code to be executed
}
}
- Outer loop: This runs first and controls how many times the inner loop will execute.
- Inner loop: Executes its full cycle each time the outer loop runs one iteration.
Flowchart of Nested Loop:
Example:
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) { // Outer loop for rows
for (int j = 1; j <= 5; j++) { // Inner loop for columns
printf("* "); // Print star in each column
}
printf("\n"); // Move to the next line after printing one row
}
return 0;
}
Output:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
- Explanation:
- The outer loop runs 5 times, controlling the number of rows.
- The inner loop also runs 5 times, controlling the number of columns in each row.
- After the inner loop finishes, the
printf("\n");
moves the output to the next line. - So, each time the outer loop runs, a new row is printed with 5 stars.