05. Break, Continue & Jump Control Flow

🟢 Difficulty: Beginner | Early Loop Termination & Iteration Skipping

📖 Definition

The break and continue statements alter normal sequential loop execution in C. break terminates the innermost enclosing loop or switch immediately, jumping execution to the first statement past the loop. continue skips the rest of the current iteration's body and jumps directly to the next loop evaluation step.

🎯 Why It Is Used

  • Early Exit (break): Exiting a search loop as soon as a target element is found, eliminating unnecessary subsequent iterations.
  • Filtering Input (continue): Skipping invalid items, negative numbers, or comments during data processing without wrapping code in nested if blocks.

⚙️ How It Works

  • When break executes, control jumps out of the innermost enclosing loop or switch block.
  • When continue executes in a for loop, control jumps directly to the counter increment step (i++) and then re-evaluates the condition. In while / do-while, control jumps directly to the condition test.

📝 Syntax

// 1. Early Termination
for (int i = 0; i < 10; i++) {
    if (i == 5) break; // Exits loop when i reaches 5
}

// 2. Skipping Current Iteration
for (int i = 0; i < 10; i++) {
    if (i % 2 == 0) continue; // Skips even numbers
    printf("%d ", i); // Prints only odd numbers
}

🔀 Control Flow Statement Comparison

Statement Action Taken Target Jump Location
breakTerminates loop/switch immediately.First statement past the enclosing } brace.
continueSkips remainder of current iteration.Loop update step (for) or condition check (while).
goto labelJumps unconditionally to a labeled statement inside the same function.Target label: in function.

🌱 Beginner Example

#include <stdio.h>

int main(void) {
    printf("Printing odd numbers from 1 to 10 (skipping evens):\n");

    for (int i = 1; i <= 10; i++) {
        if (i % 2 == 0) {
            continue; // Skip even numbers
        }
        printf("%d ", i);
    }
    printf("\n");
    return 0;
}

🌼 Intermediate Example: Early Exit Search Loop

#include <stdio.h>
#include <stdbool.h>

int main(void) {
    int dataset[] = {14, 28, 42, 99, 56, 70};
    size_t len = sizeof(dataset) / sizeof(dataset[0]);
    int target = 99;
    int found_index = -1;

    for (size_t i = 0; i < len; i++) {
        if (dataset[i] == target) {
            found_index = (int)i;
            break; // Target found; no need to inspect remaining elements!
        }
    }

    if (found_index != -1) {
        printf("Target %d found at index %d!\n", target, found_index);
    } else {
        printf("Target %d not present in dataset.\n", target);
    }
    return 0;
}

🌲 Advanced Example: Breaking Out of Multi-Nested Loops via Goto Error Handler

#include <stdio.h>
#include <stdbool.h>

bool search_matrix(int matrix[3][3], int target) {
    bool found = false;

    // Standard 'break' only exits the INNERMOST loop!
    // To cleanly break out of nested loops, use a flag or a clean goto label:
    for (int r = 0; r < 3; r++) {
        for (int c = 0; c < 3; c++) {
            if (matrix[r][c] == target) {
                printf("Found %d at row %d, col %d!\n", target, r, c);
                found = true;
                goto search_complete; // Jumps out of BOTH nested loops cleanly!
            }
        }
    }

search_complete:
    return found;
}

int main(void) {
    int grid[3][3] = {
        {10, 20, 30},
        {40, 50, 60},
        {70, 80, 90}
    };

    search_matrix(grid, 50);
    return 0;
}

🔍 Line-by-Line Explanation

  • if (i % 2 == 0) continue;: Bypasses the printf() call for even numbers, moving directly to i++.
  • break;: Terminates array search immediately upon matching target == 99, saving CPU cycles.
  • goto search_complete;: Solves the limitation that break only exits 1 level of nesting, jumping out of both 2D loops to the label.

💻 Expected Terminal Output

Printing odd numbers from 1 to 10 (skipping evens):
1 3 5 7 9
Target 99 found at index 3!
Found 50 at row 1, col 1!

⚠️ Common Mistakes & Pitfalls

  • Expecting break to Exit Multiple Nested Loops: break only exits the single innermost loop where it is called.
  • Infinite Loops with continue in while Loops: If the counter update step (e.g. i++) is placed AFTER continue in a while loop, skipping it creates an infinite loop!

📌 Important Rules & Limitations

  • break can only be used inside loops (for, while, do-while) and switch blocks.
  • continue can only be used inside loops (not in switch statements).

🌐 Multilingual Explanation: English

break exits a loop immediately; continue skips the current iteration and advances to the next pass. Note that break only breaks out of one level of nested loops.

🌐 Multilingual Explanation: Hindi (Roman Script)

break poore loop ko turant khatam kar deta hai. continue current iteration ko skip karke agli cycle par chala jata hai. Single break sirf sabse andar wale loop ko todta hai.

🌐 Multilingual Explanation: Marathi (Roman Script)

break mule loop ekadam thambto. continue mule chaluchya iteration cha baaki code skip houn navin cycle suru hote.

❓ Practice Questions

  1. What is the difference in behavior between break and continue inside a for loop?
  2. Why does break fail to exit multi-level nested loops?
  3. In a while loop, what happens if the loop counter variable is placed after a continue statement?

🚀 Practical Coding Challenge

Write a C program named prime_search.c that loops through numbers from 1 to 50. Use continue to skip even numbers except 2, use a nested loop with break to test for primality, and print all prime numbers found.

🧭 Navigation & Breadcrumbs

← Previous: 04. Nested Loops | Module 07 Guide | Next Module: 08. Functions & Scope →