A program does not always execute the same statements every time.
Sometimes the next action depends on a condition.
For example, a soccer program might need to decide:
or:
C# can use an if statement or an if/else statement to represent that kind of decision.
Consider:
if (player1.isAvailable)
{
System.Console.WriteLine("Player is available.");
}The Boolean expression is:
player1.isAvailableThe program evaluates that expression.
If it is:
truethe statement inside the braces runs.
If it is:
falsethe block is skipped.
Conceptually:
evaluate condition
↓
true?
/ \
yes no
| |
run skip
block blockA useful reading order is:
condition
↓
decision
↓
actionFor:
if (player1.isAvailable)
{
System.Console.WriteLine("Player is available.");
}read:
The condition controls whether the action occurs.
Now consider:
if (player1.isAvailable)
{
System.Console.WriteLine("Player is available.");
}
else
{
System.Console.WriteLine("Player is unavailable.");
}This decision has two possible outcomes.
Player is available.is displayed.
Player is unavailable.is displayed.
Only one of the two blocks runs for that evaluation.
The first condition is:
player1.isAvailableThe else block means:
There is no second Boolean expression after:
elsein a basic if/else structure.
if (player1.isAvailable)
{
System.Console.WriteLine("Player is available.");
}Question answered:
Possible result:
if (player1.isAvailable)
{
System.Console.WriteLine("Player is available.");
}
else
{
System.Console.WriteLine("Player is unavailable.");
}Question answered:
One path runs or the other path runs.
Suppose:
player1.position = "Goalkeeper";A condition can compare that field:
if (player1.position == "Goalkeeper")
{
System.Console.WriteLine("Use goalkeeper instructions.");
}
else
{
System.Console.WriteLine("Use field-player instructions.");
}If the current position is exactly:
Goalkeeperthe first block runs.
Otherwise, the else block runs.
Suppose:
team1.score = 3and:
if (team1.score >= 3)
{
System.Console.WriteLine("Three or more goals.");
}
else
{
System.Console.WriteLine("Fewer than three goals.");
}The comparison:
team1.score >= 3produces the Boolean result used by the decision.
The if/else structure then chooses one of the two paths.
Suppose a Player may or may not have a Team reference.
A decision can ask:
if (player1.team == null)
{
System.Console.WriteLine("No team assigned.");
}
else
{
System.Console.WriteLine("Team assigned.");
}The condition does not create or remove a Team object.
It only inspects the current reference state.
Suppose:
player1.isAvailable = falseand the code is:
if (player1.isAvailable)
{
System.Console.WriteLine("Available");
}
else
{
System.Console.WriteLine("Unavailable");
}Reason through it:
player1.isAvailable
↓
false
↓
skip if block
↓
run else blockExpected output:
UnavailableThis code:
if (player1.isAvailable)reads the current Boolean value.
It does not automatically change:
isAvailableA separate assignment would be needed to change the field.
The condition observes state.
The statements inside a branch may change state only if they contain assignments or other state-changing behavior.
Consider:
if (player1.isAvailable)
{
System.Console.WriteLine("Available");
}
else
{
System.Console.WriteLine("Unavailable");
}
System.Console.WriteLine("Roster reviewed.");Exactly one decision branch runs.
Afterward:
Roster reviewed.is displayed regardless of which branch was selected.
The program continues after the complete if/else structure.
For a basic if/else, the mental model is:
condition
|
+------+------+
| |
true false
| |
if block else block
| |
+------+------+
|
continueThat two-path model prepares you to represent the same decision visually in a UML activity diagram.