If-Else Statements in C: Basic, Nested and Advanced Control Flow with Worked Traces

Learn how C chooses a branch, then trace ladders, nested decisions, short-circuit expressions and common output traps with exact values.

KnowledgeGate Team

Exam prep & CS education

Updated 26 Oct 20266 min read

Memorising the syntax of if is easy. The real difficulty is tracing exactly one execution path through overlapping conditions, nested blocks and short-circuit expressions without mentally running code that C actually skips. Exact branch boundaries, one complete nested trace and short-circuit output traps reveal what C evaluates, what it skips and why. Mark boundaries, follow branch order and record side effects to predict outputs confidently.

If-else statements in C: how a condition controls one path

In C, a condition is a scalar expression. It is false when its value is 0 and true when its value is any non-zero number. Negative values are therefore true too:

c
int n = -3;
if (n)
    puts("true");

n evaluates to -3, which is non-zero, so the program prints true. C does not require the condition to equal 1. That distinction matters in output traces.

A plain if may run one body. An if-else chooses exactly one of two bodies. An if-else-if-else ladder tests from top to bottom and selects only the first true branch. Braces define which statements belong to a body; indentation only helps the reader and has no control-flow meaning. For the broader route from language basics to data structures, use the Coding & DSA category.

Basic if, if-else and else-if ladders with exact boundaries

Use if for an independent action, if-else for an either-or choice, and a ladder for a multi-way choice:

c
int score = 68;
if (score >= 75)
    puts("Distinction");
else if (score >= 60)
    puts("First division");
else if (score >= 50)
    puts("Second division");
else
    puts("Reappear");

The first comparison, 68 >= 75, is false. The second, 68 >= 60, is true, so the output is First division. C skips the >= 50 test and the final else because the ladder has already selected a branch.

Input

Output

49

Reappear

50

Second division

59

Second division

60

First division

74

First division

75

Distinction

The thresholds must run from highest to lowest. If score >= 50 came first, even score = 82 would be captured too early as Second division. Use Control Flow in C Programming: If-Else, Loops, Switch and Jump Statements with Worked Examples when you need the full map across sequence, selection, iteration, switch and jumps. Use the boundary table and traces here when the problem is specifically an if-else ladder, nested maximum or short-circuit side effect.

Flowchart of the score ladder for score = 68, with the No then Yes path highlighted through to Print First division.

Nested if-else in C: find the largest of three values step by step

Nesting means that a new decision occurs inside a branch selected by an earlier decision. This program finds the largest of three values:

c
int a = 7, b = 12, c = 9, max;

if (a > b) {
    if (a > c)
        max = a;
    else
        max = c;
} else {
    if (b > c)
        max = b;
    else
        max = c;
}

printf("%d", max);

Start at the outer decision. The test a > b becomes 7 > 12, which is false. Execution therefore enters the outer else; the whole branch that compares a with c is skipped.

Inside the chosen branch, b > c becomes 12 > 9, which is true. The program assigns max = b, so max becomes 12. It does not reach the alternative assignment max = c. Finally, printf reads the assigned value and outputs 12.

Step

Test

Result

Next action

1

a > b (7 > 12)

false

outer else

2

b > c (12 > 9)

true

max = 12

3

printf

12

stop

Notice that the trace contains only two comparisons. A nested program may show several possible assignments on the page, but a particular input follows one connected path. Marking that path before calculating values prevents you from mixing mutually exclusive branches.

Decision tree for a = 7, b = 12, c = 9 highlighting a > b false, then b > c true, reaching max = b = 12 and Output 12.

Advanced if-else behaviour: short-circuiting, order and the dangling else

Logical && evaluates left to right and stops as soon as the whole expression must be false:

c
int denominator = 0, numerator = 18;
if (denominator != 0 && numerator / denominator > 2)
    puts("high");
else
    puts("safe");

The first operand, denominator != 0, is false. C never evaluates 18 / 0, and the program prints safe. Replacing && with bitwise & would not provide this logical short-circuit behaviour. This difference is a safety issue.

Now consider an else without braces:

c
int a = 5, b = 8;
if (a > 0)
    if (b < 5)
        puts("A");
    else
        puts("B");

An else binds to the nearest unmatched if. Here, 5 > 0 is true and 8 < 5 is false, so the output is B. Braces make the intended ownership explicit, whether the else should pair with the inner or outer decision. Order also remains decisive: in the earlier ladder, putting score >= 50 first makes score = 82 stop there, even though a later threshold is more specific.

If-else mistakes in C: predict the wrong output before fixing it

These mistakes look small but change the executed path:

  • Assignment instead of comparison: int n = 6; if (n = 0) puts("zero"); else printf("%d", n); assigns 0 to n. The condition is false, so the program prints 0. Use n == 0 and keep compiler warnings enabled.

  • Stray semicolon: in int n = 4; if (n < 0); puts("negative");, the semicolon is an empty controlled statement. puts is outside the if, so negative prints unconditionally. Remove the semicolon and add braces.

  • Misleading indentation: with x = -1, the fragment if (x > 0) y = 1; z = 2; skips y = 1 but still runs z = 2. If both assignments belong to the condition, put both inside braces.

Range gaps, duplicate tests and reversed threshold order need boundary testing rather than visual inspection. For the score ladder, run 49, 50, 59, 60, 74 and 75. Those six values test immediately below and exactly at every threshold, making a missing or wrongly ordered range immediately visible.

If-else exam patterns: output tracing, side effects and branch matching

Common task shapes include predicting output, matching an else to its if, tracing a ladder, finding an unreachable branch and accounting for short-circuit side effects. The C Programming for Teaching CS Exams: Key Concepts guide connects these patterns to the wider exam-facing C syllabus.

Trace this expression from left to right:

c
int x = 4, y = 7;
if (x++ > 4 && ++y > 7)
    puts("T");
else
    printf("%d %d", x, y);

The comparison uses the old value of x, so 4 > 4 is false. The post-increment still leaves x = 5. Because the left operand of && is false, ++y is skipped and y remains 7. The condition is false, so the output is 5 7.

Use this scratch method:

  1. Mark braces and else ownership.

  2. Substitute current values into the first test.

  3. Evaluate left to right, stopping where short-circuiting requires.

  4. Record side effects before moving to the next statement.

If-else statements in C: the short version and next practice step

Write mutually exclusive ranges, place specific or high-threshold cases first, brace nested bodies, and test just below, at and above every boundary. While tracing, follow only the branch that executes and record side effects immediately. The nested maximum here is 12; the short-circuit side-effect output is 5 7.

For C control flow sequenced inside GATE CS preparation, continue with GATE Guidance by Sanchit Sir. For C alongside placement-oriented programming practice, use Coding For Placements. In either route, solve the same trace again with values changed at one boundary before moving on.