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.
#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; }
break – Search and Exit
#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; }
break in Nested Loops
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.
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"); }
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.
#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; }
continue – Skip Divisible by 3
#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; }
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").
#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; }
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
#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; }
Summary
| Statement | Effect | Used with |
|---|---|---|
break | Exits the loop/switch immediately | for, while, do-while, switch |
continue | Skips current iteration, goes to next | for, while, do-while |
goto label | Jumps to labeled statement | Function scope (avoid in practice) |
break— exits the innermost loop only (not outer loops)continue— skips rest of current iteration, loop continuesgoto— generally avoided; use only for error handling in embedded code
Frequently Asked Questions
What is the difference between break and continue in C?
break immediately exits the entire loop, so no more passes run. continue skips only the rest of the current pass and jumps to the next iteration. In short, break stops the loop, while continue skips one round.What does the break statement do in a loop?
break ends the loop right away and moves control to the first line after the loop. It is useful when you have found what you were looking for or hit a condition that means there is no point continuing.What does the continue statement do in C?
continue skips the remaining statements in the current iteration and goes straight to the next one. It is handy when you want to ignore certain values but keep looping, such as skipping negative numbers while processing a list.Can break and continue be used in any loop?
for, while and do-while loops. break is also used inside switch statements to end a case. In nested loops, they affect only the innermost loop that contains them.Does break exit all loops in a nested loop?
break only exits the loop it is directly inside — the innermost one. To leave several nested loops at once you need another approach, such as a flag variable or restructuring the code, since C has no labelled break.