Docs

README

break, continue, and goto Statements

πŸ“– Introduction

C provides three jump statements that alter the normal flow of control: break, continue, and goto. These statements allow you to exit loops early, skip iterations, or jump to specific locations in your code.


🎯 Overview

                    Jump Statements
                          β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚                 β”‚                 β”‚
      break           continue            goto
        β”‚                 β”‚                 β”‚
   β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”
   β”‚ Exit    β”‚      β”‚ Skip to   β”‚     β”‚ Jump to   β”‚
   β”‚ loop or β”‚      β”‚ next      β”‚     β”‚ labeled   β”‚
   β”‚ switch  β”‚      β”‚ iteration β”‚     β”‚ statement β”‚
   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ”΄ The break Statement

break immediately exits the innermost loop or switch statement.

Syntax:

break;

In Loops:

for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break;  // Exit loop when i is 5
    }
    printf("%d ", i);
}
// Output: 0 1 2 3 4

Flowchart:

       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
       β”‚ i = 0           β”‚
       β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                β”‚
                β–Ό
       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
   β”Œβ”€β”€β–Ίβ”‚ i < 10?         │──No──► Exit
   β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
   β”‚            β”‚ Yes
   β”‚            β–Ό
   β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
   β”‚   β”‚ i == 5?         │──Yes──► break ──► Exit
   β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
   β”‚            β”‚ No
   β”‚            β–Ό
   β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
   β”‚   β”‚ printf(i)       β”‚
   β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
   β”‚            β”‚
   β”‚            β–Ό
   β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
   β”‚   β”‚ i++             β”‚
   β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
   β”‚            β”‚
   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Common Use Cases:

1. Search and Exit:

int target = 42;
int index = -1;
int arr[] = {10, 20, 42, 50, 60};

for (int i = 0; i < 5; i++) {
    if (arr[i] == target) {
        index = i;
        break;  // Found! No need to continue
    }
}
printf("Found at index: %d\n", index);

2. Input Validation:

while (1) {  // Infinite loop
    int input;
    printf("Enter positive number (0 to exit): ");
    scanf("%d", &input);

    if (input == 0) {
        break;  // Exit on 0
    }

    if (input > 0) {
        printf("You entered: %d\n", input);
    }
}

3. Early Termination:

// Find first divisor
int num = 100;
for (int i = 2; i <= num; i++) {
    if (num % i == 0) {
        printf("First divisor: %d\n", i);
        break;
    }
}

break Only Exits One Level:

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        if (j == 1) {
            break;  // Only exits inner loop
        }
        printf("(%d, %d) ", i, j);
    }
    printf("\n");
}
// Output:
// (0, 0)
// (1, 0)
// (2, 0)

🟑 The continue Statement

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

Syntax:

continue;

Example:

for (int i = 0; i < 10; i++) {
    if (i % 2 == 0) {
        continue;  // Skip even numbers
    }
    printf("%d ", i);
}
// Output: 1 3 5 7 9

Flowchart:

       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
       β”‚ i = 0           β”‚
       β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                β”‚
                β–Ό
       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
   β”Œβ”€β”€β–Ίβ”‚ i < 10?         │──No──► Exit
   β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
   β”‚            β”‚ Yes
   β”‚            β–Ό
   β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
   β”‚   β”‚ i even?         │──Yes──► continue ──┐
   β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜                    β”‚
   β”‚            β”‚ No                          β”‚
   β”‚            β–Ό                             β”‚
   β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                    β”‚
   β”‚   β”‚ printf(i)       β”‚                    β”‚
   β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜                    β”‚
   β”‚            β”‚                             β”‚
   β”‚            β–Ό                             β”‚
   β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                    β”‚
   β”‚   β”‚ i++             β”‚β—„β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
   β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
   β”‚            β”‚
   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

continue in Different Loop Types:

In for Loop:

for (int i = 0; i < 5; i++) {
    if (i == 2) continue;
    printf("%d ", i);
}
// Output: 0 1 3 4
// Note: i++ still executes after continue

In while Loop:

int i = 0;
while (i < 5) {
    i++;
    if (i == 3) continue;
    printf("%d ", i);
}
// Output: 1 2 4 5

⚠️ Careful with while:

