if Statement Lets One Action Depend on a ConditionYou have already seen Boolean values such as:
isAvailable = true
and you have used decision nodes in UML activity diagrams to show that a process can follow a path only when a condition is satisfied.
C# uses an if statement to express that same basic idea in code.
A simple instructional soccer example is:
if (player1.isAvailable)
{
System.Console.WriteLine("Player is available.");
}
Read it as:
If
player1.isAvailableis true, run the statement inside the braces.
If the condition is false, the statement inside the if block is skipped.
The condition appears inside parentheses:
(player1.isAvailable)
At this point, player1.isAvailable already represents a Boolean value.
That means the program can evaluate it as either:
truefalseThe if statement uses that result to decide whether the block should run.
The braces:
{
System.Console.WriteLine("Player is available.");
}
group the statement or statements controlled by the if.
In this example, the display statement belongs to the true path.
Conceptually:
Is player1 available?
|
true
|
Display "Player is available."
When the condition is false, that block does not run.
ifConsider:
if (player1.isAvailable)
{
System.Console.WriteLine("Player is available.");
}
System.Console.WriteLine("Roster review complete.");
If player1.isAvailable is true, the program displays:
Player is available.
Roster review complete.
If player1.isAvailable is false, the first display statement is skipped, but the program still reaches:
Roster review complete.
A simple if controls whether its block runs. It does not automatically stop the rest of the method.
For this code:
if (player1.isAvailable)
{
System.Console.WriteLine("Player is available.");
}
System.Console.WriteLine("Roster review complete.");
the two paths are:
Evaluate condition
↓
Run the if block
↓
Continue after the if
Evaluate condition
↓
Skip the if block
↓
Continue after the if
Both paths eventually continue after the if.
The C#:
if (player1.isAvailable)
{
System.Console.WriteLine("Player is available.");
}
can be connected to a simple activity-diagram idea:
Decision: Is the player available?
├─ [yes] → Display "Player is available."
└─ [no] → Continue
The notation is different, but the reasoning is the same:
Evaluate a condition → follow the path that applies
The activity diagram makes the flow visual.
The C# makes the behavior executable.
== trueBecause player1.isAvailable is already Boolean, this is enough:
if (player1.isAvailable)
{
System.Console.WriteLine("Player is available.");
}
You may sometimes encounter code written as:
if (player1.isAvailable == true)
{
System.Console.WriteLine("Player is available.");
}
For a Boolean value, the extra comparison is not needed to express the idea.
At this stage, the shorter form keeps the condition easier to read.
Compare:
if (player1.isAvailable)
{
System.Console.WriteLine("Player is available.");
}
with poorly arranged code:
if (player1.isAvailable)
{
System.Console.WriteLine("Player is available.");
}
The compiler uses C# syntax to determine structure, but consistent indentation makes the controlled statements easier for people to recognize.
When reading unfamiliar code, use both the braces and the indentation to understand which statements belong to the if.
A breakpoint placed before the if lets you inspect the Boolean value before the decision occurs.
Suppose the debugger shows:
player1.isAvailable = true
You can predict that the block should run.
Using F10 lets you step through the decision and see which statement Visual Studio selects next.
If the debugger instead shows:
player1.isAvailable = false
the program should skip the block and continue after the if.
This connects three forms of evidence:
A basic if statement answers one question:
Should this block run?
If the condition is true, yes.
If the condition is false, no.
Later programming work will use more complicated conditions and several connected decision structures. For now, focus on reading one Boolean condition and tracing the one block it controls.