🟢 Beginner  ·  Lesson 15

break, continue & goto

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

break Statement

break statement nearest enclosing loop (for, while, do-while) या switch को immediately exit करता है। Execution loop/switch के बाद वाले statement से continue होती है।

C Language
for (int i = 1; i <= 10; i++) {
    if (i == 6) break;  // i=6 पर loop exit
    printf("%d ", i);
}
1 2 3 4 5

Search में break

C Language
int arr[] = {10, 25, 8, 40, 15};
int key = 8;
for(int i=0; i<5; i++) {
    if(arr[i] == key) {
        printf("%d index %d par mila\n", key, i);
        break;   // मिल गया, आगे ढूंढने की ज़रूरत नहीं
    }
}
8 index 2 par mila
💡 break केवल INNERMOST loop exit करता है

Nested loops में break सिर्फ उस loop को exit करता है जिसके अंदर है। Outer loop को exit करने के लिए flag variable या अतिरिक्त logic चाहिए।

continue Statement

continue statement current iteration का बाकी हिस्सा skip करता है और loop की अगली iteration पर जाता है। break के विपरीत, यह loop exit नहीं करता — बस एक cycle skip करता है।

C Language – Odd Numbers Print
printf("Odd numbers: ");
for (int i = 1; i <= 10; i++) {
    if (i % 2 == 0) continue;  // Even numbers skip
    printf("%d ", i);
}
Odd numbers: 1 3 5 7 9

continue – Prime Numbers

C Language
int n = 20;
printf("Prime numbers: ");
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;   // Non-prime skip
    printf("%d ", num);
}
Prime numbers: 2 3 5 7 11 13 17 19

goto Statement

goto execution को function के किसी labeled statement पर jump करता है। Modern C में इसे generally avoid किया जाता है।

⚠️ goto से बचें!

goto code को "spaghetti code" बना देता है — hard to read, debug और maintain। इसकी जगह loops, break और continue use करें।

Programs

C Language – Password Attempts with break
#include <stdio.h>
#include <string.h>
int main() {
    char correct[] = "codeKaFunda";
    char input[50];
    int attempts = 0;

    while (attempts < 3) {
        printf("Password daalen: ");
        scanf("%s", input);
        attempts++;
        if (strcmp(input, correct) == 0) {
            printf("Sahii! Swagat hai!\n");
            break;                // Sahi password mila, loop exit
        }
        printf("Galat! %d/3 attempts\n", attempts);
    }
    if (attempts == 3) printf("Account lock!\n");
    return 0;
}

सारांश

StatementEffectकिसके साथ
breakLoop/switch को immediately exit करता हैfor, while, do-while, switch
continueCurrent iteration skip, अगली iteration परfor, while, do-while
goto labelLabeled statement पर jump करता हैFunction scope (avoid करें)
  • breakinnermost loop को ही exit करता है, outer को नहीं
  • continue — current iteration के बाकी code को skip करता है, loop चलती रहती है
  • goto — generally avoid करें; loops और break/continue use करें
← Back to C Tutorial