Looping Statements in c
Looping Statements in c
INTELLE LEARN
What is a Looping Statement in C?
A looping statement in C programming is used to repeat a block of code
multiple times, depending on a specified condition. Loops allow programmers
to execute a set of instructions as long as a condition is true, making code
more efficient by avoiding the need to write repetitive tasks manually.
C provides three primary types of loops:
1. for loop
2. while loop
3. do-while loop
1. FOR LOOP
The for loop is used when the number of iterations is known beforehand. It
consists of three parts: initialization, condition, and increment/decrement.
Syntax:
for (initialization; condition; increment/decrement) {
// code to be executed in each iteration
}
Initialization: This step runs once at the start of the loop.
Condition: The loop runs as long as this condition is true.
Increment/Decrement: Updates the loop control variable after each
iteration.
Example:
#include <stdio.h>
int main() {
int i; Output:
for (i = 1; i <= 5; i++) { Iteration 1
printf("Iteration %d\n", i); Iteration 2
} Iteration 3
Iteration 4
return 0;
} Iteration 5
In this example, the loop starts with i = 1 and continues until i <= 5. After each
iteration, i is incremented by 1.
2. WHILE LOOP
The while loop is used when the number of iterations is not known
beforehand and depends on a condition. It keeps executing the loop as long
as the condition is true.
Syntax:
while (condition) {
// code to be executed in each iteration
}
Example:
#include <stdio.h>
int main() {
int i = 1;
Output:
while (i <= 5) {
printf("Iteration %d\n", i); Iteration 1
i++; // Increment the control variable Iteration 2
}
Iteration 3
return 0; Iteration 4
}
Iteration 5
In this case, the loop checks the condition i <= 5 before each iteration. Once i
exceeds 5, the loop stops.
3. DO WHILE LOOP
The do-while loop is similar to the while loop, but the key difference is that the
condition is checked after the block of code is executed. This guarantees that
the loop runs at least once, even if the condition is initially false.
Syntax:
do {
// code to be executed
} while (condition);
Example:
#include <stdio.h>
int main() {
int i = 1; Output:
do {
Iteration 1
printf("Iteration %d\n", i); Iteration 2
i++; // Increment the control variable Iteration 3
} while (i <= 5);
Iteration 4
return 0; Iteration 5
} Here, the loop executes the block of code first and checks the condition later. This
ensures that the loop runs at least once, even if the condition becomes false after
the first iteration.
4. CONTROL STATEMENTS IN LOOPS
Sometimes, you need more control over loop execution. C provides control
statements like break and continue to manage the flow of loops.
1. break Statement
The break statement is used to exit a loop prematurely, usually when a
certain condition is met.
It causes the loop to terminate immediately, regardless of the original
loop condition.
Syntax:
break;
Example:
#include <stdio.h>
int main() {
int i;
Output:
for (i = 1; i <= 10; i++) {
if (i == 5) { Iteration 1
break; // Exit the loop when i is 5 Iteration 2
}
printf("Iteration %d\n", i); Iteration 3
} Iteration 4
return 0; Here, the loop terminates when i equals 5, so only iterations from 1 to 4 are printed.
}
2. continue Statement
The continue statement skips the rest of the code inside the loop for the
current iteration and proceeds to the next iteration.
It doesn’t terminate the loop; it just skips to the next iteration.
Syntax:
continue;
Example:
#include <stdio.h>
int main() {
int i;
Output:
for (i = 1; i <= 5; i++) {
if (i == 3) { Iteration 1
continue; // Skip the iteration when i is 3 Iteration 2
}
printf("Iteration %d\n", i); Iteration 4
} Iteration 5
return 0; Here, the loop skips printing "Iteration 3" and continues with the next iterations.
}
5. NESTED LOOPS
A nested loop is a loop inside another loop. The inner loop will complete all its
iterations for every single iteration of the outer loop.
Example:
#include <stdio.h>
int main() {
int i, j;
Output:
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 2; j++) { i = 1, j = 1
printf("i = %d, j = %d\n", i, j); i = 1, j = 2
}
} i = 2, j = 1
i = 2, j = 2
return 0;
}
i = 3, j = 1
i = 3, j = 2
In this example, for each iteration of the outer loop (with i values from 1 to 3), the inner loop (with j
values from 1 to 2) runs completely.
6. DIFFERENCE BETWEEN LOOPS
1. for Loop:
Best used when the number of iterations is known in advance.
All loop control statements (initialization, condition, increment) are
located in one place.
2. while Loop:
Used when the number of iterations is not known beforehand.
The loop might not run at all if the condition is false from the beginning.
3. do-while Loop:
Ensures that the loop body is executed at least once, even if the condition
is false initially.
Thank you!!