Control Flow: Loops (for, while, do-while)
Implementing repetitive tasks using `for`, `while`, and `do-while` loops.
Loop Control Statements in C: break
and continue
Loop control statements are essential for managing the flow of execution within loops in C programming. Two important statements are break
and continue
. They provide fine-grained control over how loops iterate.
Understanding break
and continue
break
Statement
The break
statement is used to immediately terminate the execution of a loop (for
, while
, or do-while
). When the break
statement is encountered inside a loop, the control is transferred to the statement immediately following the loop.
continue
Statement
The continue
statement is used to skip the rest of the current iteration of a loop and move to the next iteration. When the continue
statement is encountered inside a loop, the remaining statements within the loop's body for that iteration are skipped, and the loop proceeds to the next iteration (if the loop condition allows).
Examples in for
, while
, and do-while
Loops
for
Loop with break
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // Exit the loop when i is 5
}
printf("Value of i: %d\n", i);
}
printf("Loop terminated.\n");
return 0;
}
In this example, the loop prints the values of i
from 1 to 4. When i
becomes 5, the break
statement is executed, and the loop is terminated. The output will be:
Value of i: 1
Value of i: 2
Value of i: 3
Value of i: 4
Loop terminated.
for
Loop with continue
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
printf("Value of i (odd): %d\n", i);
}
printf("Loop finished.\n");
return 0;
}
In this example, the loop prints only the odd numbers from 1 to 10. When i
is even, the continue
statement is executed, skipping the printf
statement for that iteration. The output will be:
Value of i (odd): 1
Value of i (odd): 3
Value of i (odd): 5
Value of i (odd): 7
Value of i (odd): 9
Loop finished.
while
Loop with break
#include <stdio.h>
int main() {
int i = 1;
while (i <= 10) {
if (i == 7) {
break; // Exit the loop when i is 7
}
printf("Value of i: %d\n", i);
i++;
}
printf("Loop terminated.\n");
return 0;
}
This while
loop prints the values of i
from 1 to 6. When i
becomes 7, the break
statement is executed, and the loop is terminated. The output will be:
Value of i: 1
Value of i: 2
Value of i: 3
Value of i: 4
Value of i: 5
Value of i: 6
Loop terminated.
while
Loop with continue
#include <stdio.h>
int main() {
int i = 0;
while (i < 10) {
i++;
if (i % 3 != 0) {
continue; // Skip numbers not divisible by 3
}
printf("Value of i (divisible by 3): %d\n", i);
}
printf("Loop finished.\n");
return 0;
}
This while
loop prints the numbers from 1 to 10 that are divisible by 3. When i
is not divisible by 3, the continue
statement is executed, skipping the printf
statement for that iteration. The output will be:
Value of i (divisible by 3): 3
Value of i (divisible by 3): 6
Value of i (divisible by 3): 9
Loop finished.
do-while
Loop with break
#include <stdio.h>
int main() {
int i = 1;
do {
if (i == 4) {
break; // Exit the loop when i is 4
}
printf("Value of i: %d\n", i);
i++;
} while (i <= 5);
printf("Loop terminated.\n");
return 0;
}
This do-while
loop prints the values of i
from 1 to 3. When i
becomes 4, the break
statement is executed, and the loop is terminated. The output will be:
Value of i: 1
Value of i: 2
Value of i: 3
Loop terminated.
do-while
Loop with continue
#include <stdio.h>
int main() {
int i = 0;
do {
i++;
if (i % 5 != 0) {
continue; // Skip numbers not divisible by 5
}
printf("Value of i (divisible by 5): %d\n", i);
} while (i <= 10);
printf("Loop finished.\n");
return 0;
}
This do-while
loop prints the numbers from 1 to 10 that are divisible by 5. When i
is not divisible by 5, the continue
statement is executed, skipping the printf
statement for that iteration. The output will be:
Value of i (divisible by 5): 5
Value of i (divisible by 5): 10
Loop finished.
Important Considerations
- Using
break
andcontinue
can sometimes make code harder to read and understand. Consider whether alternative loop structures or conditional statements could achieve the same result with greater clarity. - Carefully plan the logic of your loops when using
break
andcontinue
to avoid unintended behavior.