Back

Activity 1.13: Chains of Decisions

divider

Activity 1.13

Chains of Decisions

Key Concepts

More Than Two Paths

First Match Wins

Unreachable Code

Two Paths Is Not Enough

Activity 1.12 gave you a decision with exactly two outcomes. Approved or refused. Warning or nominal.

A grade is not pass or fail. A storm is not calm or hurricane. Most real decisions have more than two answers.

else if Adds a Branch

int score = 85;
if (score >= 90)
{
Console.WriteLine("Grade: A");
}
else if (score >= 80)
{
Console.WriteLine("Grade: B");
}
else
{
Console.WriteLine("Grade: C or below");
}

else if Adds a Branch

Terminal window
Grade: B

One answer, out of three possible ones. You can add as many else if branches as the problem needs.

How a Chain Runs

  • Conditions are checked top to bottom
  • The first one that is true wins
  • Everything below it is skipped without even being checked
  • The final else catches whatever is left over

85 is also greater than 60, and greater than 0. Neither mattered — the chain stopped at the first match and never looked further.

Order Is Not Optional

int score = 95;
if (score >= 60)
{
Console.WriteLine("Grade: D or better");
}
else if (score >= 90)
{
Console.WriteLine("Grade: A");
}

A 95 should be an A. Predict what prints.

Order Is Not Optional

Terminal window
Grade: D or better

A 95 was reported as D or better. Both conditions were true, and the loosest one came first and swallowed everything behind it.

That second branch can never run. Not for a 95, not for a 100, not for any score at all.

C# Has a Warning for Dead Code

if (false)
{
Console.WriteLine("never");
}
Console.WriteLine("done");

This block can never run, and C# says so.

C# Has a Warning for Dead Code

Terminal window
1-13-storm.cs(3,5): warning CS0162: Unreachable code detected
done

CS0162, and it is the right warning for exactly the bug you just saw.

It did not fire on the grade chain. Not one warning. The dead branch compiled silently.

Why the Compiler Could Not Help You

false is provably never true, so C# can see the block is dead by reading it.

score >= 90 is a perfectly reachable condition. It is only dead because of the branch above it — and working that out means reasoning about what the numbers can be, which the compiler does not do.

Ordering a chain correctly is your job. Nothing is going to check it for you.

The Rule

In a chain of overlapping conditions, put the most specific first and the loosest last.

For numeric ranges that means starting at the extreme end and working inward — highest threshold first, counting down.

A Chain Is Not Three Separate Ifs

int score = 95;
if (score >= 90)
{
Console.WriteLine("A");
}
if (score >= 80)
{
Console.WriteLine("B");
}
if (score >= 60)
{
Console.WriteLine("D or better");
}

Same three conditions. No else anywhere.

A Chain Is Not Three Separate Ifs

Terminal window
A
B
D or better

Three separate if statements are three separate decisions. Every one gets checked, so every one can fire.

A chain gives you exactly one answer. Separate ifs give you any number, including none. Neither is wrong — they answer different questions, and picking the wrong one is a real bug.

Today's Objectives

  • Writing a chain with several else if branches
  • Ordering conditions so every branch is reachable
  • Recognizing unreachable code, and why CS0162 will not catch it
  • Choosing between a chain and separate ifs

Key Terms

Chain
A series of conditions where the first true one runs and the rest are skipped.
Branch
One possible path through a decision.
Unreachable code
Code that can never run, no matter what the input is.
Mutually exclusive
Outcomes where only one can happen at a time.

'F' → Fullscreen

divider

Build

Everything today goes in a new program named 1-13-storm.

How to read the code blocks. Each task's code block shows your whole program, from line 1, as it should look when that task is finished.

You never start a new file and you never delete your work. The green lines marked with a + are the ones you add or change for that task — everything else is already in your file. Do not type the + sign.

Where a block shows only comments, you write that code yourself. The comments stay in your file.


Task 1: Three Branches

  • Write the code below and run it.
  • Test all three branches by editing windSpeed: try 80, then 46, then 5. Write down what each one prints.
  • Exactly one category prints every time. That is the chain doing its job — 80 is also greater than 39, and the tropical-storm branch was never even checked.
  • Set it back to 46 before you continue.
1-13-storm
int windSpeed = 46;
Console.WriteLine($"Wind speed: {windSpeed}");
if (windSpeed >= 74)
{
Console.WriteLine("Category: hurricane");
}
else if (windSpeed >= 39)
{
Console.WriteLine("Category: tropical storm");
}
else
{
Console.WriteLine("Category: calm");
}

Task 2: The Full Scale

This task rewrites the chain, it does not add to it. Your program goes from 16 lines to 32, and the hurricane branch becomes 1 - dangerous. The two bottom branches — tropical storm and calm — are the only ones that survive unchanged.

  • Expand the chain to all seven branches.
  • Notice the order: highest threshold first, working down. That is not a style choice. Reverse it and almost every branch becomes unreachable.
  • Test at least four different speeds, including one above 157 and one below 39.
1-13-storm
int windSpeed = 46;
Console.WriteLine($"Wind speed: {windSpeed}");
if (windSpeed >= 157)
{
Console.WriteLine("Category: 5 - catastrophic");
}
else if (windSpeed >= 130)
{
Console.WriteLine("Category: 4 - extreme");
}
else if (windSpeed >= 111)
{
Console.WriteLine("Category: 3 - devastating");
}
else if (windSpeed >= 96)
{
Console.WriteLine("Category: 2 - extensive");
}
else if (windSpeed >= 74)
{
Console.WriteLine("Category: 1 - dangerous");
}
else if (windSpeed >= 39)
{
Console.WriteLine("Category: tropical storm");
}
else
{
Console.WriteLine("Category: calm");
}

