C Language Control Statements: break, continue & goto Explained with Examples

 

C Control Statements: break, continue, and goto

1 break Statement in C

Explanation:
The break statement is used to exit a loop immediately (for, while, or do-while). Once break is executed, the loop stops running, and control moves to the next statement after the loop.

Program:
#include <stdio.h>

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

Explanation:
• Loop runs from 1 to 10.
• When i == 5, the break statement executes.
• The loop stops immediately, so numbers after 4 are not printed.

2 continue Statement in C

Explanation:
The continue statement skips the current iteration of the loop and moves to the next one. Unlike break, it does not stop the loop completely—it just skips one cycle.

Program:
#include <stdio.h>

int main() {
    int i;
    for(i = 1; i <= 10; i++) {
        if(i == 5) {
            continue; // skip printing when i = 5
        }
        printf("%d ", i);
    }
    return 0;
}
Output:
1 2 3 4 6 7 8 9 10

Explanation:
• Loop runs from 1 to 10.
• When i == 5, the continue statement executes, skipping that iteration.
• All numbers are printed except 5.

3 goto Statement in C

Explanation:
The goto statement is used to jump to a labeled part of the program. It should be used carefully because too much use makes code hard to read.

Program:
#include <stdio.h>

int main() {
    int i = 1;

    start: // label
    if(i <= 5) {
        printf("%d ", i);
        i++;
        goto start; // jumps back to "start"
    }

    return 0;
}
Output:
1 2 3 4 5

Explanation:
• A label start is defined.
• The program prints numbers from 1 to 5.
• Each time, goto start; jumps back to the label, creating a loop.
• When i > 5, condition fails, and program ends.

Summary

break
Exits the loop completely
continue
Skips only the current iteration
goto
Jumps to a labeled section of code

Additional Analysis

1. Evaluate the structure alternative of goto with example

Explanation: goto jumps directly, but it makes code messy. Structured alternatives: loops (for, while, do-while) and functions.

Example using while instead of goto:
#include <stdio.h>
int main() {
    int i = 1;
    while(i <= 5) {
        printf("%d ", i);
        i++;
    }
    return 0;
}

Result: This does the same as goto but is cleaner and more readable.

2. Evaluate the importance of break statement in loops and switch

Explanation:

  • In loops: Ends loop immediately when a condition is met
  • In switch: Prevents fall-through (unwanted execution of next case)
  • Saves time by avoiding unnecessary iterations

3. Analyze the impact of break inside nested loops

Explanation: break only exits the innermost loop where it appears. Outer loops continue running.

Example:
#include <stdio.h>
int main() {
    for(int i = 1; i <= 3; i++) {
        for(int j = 1; j <= 3; j++) {
            if(j == 2) break; // breaks inner loop only
            printf("i=%d j=%d\n", i, j);
        }
    }
    return 0;
}

Result: Only the inner loop stops when j == 2, but outer loop (i) continues.

4. Analyze fall-through behavior in switch case without break

Explanation: If no break, program continues executing next cases even if condition doesn't match.

Example:
#include <stdio.h>
int main() {
    int x = 2;
    switch(x) {
        case 1: printf("One\n");
        case 2: printf("Two\n");
        case 3: printf("Three\n");
    }
    return 0;
}

Output: Two Three

Explanation: Since no break, execution "falls through" to case 3.

5. Evaluate why goto statement is discouraged in structured programming

Explanation:

  • Makes code hard to read
  • Creates "spaghetti code" (confusing jumps)
  • Hard to debug and maintain
  • Alternatives (loops, functions) are more structured and safer

6. Analyze the role of continue statement in skipping unwanted iterations

Explanation:

  • Skips current loop iteration
  • Immediately jumps to next cycle of loop
  • Useful when certain values should be ignored in processing

7. Apply continue in nested loop to skip inner loop iteration

Explanation: In nested loops, continue only affects the innermost loop.

Example:
#include <stdio.h>
int main() {
    for(int i = 1; i <= 3; i++) {
        for(int j = 1; j <= 3; j++) {
            if(j == 2) continue; // skip printing when j=2
            printf("i=%d j=%d\n", i, j);
        }
    }
    return 0;
}

Output:

i=1 j=1
i=1 j=3
i=2 j=1
i=2 j=3
i=3 j=1
i=3 j=3

Explanation: Whenever j == 2, that iteration is skipped.


Post a Comment

0 Comments