Switch and Jump Statements Interview Questions: C Model Answers and Output Traces

Prepare for C control-flow interviews with precise definitions, a complete score trace, common switch traps and a practical multi-loop goto example.

KnowledgeGate Team

Exam prep & CS education

Updated 27 Nov 20266 min read
Switch and Jump Statements Interview Questions: C Model Answers and Output Traces

Interviewers test these statements by asking where control enters, where it leaves, and what output follows when cases fall through. In the sequence, {1, 2, 0, 7, 3, 9, 1}, the trace reaches 52 and never processes the final 1. Definitions, exact paths, and a controlled multi-loop example expose the distinctions behind those answers. Employers may combine definitions, output prediction, debugging and design judgement.

What do switch and jump statements actually control?

What is a switch statement in C?

A switch evaluates its controlling expression once, compares the value with integral constant case labels, and transfers control to the matching label. If none matches, it reaches default when present. Statements then run in source order until the switch ends or a transfer changes the path.

The four jump statements have distinct jobs:

  • break exits the nearest enclosing loop or switch.

  • continue starts the next iteration of the nearest enclosing loop.

  • return ends the current function and may supply its result.

  • goto transfers control to a named label within the same function.

The mental model is: switch chooses an entry point, while a jump chooses an exit or destination. A matching case does not exit automatically, and cases are not independent blocks.

Can C switch on a string or floating-point value? No. After integer promotions, the expression must have an integer or enumeration type. enum Mode { READ = 1, WRITE = 2 }; enum Mode mode = WRITE; is valid, and switch (mode) enters case WRITE:. Both double rate = 2.0; switch (rate) and switch (name) where name is a char * are invalid; use string comparisons.

Can a variable be used after case? Each label must be a unique integer constant expression after conversion. enum { LIMIT = 2 }; case LIMIT: is valid. int limit = 2; case limit: and block-scope const int limit = 2; case limit: are invalid; const alone does not create a C integer constant expression.

default is optional and may appear anywhere, though last is usually clearer. Case order does not affect matching, but source order controls fall-through after entry. See Conditionals in C: if, else-if and switch with examples for the broader comparison.

Fully worked output trace: fall-through, break, continue and return

Predict the exact output and explain why the last input is ignored.

c
int route(const int code[], int n) {
    int score = 0;

    for (int i = 0; i < n; ++i) {
        switch (code[i]) {
            case 0:
                continue;
            case 1:
                score += 2;
                break;
            case 2:
                score += 3;
                /* intentional fall-through */
            case 3:
                score += 4;
                break;
            case 9:
                return score;
            default:
                score -= 1;
        }
        score += 10;
    }
    return score;
}

int code[] = {1, 2, 0, 7, 3, 9, 1};
printf("%d\n", route(code, 7));

Trace only the executed path:

Index

Value

Exact executed path

Score

i = 0

1

0 + 2 = 2; break; then 2 + 10 = 12

12

i = 1

2

12 + 3 = 15; fall through; 15 + 4 = 19; break; then 19 + 10 = 29

29

i = 2

0

continue; skip +10

29

i = 3

7

29 - 1 = 28; then 28 + 10 = 38

38

i = 4

3

38 + 4 = 42; break; then 42 + 10 = 52

52

i = 5

9

return 52; skip +10 and exit the function

52

i = 6

1

Not visited

52

The exact output is 52. Case 2 deliberately shares case 3's work. break reaches the statement after the switch, continue reaches the for iteration expression, and return goes to the caller.

Trace table showing the input sequence and scores 12, 29, 29, 38, 52, with index 6 unvisited after return 52.

Why break, continue and return are not interchangeable

What exactly does break exit? At code 1, it leaves the nearest switch, not the for, so score += 10 still runs. In general, it exits only the nearest enclosing loop or switch, never every surrounding construct.

What does continue target inside a switch within a loop? A switch is not a loop. At code 0, continue targets the for, skips score += 10, reaches ++i, and retests. In a while, it reaches the condition test; in a do-while, the bottom condition test.

Does return need a following break? No. At code 9, return score ends route; a following break is unreachable. Only return exits the function.

Is goto always wrong? Trace one controlled multi-loop exit

Can goto be reasonable in C? Structured flow is the default, but a short local jump to one same-function label can express a multi-level exit or centralised cleanup without pretending one break exits several loops.

c
int grid[3][4] = {
    {4, 8, 1, 6},
    {3, 5, 9, 2},
    {7, 0, 11, 10}
};
int r, c;

for (r = 0; r < 3; ++r) {
    for (c = 0; c < 4; ++c) {
        if (grid[r][c] == 9) goto found;
    }
}
printf("not found\n");
return;

found:
printf("%d %d\n", r, c);

The visits are 4, 8, 1, 6, 3, 5, 9. The match at r = 1, c = 2 triggers goto found, leaves both loops, and prints 1 2. A plain break would leave only the inner loop. A helper function that returns on finding 9 can be equally clear. Judge a jump by its locality, destination and ownership, not the slogan “never use goto”.

Grid scan highlighting value 9 at row 1, column 2, with a goto jump to the found label and output 1 2.

Switch and jump-statement traps interviewers use

  • Is every missing break an error? No. It causes fall-through, which may be intentional and should be clear.

  • Can two cases have the same value? No. Duplicate values violate C's switch constraints.

  • Can a runtime variable label a case? No. A case needs an integer constant expression.

  • Is default compulsory? No.

  • Can switch test strings or floating-point values? No. Use other comparisons.

Is a switch always faster than an if-else chain? No. C defines behaviour, not an O(1) promise. A compiler may use comparisons, a jump table or another translation. Use switch for equality against constant labels, and if or else if for ranges and compound predicates.

For maintainability, brace declarations when cases need separate local names. Mark intentional fall-through per project compiler and style rules; omit dead break statements after unconditional return. Validate the C snippet before predicting output. An invalid case label needs a diagnostic, not a runtime guess.

How fundamentals-first and depth-oriented interviews probe control flow

Prepare in three rungs. Fundamentals: define switch, state what break exits, and whether default is compulsory. Trace: reproduce 0 -> 12 -> 29 -> 29 -> 38 -> 52. Depth: explain why case 0 reaches ++i, why labels must be constant, why switch has no speed guarantee, and when local goto is clearer than nested-loop flags.

Use four passes:

  1. Mark the nearest enclosing loop, switch and function.

  2. Identify the selected case and any fall-through.

  3. Draw each break, continue, return or goto destination.

  4. Update values only on the executed path.

The Loops and Iteration Interview Questions and Answers set handles iteration counts and nested-loop termination, while switch questions centre on entry labels, fall-through and jump destinations. Use the four passes above to connect the two topics.

Switch and jump statements in 60 seconds, then the next step

Say this checksum: switch evaluates once to select an entry label; cases need unique integer constant expressions; fall-through follows source order; break exits the nearest loop or switch; continue advances the nearest loop; return exits the function; goto reaches a same-function label.

Without notes, reproduce 52, 0 -> 12 -> 29 -> 29 -> 38 -> 52, skipped index 6, and grid result (r, c) = (1, 2). Use the C Language Course for structured concept-and-code practice, or Placement Preparation & Coding for wider preparation.