int i = 0;
while (i < 5) {
    if (i == 2) {
        continue;  // BUG: i never incremented!
    }
    printf("%d ", i);
    i++;
}
// Infinite loop!

Common Use Cases:

1. Skip Invalid Data:

int data[] = {5, -1, 10, -2, 15, 0, 20};
int sum = 0;

for (int i = 0; i < 7; i++) {
    if (data[i] <= 0) {
        continue;  // Skip non-positive values
    }
    sum += data[i];
}
printf("Sum of positive: %d\n", sum);  // 50

2. Filter Processing:

char str[] = "Hello, World!";

printf("Letters only: ");
for (int i = 0; str[i] != '\0'; i++) {
    if (!((str[i] >= 'a' && str[i] <= 'z') ||
          (str[i] >= 'A' && str[i] <= 'Z'))) {
        continue;  // Skip non-letters
    }
    printf("%c", str[i]);
}
// Output: HelloWorld

πŸ”΅ The goto Statement

goto transfers control to a labeled statement. Use with extreme caution!

Syntax:

goto label;

// ... code ...

label:
    // code at label

Example:

int i = 0;

loop:
    if (i >= 5) goto end;
    printf("%d ", i);
    i++;
    goto loop;

end:
    printf("\nDone!\n");
// Output: 0 1 2 3 4
// Done!

⚠️ Why goto is Discouraged

  1. β€’Creates "spaghetti code" - hard to follow
  2. β€’Breaks structured programming
  3. β€’Difficult to debug
  4. β€’Can jump over variable initializations
  5. β€’Makes code maintenance harder

Acceptable Uses of goto:

1. Breaking Out of Nested Loops:

for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
        for (int k = 0; k < 10; k++) {
            if (condition) {
                goto found;  // Exit all loops
            }
        }
    }
}
found:
    printf("Exited all loops\n");

2. Error Handling with Cleanup:

int processFile() {
    FILE *file = fopen("data.txt", "r");
    if (!file) goto error_file;

    int *buffer = malloc(1000);
    if (!buffer) goto error_buffer;

    // Process file...

    // Success path
    free(buffer);
    fclose(file);
    return 0;

error_buffer:
    fclose(file);
error_file:
    return -1;
}

Alternatives to goto:

// Instead of goto for nested loops, use a flag:
int found = 0;
for (int i = 0; i < 10 && !found; i++) {
    for (int j = 0; j < 10 && !found; j++) {
        if (condition) {
            found = 1;
        }
    }
}

// Or extract to a function and use return:
void searchMatrix() {
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            if (condition) {
                return;  // Exit function
            }
        }
    }
}

βš–οΈ Comparison Table

StatementPurposeScopeUse Case
breakExit loop/switchInnermost loopEarly exit
continueSkip iterationCurrent loopFilter data
gotoJump to labelFunction-wideEmergency (avoid!)

πŸ“Š Control Flow Summary

Normal loop flow:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  for (init; condition; update) {            β”‚
β”‚      if (x) break;     // Exit loop now     β”‚
β”‚      if (y) continue;  // Skip to update    β”‚
β”‚      // Normal code                         β”‚
β”‚  }                                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

With goto (avoid):
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  label:                                     β”‚
β”‚      // code                                β”‚
β”‚      if (x) goto label;  // Jump to label   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🎯 Best Practices

Do's:

  1. β€’Use break to exit loops when target is found
  2. β€’Use continue to skip unwanted iterations
  3. β€’Keep loop logic simple and readable
  4. β€’Consider extracting complex loops to functions

Don'ts:

  1. β€’Don't use goto except for specific cleanup patterns
  2. β€’Don't overuse break - consider restructuring
  3. β€’Don't forget to update loop variables before continue in while
  4. β€’Don't use break to exit from nested loops (use flags or functions)

πŸ”‘ Key Takeaways

  1. β€’break exits the innermost loop or switch immediately
  2. β€’continue skips to the next loop iteration
  3. β€’goto jumps to a label (avoid using it!)
  4. β€’break and continue only affect one loop level
  5. β€’Use flags or functions instead of goto for nested loops
  6. β€’Be careful with continue in while loops

⏭️ Next Topic

Continue to Nested Loops to learn about loops within loops.

README - C Programming Tutorial | DeepML