Switch and Jump Statements in C: Concepts, Traces and Exam Traps

Learn where switch starts execution and where break, continue, goto, and return send control. Trace a loop example step by step and correct common exam mistakes.

KnowledgeGate Team

Exam prep & CS education

Updated 28 Oct 20266 min read

Output questions become confusing when switch is treated as the whole control-flow story. A switch chooses an entry label, but break, continue, goto, and return decide where control goes next. Inside a loop, a switch transfers control according to the matching label and each jump statement's destination.

What a switch statement actually does

A switch evaluates its controlling expression once and applies the integer promotions. It compares the resulting value with the case values, then transfers control to the matching label. The controlling expression must have integer type, so integer, character, and enumeration values work. Floating-point values and strings cannot be used directly.

c
switch (expression) {
    case 1:
        statement_1;
        break;
    case 2:
        statement_2;
        break;
    default:
        default_statement;
}

case and default are labels within one statement. They are not separate conditions like the branches of an if and else if chain. Learners building the wider language foundation can use the Coding & Skills category without mixing these C rules with C++ rules.

Case matching, fall-through and default

Consider this exact trace:

c
int x = 2;
int y = 0;

switch (x) {
    case 1: y += 1;
    case 2: y += 2;
    case 3: y += 3; break;
    default: y = 99;
}

x matches case 2, so execution starts there. First, y becomes 0 + 2 = 2. There is no break, so execution falls through to case 3, where y becomes 2 + 3 = 5. The break then exits the switch. The final value is 5, not 2 or 99.

Stacked labels can make fall-through intentional. For example, case 1: immediately followed by case 3: lets both values share one body. default is optional, and a switch can have at most one. It is selected only when no case matches. Its physical position does not affect selection, though execution starting there can fall through to later statements if no jump stops it.

Every case value must be an integer constant expression. After conversion to the promoted type of the controlling expression, duplicate case values are invalid.

Left-to-right control-flow diagram for a switch example where x = 2 falls through case 3 and ends with y = 5.

The four jump statements and their exact destinations

Statement

Exact destination

break

After the innermost enclosing loop or switch

continue

The next iteration of the innermost enclosing loop

goto label;

The named label within the same function

return

The caller of the current function, optionally with a value

Nesting decides many output questions. A continue inside a switch that sits inside a loop applies to the loop, because a switch is not a loop. A break in that same position exits only the switch, not the surrounding loop.

The legal contexts matter too. break requires an enclosing loop or switch. continue requires an enclosing loop. A goto target must be in the same function. Statements after an unconditional return on the same path are not executed.

Worked example: trace switch, break and continue together

c
int total = 0;

for (int i = 1; i <= 5; i++) {
    switch (i) {
        case 1:
        case 3:
            total += i;
            break;
        case 4:
            total += 10;
            continue;
        default:
            total += 2;
    }
    total += 100;
}

printf("%d", total);

Trace every iteration instead of combining the arithmetic mentally:

i

Matched label

Calculation

Control effect

Total

1

case 1

0 + 1 + 100

break reaches after switch

101

2

default

101 + 2 + 100

Switch ends normally

203

3

case 3

203 + 3 + 100

break reaches after switch

306

4

case 4

306 + 10

continue skips +100

316

5

default

316 + 2 + 100

Switch ends normally

418

The stacked case 1 and case 3 labels share the first body. Their break transfers control to total += 100, the statement after the switch. At i = 4, continue transfers control to the loop update i++, so total += 100 does not run. The program prints 418.

Five-row program trace table showing switch changes, jumps, totals through 418, and iteration 4 bypassing the +100 operation.

goto and return without hand-waving

Start a goto trace with n = 3 and sum = 0. At label again:, execute sum += n, then test if (--n > 0) goto again;. The visits are (n, sum) = (3, 3), (2, 5), and (1, 6). The next decrement makes n = 0, the condition fails, and the printed sum is 6. This shows label transfer. It is not a reason to replace a clear loop with goto.

Now suppose classify(0) executes return 10; from case 0, while classify(5) executes return 20; from default. Then classify(0) + classify(5) = 10 + 20 = 30. return ends the whole function immediately. break ends only the nearest switch or loop.

For advanced C questions, remember one boundary: a jump must not enter the scope of an identifier with variably modified type. This is a C scope constraint, not a C++ object-lifetime rule.

Common traps and the correction for each

Trap

What the reader predicts

What C does

Smallest correction

Missing break

Only the matched body runs

Execution continues into later bodies

Trace downward to the next jump

Nested break

Every enclosing construct ends

Only the nearest loop or switch ends

Mark that construct before tracing

continue inside switch and loop

Only the switch ends

The loop starts its next iteration

Jump to the loop update or condition

default written in the middle

It runs before later cases

It is selected only if no case matches

Separate selection from physical order

Two more traps fail at compile time. case 1.5: is not a valid integer constant expression. case k: is also invalid when ordinary variable k is not an integer constant expression. In contrast, case 'A': and a case using an enumeration constant are valid.

Use one tracing discipline every time: circle the matched label, follow statements downward until a jump or the end of the switch, then identify the jump's exact target. The Control Flow in C Programming: If-Else, Loops, Switch and Jump Statements with Worked Examples overview provides the broad map across if-else, loops, switch, and jumps; focused switch questions hinge on entry selection, fall-through, and the distinct destinations of each jump.

How exam-style questions test switch and jump statements

Exam-style questions ask you to predict output with fall-through, find an invalid case label, decide which enclosing construct a jump affects, or count which later statements execute after break, continue, or return.

Try three changes to the worked program:

  • Remove the continue after total += 10. What prints? Execution falls through to default, then runs +100, so the final total is 520.

  • Replace that continue with break. What prints? The break exits the switch and +100 runs, so the final total is 518.

  • Remove the break after total += i. What prints? For i = 1 and i = 3, control falls into case 4, adds 10, then continue skips +100. The final total is 238.

Work through additional switch and jump statement traces in the Programming Languages hub.

Short version and next step

switch selects the starting label. Fall-through continues downward. break exits the nearest loop or switch. continue, goto, and return each have a different destination. In the worked loop, those rules produce 418.

Redraw the five-row trace from memory. Then change only the case 4 jump to break and justify 518 before compiling. The C Language, Concepts, MCQs and Coding Questions course is the focused next step for concept, MCQ, and coding practice. Zero to Hero, Complete CS Course is the broader CS route.