How break works
break causes immediate exit from the nearest enclosing for, while, do-while, or switch statement. Execution continues at the first statement after the loop or switch. It does not exit nested loops all at once — only the innermost one containing it.
Common uses: stop searching once a target is found, exit a menu loop on quit, terminate an infinite loop on a condition, and end a switch case to prevent fall-through.
for (init; condition; update) {
// ... statements ...
if (someCondition)
break; ← exits the for loop, goes to A
}
/* A: execution continues here after break */
for loop walks every element. The moment arr[i] == target, we save the index and fire break — the loop stops immediately, even if half the array hasn't been scanned yet. Without break, the loop would needlessly continue through all remaining elements.
#include <stdio.h> int main() { int rolls[] = { 101, 104, 107, 110, 113, 116, 119, 122 }; int n = sizeof(rolls) / sizeof(rolls[0]); int target = 113; int found = -1; printf("Searching for roll %d...\n", target); for (int i = 0; i < n; i++) { printf(" Checking index %d → %d", i, rolls[i]); if (rolls[i] == target) { found = i; printf(" ✓ MATCH!\n"); break; /* stop — no need to scan further */ } printf("\n"); } /* Execution resumes HERE after break */ if (found != -1) printf("\nRoll %d found at index %d.\n", target, found); else printf("\nRoll %d not found.\n", target); return 0; }
Searching for roll 113... Checking index 0 → 101 Checking index 1 → 104 Checking index 2 → 107 Checking index 3 → 110 Checking index 4 → 113 ✓ MATCH! Roll 113 found at index 4.
break only exits the innermost enclosing loop. If you have a for inside a while, break inside the for exits the for only — the while keeps running. To exit all nested loops, use a flag variable or goto (see Part 3).How continue works
continue skips the rest of the current iteration of the nearest enclosing loop. In a for loop, it jumps to the update expression (e.g. i++) then re-checks the condition. In a while or do-while, it jumps directly to the condition check.
The loop itself does not exit — it continues running; only the remaining code in that one iteration is skipped. Use continue to filter or ignore certain values without deeply nesting your logic.
if (skipCondition)
continue; ← jumps to i++ then condition check
/* Everything below is SKIPPED for this iteration */
doWork();
}
i % 2 != 0), we fire continue — this skips the printf and the square calculation entirely and jumps straight to i++ and then the condition check. Only even numbers ever reach the printf statements.
#include <stdio.h> int main() { int sumEvens = 0; printf("Even numbers from 1 to 15 and their squares:\n\n"); printf(" %-6s %-8s\n", "n", "n²"); printf(" %s\n", "---------------"); for (int i = 1; i <= 15; i++) { if (i % 2 != 0) continue; /* odd → skip rest, go to i++ */ /* Only even numbers reach here */ printf(" %-6d %-8d\n", i, i * i); sumEvens += i; } printf(" %s\n", "---------------"); printf("\nSum of evens (2+4+…+14) = %d\n", sumEvens); printf("Formula check n(n+2)/2 = %d × %d / 2 = %d\n", 7, 8, 7*8/2); /* 7 even numbers, max=14 */ return 0; }
Even numbers from 1 to 15 and their squares: n n² --------------- 2 4 4 16 6 36 8 64 10 100 12 144 14 196 --------------- Sum of evens (2+4+…+14) = 56 Formula check n(n+2)/2 = 7 × 8 / 2 = 28
continue vs break. continue keeps the loop running but skips this iteration's remaining code. break kills the loop entirely. Think of continue as "next please" and break as "I'm done here".How goto works
goto labelName transfers execution unconditionally to the line marked labelName: (identifier + colon) anywhere within the same function. The jump can be forward (skip code below) or backward (create a loop). Unlike break and continue, goto is not restricted to loops.
Modern accepted uses: (1) escape nested loops — one goto exits all levels at once where multiple breaks would be needed; (2) centralised error cleanup — jump to a single cleanup label at the end of a function to free resources. For everything else, use if/while/for/return.
statement;
goto labelName; ← jumps to label immediately
/* Rules: • label and goto must be in the SAME function • can jump forward (skip code) or backward (loop) • cannot jump INTO a variable declaration scope • label needs a statement after ':' — use ';' if nothing */
for loops scan row by row. When the target is found, a single goto found exits both loops instantly and jumps to the result-printing label. Without goto, we would need either a flag variable checked after each inner loop, or a wrapper function with return. The goto is the cleanest option here.
#include <stdio.h> #define ROWS 3 #define COLS 4 int main() { int grid[ROWS][COLS] = { { 5, 12, 19, 7 }, { 33, 48, 2, 61 }, { 14, 77, 29, 9 } }; int target = 48; int foundR = -1, foundC = -1; printf("Grid:\n"); for (int r = 0; r < ROWS; r++) { printf(" "); for (int c = 0; c < COLS; c++) printf("%4d", grid[r][c]); printf("\n"); } printf("\nSearching for %d...\n", target); for (int r = 0; r < ROWS; r++) { for (int c = 0; c < COLS; c++) { printf(" Checking [%d][%d] = %d\n", r, c, grid[r][c]); if (grid[r][c] == target) { foundR = r; foundC = c; goto found; /* EXIT both loops! */ } } } found: /* both loops jump here */ if (foundR != -1) printf("\nFound %d at grid[%d][%d]!\n", target, foundR, foundC); else printf("\n%d not found.\n", target); return 0; }
Grid:
5 12 19 7
33 48 2 61
14 77 29 9
Searching for 48...
Checking [0][0] = 5
Checking [0][1] = 12
Checking [0][2] = 19
Checking [0][3] = 7
Checking [1][0] = 33
Checking [1][1] = 48
Found 48 at grid[1][1]!
goto for ordinary loops or if/else. Every normal while loop is just a backward goto — but that doesn't mean you should write loops with goto. Use structured constructs (while, for, if, return) for all normal control flow. Reserve goto only for the two accepted patterns: nested-loop exit and error-cleanup at the bottom of a function.| Keyword | What it does | Works in | Destination | Best use |
|---|---|---|---|---|
| break | Exits the enclosing loop or switch immediately | for · while · do-while · switch | Statement after the loop/switch | Stop searching when target found; end a switch case |
| continue | Skips rest of current iteration; loop continues | for · while · do-while | Update expression (for) or condition check (while) | Filter/skip unwanted values without deep nesting |
| goto | Jumps unconditionally to a labelled point | Anywhere in function | Any label in the same function (forward or back) | Exit nested loops; centralised error cleanup |
continue in while. In a while loop, if you continue before updating the loop variable, the condition is checked again with the same value — infinite loop! Always ensure the update expression runs before continue fires:while (i < n) { if (skip) { i++; continue; } /* work */ i++; }- break — exits the nearest enclosing
for/while/do-while/switchimmediately. Execution resumes at the statement after the closing brace. Only exits one level — not all nested loops. - continue — skips the rest of the current iteration and jumps to the update (for) or condition (while). The loop itself keeps running. Never exits the loop. Classic use: filter / skip unwanted values.
- continue in while — danger! If the loop counter update comes after
continue, it gets skipped too, causing an infinite loop. Always update the counter before firingcontinueinside awhile. - goto — jumps unconditionally to a label (
name:) in the same function. Can go forward or backward. Two accepted uses only: (1) exit nested loops, (2) centralised error/resource cleanup at function end. - break vs continue —
break= "I'm done with this loop".continue= "skip this item, give me the next one".goto= "jump to this exact spot in the code, no questions". - Nested loops + break —
breakexits only the innermost loop. To escape multiple levels cleanly, use a flag variable, wrap in a function andreturn, or usegotowith a label placed after all the loops.