Task 3: Plan, Then Classify Some Input

  • The comments describe a rainfall scale with four categories. Write the code under them, leaving the comments where they are.
  • Convert the input before you compare it. Same as Activity 1.12.
  • Exactly one category must print, so this needs a chain and not separate ifs.
  • Work out your ordering before you type it. Writing the four thresholds down on paper first is not cheating.
1-13-storm
int windSpeed = 46;
Console.WriteLine($"Wind speed: {windSpeed}");
if (windSpeed >= 157)
{
Console.WriteLine("Category: 5 - catastrophic");
}
else if (windSpeed >= 130)
{
Console.WriteLine("Category: 4 - extreme");
}
else if (windSpeed >= 111)
{
Console.WriteLine("Category: 3 - devastating");
}
else if (windSpeed >= 96)
{
Console.WriteLine("Category: 2 - extensive");
}
else if (windSpeed >= 74)
{
Console.WriteLine("Category: 1 - dangerous");
}
else if (windSpeed >= 39)
{
Console.WriteLine("Category: tropical storm");
}
else
{
Console.WriteLine("Category: calm");
}
Console.WriteLine();
// Ask the user for a rainfall total in millimeters and convert it
// 200 or more is "flood risk"
// 50 or more is "heavy"
// 10 or more is "moderate"
// anything else is "light"
// Display the rainfall, then its single category

Task 4: Kill a Branch on Purpose

Reorder your rainfall chain so that the flood risk branch can never run, no matter what the user types. You are allowed to move lines, not to delete them.

  1. Run it with a rainfall of 400 and write down what it says instead.
  2. Write down every warning the compiler gave you. If there were none, write “none” — that is the answer this task is looking for.
  3. Write a comment in your file naming which line made the branch dead, and why the program still built.
  4. Put the chain back in working order before you submit.

Challenge (Optional): Two Answers at Once

  • At the very bottom of your program, add a second copy of the rainfall chain with every else removed — so it becomes three or four separate if statements.
  • Run it with a rainfall of 400 and note how many lines print.
  • Write a comment describing a rainfall report where you would want that behavior instead of a single category.
  • Delete the extra copy before submitting.
divider

Same Idea, Exam Notation

This is the biggest difference between the two notations in the whole course — and it is the one Activity 1.12 promised you.

The exam reference sheet has no ELSE IF. It offers IF and it offers IF/ELSE, and that is all. A chain has to be built by putting a whole new IF inside the ELSE block.

C#
if (score >= 90)
{
Console.WriteLine("A");
}
else if (score >= 80)
{
Console.WriteLine("B");
}
else
{
Console.WriteLine("C");
}
Exam Notation
IF(score ≥ 90)
{
DISPLAY("A")
}
ELSE
{
IF(score ≥ 80)
{
DISPLAY("B")
}
ELSE
{
DISPLAY("C")
}
}

Read the right-hand side carefully. The second IF is not a sibling of the first — it lives inside the first one's ELSE, indented a level deeper. The closing braces stack up at the bottom.

Why This Matters on Exam Day

  • The nesting grows with every branch. Three branches means two levels. Seven branches, like your Task 2, would mean six levels and a wall of closing braces at the end.
  • Exam questions look harder than they are. When you meet a deeply indented block of IF/ELSE, it is very often just a chain, written the only way the notation allows.
  • The logic is identical. First match wins, exactly as in C#. Read it as a chain even though it is drawn as a nest.

A useful trick when you are tracing one of these: find the deepest ELSE and read it as “none of the conditions above were true.”

The ordering rule survives the translation. A nested exam-notation chain has exactly the same dead-branch problem, in exactly the same way, and the reference sheet will not warn you either. If the loosest condition is at the top, the branches underneath it are unreachable whichever notation you wrote them in.

divider

Checkpoint

Your finished program should run like this.

Example Output
Wind speed: 46
Category: tropical storm
Rainfall in mm? 64 [Enter]
Rainfall: 64
Category: heavy
  • Test your rainfall chain at 4, 25, 120, and 400. Each one should give a different category. If two of them agree, one of your branches is unreachable.
  • All three Task 1 speeds are written down, and each printed exactly one category.
  • Four different wind speeds were tested in Task 2, including one above 157 and one below 39.
  • Task 4's dead-branch result is written down, including the number of warnings the compiler gave.
  • Your rainfall chain is back in working order.
divider

Reflection

Answer the following questions before submitting your work.

  1. A score of 95 was reported as “D or better.” Explain what went wrong, and why C# gave no warning even though it has one for unreachable code.
  2. Describe a situation where you would want separate if statements instead of a chain.
  3. The exam notation has no else if, so chains are written as nested blocks. Does that change what the program does? Explain your answer.
divider

Submit

Submit four things to the dropbox:

  1. Your 1-13-storm program file, with the rainfall chain working.
  2. Your Task 1 and Task 2 test results, and one full run of the finished program.
  3. Your Task 4 result: what a rainfall of 400 reported, how many warnings you got, and which line killed the branch.
  4. Your three reflection answers.

Activity Complete