Control Flow in C Programming: If-Else, Loops, Switch and Jump Statements with Worked Examples

Learn how C chooses, repeats and redirects execution. Trace common output questions step by step, including do-while, continue, switch fall-through and dangling else.

KnowledgeGate Team

Exam prep & CS education

Updated 17 Aug 20265 min read56 views

Every C learner writes if-else and for loops early, then assumes control flow is easy. A GATE or interview question with switch fall-through, a dangling else, or a do-while can still cost a mark. Traced line by line the way the compiler executes them, each of those snippets reduces to one plain rule.

Control flow in C: sequence, selection, iteration and jump

Every C program uses three basic structures: sequence, selection and iteration. Jump statements redirect them.

Category

C keywords

What it decides

Sequence

statements and ;

order of execution

Selection

if, if-else, else-if, switch

which block runs

Iteration

while, do-while, for

how many times a block runs

Jump

break, continue, goto, return

where control goes next

Most output questions reduce to two checks: which block ran, and how many times?

Selection with if, if-else, ladders and nesting

A plain if runs its body only when the condition is non-zero.

if (score >= 40)
    printf("Pass");

An if-else chooses exactly one of two paths.

if (n % 2 == 0)
    printf("Even");
else
    printf("Odd");

In an else-if ladder, the first true branch runs and the rest are skipped. A nested if makes another decision inside a selected path.

if (n > 0)
    if (n < 10)
        printf("One digit");

An else belongs to the nearest unmatched if above it, regardless of indentation. Braces show the intended pairing. Remember that = stores a value, while == compares values.

Loops and the one that runs at least once

while checks before its body, so it can run zero times. do-while checks afterwards, so it runs at least once. for puts initialisation, condition and update together for counted repetition.

Trace this example:

int i = 10;
do {
    printf("%d ", i);
    i++;
} while (i < 5);

The body first prints 10 . Then i++ changes i to 11. The test 11 < 5 is false, so the loop stops. The exact output is one number, 10, followed by a space. A while (i < 5) version would print nothing because it tests first. The three loop forms are worked side by side in Loops in C: for, while and do-while with worked examples.

Jump statements and a complete continue trace

break exits the innermost loop or switch. continue skips the rest of an iteration. return leaves the function. goto jumps to a label, but usually makes control harder to follow.

With &&, C stops at the first false operand. With ||, it stops at the first true one. The right-hand expression may not execute.

Now trace continue inside a for loop:

int i, sum = 0;
for (i = 1; i <= 5; i++) {
    if (i % 2 == 0)
        continue;
    sum = sum + i;
}
printf("%d", sum);

i

i % 2 == 0?

Action

Sum after

1

no

sum = 0 + 1

1

2

yes

continue, skip addition

1

3

no

sum = 1 + 3

4

4

yes

continue, skip addition

4

5

no

sum = 4 + 5

9

After the fifth iteration, i++ makes i equal to 6. Since 6 <= 5 is false, the loop ends and prints exactly 9, which is 1 + 3 + 5. continue does not skip a for loop's update expression.

Flowchart of the continue loop that skips even values of i and adds the odd ones, ending with sum equal to 9.

Switch-case and the fall-through trap

A switch jumps to the matching case, then continues until break, return, or the end of the switch. A missing break causes fall-through.

int x = 2;
switch (x) {
    case 1: printf("A");
    case 2: printf("B");
    case 3: printf("C");
            break;
    case 4: printf("D");
}

Since x is 2, control jumps to case 2 and prints B. With no break, it falls into case 3 and prints C. That case's break ends the switch. The exact output is BC. case 1 is above the match, while case 4 is below the break, so neither runs.

Fall-through can be deliberate when several cases share one action. List those labels together, then put the shared statements and one break after the last label.

Switch diagram for x equal to 2 that jumps to case 2, falls through to case 3, then breaks, giving output BC.

Traps that quietly cost marks

Dangling else

Consider this code:

int a = 5, b = 10;
if (a > 0)
    if (b > 20)
        printf("X");
    else
        printf("Y");

The else binds to the nearest unmatched if, here if (b > 20). Since a > 0 is true and b > 20 is false, the inner else prints exactly Y. Use braces to create a different grouping.

Assignment, bounds and infinite loops

  • Assignment instead of comparison: if (x = 0) assigns 0, so the condition is false. if (x = 5) assigns a non-zero value, so it is true. Use == to compare. Writing if (0 == x) is one defensive style because 0 = x cannot compile.

  • Off-by-one bounds: for (i = 1; i <= n; i++) runs n times for positive n, while i < n runs n - 1 times. The wrong boundary can produce a wrong count or an array overrun.

  • Movement in the wrong direction: for (i = 5; i >= 0; i++) does not move towards termination. A continue placed before the counter update in a while loop can freeze the counter for the same reason. Check that every path moves the loop variable towards the exit condition.

For for(i=1;i<=3;i++) for(j=1;j<=i;j++) count++;, the inner body runs 1, then 2, then 3 times. Thus 1 + 2 + 3 = 6, so count becomes 6 if it starts at 0.

How GATE and interviews test control flow

Questions ask you to predict output, count nested-loop iterations, or spot assignment, boundary and missing-break bugs. Interviews may ask for a paper trace or a loop rewritten without break.

The official GATE Computer Science syllabus lists Programming and Data Structures, including C programming, as a core section. Marks, question counts and sectional details for the current cycle sit in the organising IIT's information brochure. GATE CS in 6 Months: A Realistic Study Plan shows where C sits among the other subjects and how much of your calendar it usually earns.

The same tracing skill carries into algorithm analysis. When you read the nested loops in Sorting Algorithms: Complexity, Stability, n log n Bound, you are still asking which block runs and how many times.

The short version and your next step

Sequence fixes order, selection chooses a path, and iteration repeats a block. do-while runs at least once. switch falls through without break, and else binds to the nearest unmatched if. Trace tricky snippets on paper.

To make output prediction automatic, work through a structured course with graded practice. The C Programming Course and C Language Course: Concepts, MCQs & Coding cover control flow through worked questions and coding sets.