🟢 Beginner  ·  Lesson 15

break, continue & goto

break Statement

The break statement immediately exits the nearest enclosing loop (for, while, do-while) or switch. Execution continues from the statement after the loop/switch.

C Language – break in for loop
#include <stdio.h>
int main() {
    printf("Counting: ");
    for (int i = 1; i <= 10; i++) {
        if (i == 6) break;  // Stop when i reaches 6
        printf("%d ", i);
    }
    printf("\nLoop ended\n");
    return 0;
}
Counting: 1 2 3 4 5 Loop ended

break – Search and Exit

C Language – Linear Search with break
#include <stdio.h>
int main() {
    int arr[] = {10, 25, 8, 40, 15};
    int n = 5, key = 8, found = 0;

    for (int i = 0; i < n; i++) {
        if (arr[i] == key) {
            printf("%d found at index %d\n", key, i);
            found = 1;
            break;   // No need to search further
        }
    }
    if (!found) printf("Not found\n");
    return 0;
}
8 found at index 2

break in Nested Loops

💡 break only exits the INNERMOST loop

break exits only the loop it is directly inside. In nested loops, it won't exit the outer loop. You need additional logic (flag variable or goto) to break out of multiple loops.

C Language – break exits inner loop only
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        if (j == 2) break;   // Exits INNER loop only
        printf("(%d,%d) ", i, j);
    }
    printf("\n");
}
(1,1) (2,1) (3,1)

continue Statement

The continue statement skips the rest of the current iteration and moves to the next iteration of the loop. Unlike break, it doesn't exit the loop — it just skips one cycle.

C Language – Skip Even Numbers
#include <stdio.h>
int main() {
    printf("Odd numbers 1-10: ");
    for (int i = 1; i <= 10; i++) {
        if (i % 2 == 0) continue;  // Skip even numbers
        printf("%d ", i);
    }
    printf("\n");
    return 0;
}
Odd numbers 1-10: 1 3 5 7 9

continue – Skip Divisible by 3

C Language
#include <stdio.h>
int main() {
    printf("Skip multiples of 3:\n");
    for (int i = 1; i <= 15; i++) {
        if (i % 3 == 0) {
            printf("(skip %d) ", i);
            continue;
        }
        printf("%d ", i);
    }
    printf("\n");
    return 0;
}
Skip multiples of 3: 1 2 (skip 3) 4 5 (skip 6) 7 8 (skip 9) 10 11 (skip 12) 13 14 (skip 15)

goto Statement

The goto statement jumps execution to a labeled statement elsewhere in the function. While it exists in C, it is generally avoided as it makes code hard to read and maintain (leads to "spaghetti code").

C Language – goto Syntax
#include <stdio.h>
int main() {
    int i = 1;
start:                           // Label
    if (i > 5) goto end;         // Jump to 'end' label
    printf("%d ", i);
    i++;
    goto start;                  // Jump back to 'start'
end:
    printf("\nDone!\n");
    return 0;
}
1 2 3 4 5 Done!
⚠️ Avoid goto in Practice!

The only acceptable use of goto in modern C is for error handling and cleanup in embedded/systems code — jumping to cleanup code when an error occurs in a deeply nested section. For all normal programming, use loops and break/continue instead.

Complete Programs

Prime Numbers with continue

C Language – Print Primes up to N
#include <stdio.h>
int main() {
    int n;
    printf("Enter N: ");
    scanf("%d", &n);
    printf("Primes up to %d: ", n);

    for (int num = 2; num <= n; num++) {
        int isPrime = 1;
        for (int j = 2; j * j <= num; j++) {
            if (num % j == 0) { isPrime = 0; break; }
        }
        if (!isPrime) continue;    // Skip non-primes
        printf("%d ", num);
    }
    printf("\n");
    return 0;
}
Enter N: 30 Primes up to 30: 2 3 5 7 11 13 17 19 23 29

Summary

StatementEffectUsed with
breakExits the loop/switch immediatelyfor, while, do-while, switch
continueSkips current iteration, goes to nextfor, while, do-while
goto labelJumps to labeled statementFunction scope (avoid in practice)
  • break — exits the innermost loop only (not outer loops)
  • continue — skips rest of current iteration, loop continues
  • goto — generally avoided; use only for error handling in embedded code

Frequently Asked Questions

What is the difference between break and continue in C?
break immediately exits the entire loop, so no more passes run. continue skips only the rest of the current pass and jumps to the next iteration. In short, break stops the loop, while continue skips one round.
What does the break statement do in a loop?
break ends the loop right away and moves control to the first line after the loop. It is useful when you have found what you were looking for or hit a condition that means there is no point continuing.
What does the continue statement do in C?
continue skips the remaining statements in the current iteration and goes straight to the next one. It is handy when you want to ignore certain values but keep looping, such as skipping negative numbers while processing a list.
Can break and continue be used in any loop?
Yes, both work in for, while and do-while loops. break is also used inside switch statements to end a case. In nested loops, they affect only the innermost loop that contains them.
Does break exit all loops in a nested loop?
No. A single break only exits the loop it is directly inside — the innermost one. To leave several nested loops at once you need another approach, such as a flag variable or restructuring the code, since C has no labelled break.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.