C
Jump Statements
Lecture 11: break, continue, goto
Control Flow Alteration

Jump Statements in C

C provides three primary jump statements — break, continue, and goto — that unconditionally transfer execution to another statement, altering standard loop or branching flow.

What happens when a break statement runs inside a nested loop?
jump_overview.cControl Jump Flow
for (int i = 1; i <= 10; i++) { if (i == 3) continue; // Skip iteration 3 if (i == 7) break; // Exit loop completely printf("%d ", i); } // Output: 1 2 4 5 6
Early Loop Exit

The break Statement

The break statement terminates the enclosing loop (`for`, `while`, `do-while`) or `switch` block immediately. Execution jumps to the statement following the loop body.

Interactive Search Lab: Array Linear Search

Search for a target value in array [15, 28, 42, 73, 91]:

[0]15
[1]28
[2]42
[3]73
[4]91
Enter a number and click run.
linear_search.cEarly Exit Mechanics
int arr[] = {15, 28, 42, 73, 91}; int target = 42; for (int i = 0; i < 5; i++) { if (arr[i] == target) { printf("Found at index %d!", i); break; // STOP! Saves checking remaining items } }
Iteration Bypass

The continue Statement

The continue statement skips the remainder of the current loop iteration and jumps directly to the update expression (in `for` loops) or condition evaluation (in `while` loops).

Interactive Data Filter Lab

Sum only positive numbers from array [10, -5, 20, -8, 30]:

[0]10
[1]-5
[2]20
[3]-8
[4]30
Click run to watch `continue` skip negative values.
data_filter.cIteration Bypass
int nums[] = {10, -5, 20, -8, 30}; int sum = 0; for (int i = 0; i < 5; i++) { if (nums[i] < 0) { continue; // Skip negative number, move to i++ } sum += nums[i]; // Executed ONLY for positive numbers }
Comparative Analysis

break vs continue at a Glance

Understanding how `break` and `continue` alter execution flow inside loop structures.

🛑

The `break` Statement

Action: Terminates the loop completely and exits immediately.

for (int i = 1; i <= 5; i++) { if (i == 3) break; printf("%d ", i); } // Output: 1 2

Loop stops at 3. Iterations 3, 4, 5 are NEVER executed.

⏭️

The `continue` Statement

Action: Skips only the current iteration and moves to the next cycle.

for (int i = 1; i <= 5; i++) { if (i == 3) continue; printf("%d ", i); } // Output: 1 2 4 5

Only iteration 3 is skipped. Iterations 4 and 5 execute normally.

Unconditional Transfer

The goto Statement & Statement Labels

The goto statement transfers execution unconditionally to a specified statement label within the same function.

🏷️

Label Definition Syntax

A label is an identifier followed by a colon (label_name:). The goto statement jumps directly to it.

int age = 15; if (age < 18) goto underage; printf("Welcome!"); return 0; underage: printf("Access Denied!");
🔄

Forward vs Backward Jumps

Jumping forward skips intervening statements. Jumping backward creates custom loop structures (which can cause infinite loops!).

int count = 1; start_loop: printf("%d ", count++); if (count <= 5) goto start_loop;
⚠️

Label Scope Rule

A label is visible throughout the entire function in which it is defined, but cannot be jumped to from a different function!

// Invalid: Jumping across functions void funcA() { goto label_B; } void funcB() { label_B: return; }
Practical Applications

Escaping Nested Loops with goto

A single `break` statement only exits the innermost loop. `goto` provides a clean way to jump out of deeply nested loops when an error or target condition occurs.

Approach A: Multiple Flags (Complex)

int found = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (matrix[i][j] == target) { found = 1; break; // Breaks j loop only! } } if (found) break; // Must check flag in i loop! }

Approach B: `goto` Label Exit (Clean)

for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (matrix[i][j] == target) { goto found_target; // Jumps out of BOTH loops! } } } found_target: printf("Target located cleanly!");
Knowledge Check

Test Your Jump Statements Mastery

Question 1 of 5
Loading question...