Docs
Break Continue Goto
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
- β’Creates "spaghetti code" - hard to follow
- β’Breaks structured programming
- β’Difficult to debug
- β’Can jump over variable initializations
- β’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
| Statement | Purpose | Scope | Use Case |
|---|---|---|---|
break | Exit loop/switch | Innermost loop | Early exit |
continue | Skip iteration | Current loop | Filter data |
goto | Jump to label | Function-wide | Emergency (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:
- β’Use
breakto exit loops when target is found - β’Use
continueto skip unwanted iterations - β’Keep loop logic simple and readable
- β’Consider extracting complex loops to functions
Don'ts:
- β’Don't use goto except for specific cleanup patterns
- β’Don't overuse
break- consider restructuring - β’Don't forget to update loop variables before
continuein while - β’Don't use
breakto exit from nested loops (use flags or functions)
π Key Takeaways
- β’
breakexits the innermost loop or switch immediately - β’
continueskips to the next loop iteration - β’
gotojumps to a label (avoid using it!) - β’
breakandcontinueonly affect one loop level - β’Use flags or functions instead of goto for nested loops
- β’Be careful with
continuein while loops
βοΈ Next Topic
Continue to Nested Loops to learn about loops within loops.