A basic if/else chooses between two paths.
Sometimes the program needs to distinguish among several possible conditions.
For example, a soccer program might classify the number of goals scored:
An if/else if chain lets the program test conditions in order.
Consider:
if (goals >= 3)
{
System.Console.WriteLine("Three or more goals.");
}
else if (goals == 2)
{
System.Console.WriteLine("Two goals.");
}
else if (goals == 1)
{
System.Console.WriteLine("One goal.");
}
else
{
System.Console.WriteLine("No goals.");
}The program does not evaluate every branch as though they were separate unrelated if statements.
It checks the conditions in sequence.
Suppose:
goals = 2Trace the chain.
goals >= 3Result:
falseContinue to the next condition.
goals == 2Result:
trueThe program runs:
System.Console.WriteLine("Two goals.");The remaining branches in the chain are skipped.
Consider:
if (goals >= 1)
{
System.Console.WriteLine("At least one goal.");
}
else if (goals >= 3)
{
System.Console.WriteLine("Three or more goals.");
}If:
goals = 4the first condition is already true:
4 >= 1So the first branch runs.
The later:
goals >= 3condition is never reached for that execution.
The source may compile, but the branch order may not represent the intended classification.
For a descending scoring classification:
if (goals >= 3)
{
System.Console.WriteLine("Three or more goals.");
}
else if (goals == 2)
{
System.Console.WriteLine("Two goals.");
}
else if (goals == 1)
{
System.Console.WriteLine("One goal.");
}
else
{
System.Console.WriteLine("No goals.");
}the earlier conditions handle the higher-priority cases first.
Each later branch applies only when all earlier conditions were false.
Think of the structure as:
Question 1?
|
+-- true → action 1
|
+-- false → Question 2?
|
+-- true → action 2
|
+-- false → Question 3?Each else if is another decision reached only after the previous condition was false.
In:
else
{
System.Console.WriteLine("No goals.");
}there is no new condition.
The final else means:
This creates a final path for the remaining values.
A chain does not always need a final else, but when one is present, that is its role.
Consider separate statements:
if (goals >= 1)
{
System.Console.WriteLine("Scored.");
}
if (goals >= 3)
{
System.Console.WriteLine("High scoring.");
}If:
goals = 4both conditions are true.
Both blocks can run.
Now compare:
if (goals >= 3)
{
System.Console.WriteLine("High scoring.");
}
else if (goals >= 1)
{
System.Console.WriteLine("Scored.");
}When the first condition is true, the second branch is skipped.
The structures represent different behavior.
A chain works well when one execution should select one category or path.
For example:
three or more goals
two goals
one goal
no goalsThese are alternate classifications of one score value.
The chain makes the exclusivity visible.
Suppose:
public void DisplayGoalCategory(int goals)
{
if (goals >= 3)
{
System.Console.WriteLine("Three or more goals.");
}
else if (goals == 2)
{
System.Console.WriteLine("Two goals.");
}
else
{
System.Console.WriteLine("Fewer than two goals.");
}
}The argument supplied by the caller becomes the parameter:
goalsThat value is tested by each condition in sequence.
Suppose:
goals = 1For:
if (goals >= 3)the result is false.
For:
else if (goals == 2)the result is false.
The program reaches:
elseand executes its block.
A strong explanation names every condition that was evaluated before the selected branch.
A chained:
if
else if
else if
elsestructure is the current focus.
Nested if statements place one decision inside another branch.
That is a different structure and is taught in CO-045.
For now, think:
rather than:
An if/else if chain is evaluated from top to bottom.
The first condition that evaluates to true selects its branch.
If none are true and a final else exists, the else branch runs.
That execution pattern is what the next activity diagram must represent.