NBC 103 • Unit 214 min read★ High Exam Frequency
Jump Statements & Control Flow: break, continue, goto & exit
Unit 2: Control Statements, Loops & Modular Functions • Problem Solving Using C
👨🏫 Professor's Mental Model: Emergency Fire Brake vs Turn-Skip in a Board Game
break ek emergency brake jaisa hai: jaise hi lagaya, gadi loop ke bahar nikal kar permanently ruk jati hai. continue ludo me 'turn skip' karne jaisa hai: aap is baari ka baaki kaam chhod kar seedha agli baari (next iteration) par jump kar jaate ho. Aur goto ek teleportation portal hai jo code ko bina kisi condition ke seedha kisi bhi doosre label par phek deta hai—lekin agar sambhal kar use na kiya jaye toh code spaghetti jaisa ulajh jata hai!
Academic Lecture Notes & Solved Study Pages
Unit 2 • Core Concepts, Step-by-Step Proofs & Notebook Solutions
NOTEBOOK PAGE 1 OF 4• Lecture Study Sheet
1. The break Statement: Loop & Switch Escape Mechanism
Purpose: Causes an immediate, early exit from the innermost enclosing loop (`for`, `while`, `do-while`) or `switch` statement.
Control Flow: Execution resumes at the statement immediately following the terminated construct.
Nested Loops: When placed inside nested loops, `break` terminates ONLY the innermost loop containing it. The outer loops continue uninterrupted.
NOTEBOOK PAGE 2 OF 4• Lecture Study Sheet
2. The continue Statement: Iteration Skip
Purpose: Bypasses the remaining statements in the current iteration of a loop and triggers the immediate start of the next iteration.
Behavior in for vs while:
- In `for` loop: Execution jumps directly to the update expression (`i++`), followed by condition test.
- In `while` / `do-while`: Execution jumps directly to the condition test expression. CAUTION: If the counter update is placed AFTER `continue`, it will be skipped, causing an INFINITE LOOP!
NOTEBOOK PAGE 3 OF 4• Lecture Study Sheet
3. The goto Statement & Structured Programming
Syntax: `goto label_name; ... label_name: statements;`
Unconditional Jump: Transfers control to a designated labeled statement within the SAME function scope.
Forward Jump: Jumping ahead over intermediate code.
Backward Jump: Jumping backward, effectively creating an unstructured loop.
Why is goto discouraged? Edsger Dijkstra's famous 1968 paper 'Go To Statement Considered Harmful' demonstrated that arbitrary jumps destroy code structure, making verification and debugging exceedingly difficult (Spaghetti Code).
Legitimate Use Case: Breaking out of deeply nested (3+ level) loops on unrecoverable hardware/file errors.
NOTEBOOK PAGE 4 OF 4• Lecture Study Sheet
4. return vs exit() from <stdlib.h>
`return`: A language keyword that terminates the currently executing function and returns a value to the calling function.
`exit(status)`: A standard library function in `<stdlib.h>` that immediately terminates the ENTIRE PROGRAM process, flushes all open file streams, and returns an exit code (`0` for success, non-zero for error) directly to the Operating System.
Master 6-Parameter Comparison: break vs continue
| Parameter | break Statement | continue Statement |
|---|---|---|
| Primary Action | Permanently terminates the loop | Terminates only the current iteration |
| Supported Constructs | Valid inside loops (for, while, do-while) AND switch | Valid ONLY inside loops (Illegal inside switch alone) |
| Next Execution Point | First statement immediately outside the loop | Advances to loop update/condition of next cycle |
| Iterations Executed | Stops all future remaining iterations | Executes remaining subsequent iterations |
| Impact on switch | Prevents fall-through to subsequent cases | Cannot be used to control switch cases |
| Keyword Syntax | break; | continue; |
Interactive Tested Code Example
#include <stdio.h>
int main(void) {
printf("=== BREAK DEMONSTRATION (Stop when 7 found) ===\n");
for (int i = 1; i <= 10; i++) {
if (i == 7) {
printf("\n[!] Hit %d -> Executing BREAK (Terminating loop)\n", i);
break;
}
printf("%d ", i);
}
printf("\n=== CONTINUE DEMONSTRATION (Skip multiples of 3) ===\n");
for (int i = 1; i <= 10; i++) {
if (i % 3 == 0) {
// Skip current iteration
continue;
}
printf("%d ", i);
}
printf("\n");
return 0;
}💡 Note:Contrasts break (early abort at 7) with continue (skipping multiples of 3: 3, 6, 9) within standard loop sequences.
🎯 University Exam Scoring Blueprint
- In university exams, always emphasize that 'continue' cannot be used inside a switch statement unless that switch is enclosed within a loop.
- Draw clean comparative flowcharts showing break exiting to the outside box, and continue looping back to the update box.
- Mention Dijkstra's structured programming principle when explaining why goto should be avoided.
Top Viva Questions on Jump Statements & Control Flow: break, continue, goto & exit
1
Can continue be used inside a switch statement?
2