1.5.15 Debugging if/else if Statements

Last Updated: 9/21/2026
Debug the Chain in the Same Order C# Evaluates It

An if/else if chain is evaluated from top to bottom.

When the wrong branch runs, inspect the conditions in that same order.

Consider:

if (goals >= 3)
{
    System.Console.WriteLine("High scoring");
}
else if (goals == 2)
{
    System.Console.WriteLine("Two goals");
}
else if (goals == 1)
{
    System.Console.WriteLine("One goal");
}
else
{
    System.Console.WriteLine("No goals");
}
Pause Before the First Condition

Set a breakpoint before or on the first if.

Inspect:

goals

Suppose:

goals = 2

Predict the entire decision path before stepping.

Evaluate Each Reached Condition

For:

goals = 2

the trace is:

goals >= 3 → false
goals == 2 → true

The "Two goals" branch should run.

The later conditions should be skipped.

Stop Tracing Once a Branch Is Selected

If the first condition is true, later conditions are not evaluated.

For:

goals = 4

the trace is simply:

goals >= 3 → true

The chain has selected its branch.

Do not continue reasoning as though later else if conditions also run.

Check the Order When a Broader Condition Captures a Value Too Early

Suppose:

if (goals >= 1)
{
    System.Console.WriteLine("At least one");
}
else if (goals >= 3)
{
    System.Console.WriteLine("High scoring");
}

and:

goals = 4

The first condition is true.

The second condition is never reached.

The debugger may show perfectly correct execution of code that does not match the intended categorization.

The defect is the branch order.

Debug Boundary Values

For:

if (goals >= 3)

test mentally:

2 → false
3 → true
4 → true

If the requirement says "at least 3," the boundary value 3 must enter the first branch.

A wrong relational operator can create a defect only at the edge.

Check Equality Branches Exactly

For:

else if (goals == 2)

inspect the actual runtime value.

If:

goals = 1

the condition is false.

If:

goals = 2

it is true.

Equality branches are exact.

Use F10 to Follow the Selected Path

Step over each reached condition.

Observe which block becomes active.

A useful debugging trace is:

runtime value
   ↓
condition result
   ↓
next condition or selected block

This makes the program's choice visible.

Distinguish Wrong Input from Wrong Conditional Logic

Suppose the requirement expects:

goals = 3

but the debugger shows:

goals = 2

The chain may correctly choose "Two goals".

The defect may be in the earlier calculation or assignment that produced the value.

Do not rewrite the conditional until you verify its input.

Distinguish Wrong Branch Selection from Wrong Branch Action

Suppose the debugger confirms:

goals = 2

and execution correctly enters:

else if (goals == 2)

but that branch displays:

"One goal"

The condition is correct.

The action inside the branch is wrong.

Debugging requires checking both parts.

Trace a Parameter That Drives the Chain

Suppose:

team1.DisplayGoalCategory(2);

calls:

public void DisplayGoalCategory(int goals)
{
    if (goals >= 3)
    {
        System.Console.WriteLine("High scoring");
    }
    else if (goals == 2)
    {
        System.Console.WriteLine("Two goals");
    }
    else
    {
        System.Console.WriteLine("Fewer than two");
    }
}

Trace:

argument 2
   ↓
parameter goals = 2
   ↓
goals >= 3 → false
   ↓
goals == 2 → true
   ↓
"Two goals"

The debugger can verify every stage.

Use the Call Stack When the Same Method Has Several Callers

If the method is called from several places, the Call Stack helps identify which caller supplied the current parameter value.

That can explain why the same chain chooses different branches at different times.

Build a Precise Explanation

A strong debugging statement is:

That explanation identifies the actual path.

Key Debugging Process
  1. Inspect the input value.
  2. Evaluate the first condition.
  3. If false, evaluate the next reached condition.
  4. Stop when a condition is true.
  5. Verify the action in the selected branch.
  6. Compare the result with the requirement.
  7. Correct the specific defect supported by the evidence.