Cbreak & continue

break & continue

break and continue are jump statements that change a loop's normal flow. break exits the enclosing loop (or switch) immediately, skipping any remaining iterations entirely, while continue skips only the rest of the current iteration and jumps straight to the next one.

Statement

Effect

break

Immediately exits the loop (or switch) entirely -- no more iterations run

continue

Skips the remainder of the current iteration and moves on to the next one

break in a for Loop
break is commonly used to stop searching as soon as a match is found, avoiding unnecessary work for the rest of the loop.

C
#include <stdio.h>

int main(void) {
    int values[] = {4, 8, 15, 16, 23, 42};
    int target = 16;
    int foundIndex = -1;

    for (int i = 0; i < 6; i++) {
        if (values[i] == target) {
            foundIndex = i;
            break; // no need to keep scanning once we've found it
        }
    }

    printf("Found at index: %d\n", foundIndex);
    // Output: Found at index: 3

    return 0;
}
continue in a for Loop
continue is useful for skipping specific elements without wrapping the rest of the loop body in an extra if.

C
#include <stdio.h>

int main(void) {
    // Print only the odd numbers from 0 to 9
    for (int i = 0; i < 10; i++) {
        if (i % 2 == 0) {
            continue; // skip even numbers, move to the next i
        }
        printf("%d ", i);
    }
    printf("\n");
    // Output: 1 3 5 7 9

    return 0;
}
break in a while Loop

C
#include <stdio.h>

int main(void) {
    int number;

    while (1) { // intentional infinite loop
        printf("Enter a number (-1 to stop): ");
        scanf("%d", &number);

        if (number == -1) {
            break; // exit the loop entirely
        }

        printf("You entered: %d\n", number);
    }

    printf("Done.\n");
    return 0;
}
continue in a while Loop

C
#include <stdio.h>

int main(void) {
    int i = 0;

    while (i < 10) {
        i++;
        if (i % 3 != 0) {
            continue; // skip everything below for non-multiples of 3
        }
        printf("%d is a multiple of 3\n", i);
    }

    return 0;
}
Note
In a for loop, continue still triggers the update expression (like i++) before re-checking the condition — it does not skip the update step, only the rest of the body.
break Inside a switch
break also terminates a switch statement, which is why it appears at the end of nearly every case block (see the switch/case page for details on fallthrough). Inside a loop that contains a switch, a break inside a case only exits the switch, not the surrounding loop.

C
for (int i = 0; i < 3; i++) {
    switch (i) {
        case 1:
            break; // exits the switch only, NOT the for loop
        default:
            printf("i = %d\n", i);
    }
}
// Output: i = 0, i = 2  (i = 1 hits the switch's break, prints nothing,
// but the for loop itself keeps going)
Key Points
  • break exits the enclosing loop or switch immediately -- no further iterations happen.

  • continue skips the rest of the current iteration and jumps to the next one.

  • In a for loop, continue still runs the update expression before re-checking the condition.

  • Inside a switch nested in a loop, break exits only the switch, not the loop.