0
0
Cprogramming~5 mins

Why loop control is required

Choose your learning style9 modes available
Introduction

Loop control helps manage how many times a loop runs. It stops loops from running forever and lets you skip or end loops early.

When you want to repeat a task a certain number of times.
When you need to stop a loop if a condition is met.
When you want to skip some steps inside a loop.
When you want to avoid infinite loops that freeze your program.
Syntax
C
for (initialization; condition; update) {
    // code to repeat
}

while (condition) {
    // code to repeat
}

// Loop control statements:
break;   // stops the loop
continue; // skips to next loop cycle

break stops the loop immediately.

continue skips the rest of the current loop and moves to the next cycle.

Examples
This loop stops when i reaches 3, so it prints 0 1 2.
C
for (int i = 0; i < 5; i++) {
    if (i == 3) {
        break;
    }
    printf("%d ", i);
}
This loop skips printing 3 but continues with other numbers: 0 1 2 4.
C
for (int i = 0; i < 5; i++) {
    if (i == 3) {
        continue;
    }
    printf("%d ", i);
}
Sample Program

This program prints odd numbers less than 5. It skips even numbers and stops when i reaches 5.

C
#include <stdio.h>

int main() {
    for (int i = 0; i < 10; i++) {
        if (i == 5) {
            break; // stop loop when i is 5
        }
        if (i % 2 == 0) {
            continue; // skip even numbers
        }
        printf("%d ", i);
    }
    return 0;
}
OutputSuccess
Important Notes

Without loop control, loops might run forever and crash your program.

Use break to exit loops early when you find what you need.

Use continue to skip steps you don't want to run inside the loop.

Summary

Loop control helps manage how loops run and stop.

break stops the loop immediately.

continue skips to the next loop cycle.