0.3.15 Reading a Simple if Statement

An if Statement Lets One Action Depend on a Condition

You have already seen Boolean values such as:

Plain text
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:

C#
if (player1.isAvailable)
{
    System.Console.WriteLine("Player is available.");
}

Read it as:

If player1.isAvailable is true, run the statement inside the braces.

If the condition is false, the statement inside the if block is skipped.

Start With the Condition

The condition appears inside parentheses:

C#
(player1.isAvailable)

At this point, player1.isAvailable already represents a Boolean value.

That means the program can evaluate it as either:

The if statement uses that result to decide whether the block should run.

The Braces Group the Conditional Work

The braces:

C#
{
    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:

Plain text
Is player1 available?
       |
      true
       |
Display "Player is available."

When the condition is false, that block does not run.

The Program Continues After the if

Consider:

C#
if (player1.isAvailable)
{
    System.Console.WriteLine("Player is available.");
}

System.Console.WriteLine("Roster review complete.");

If player1.isAvailable is true, the program displays:

Plain text
Player is available.
Roster review complete.

If player1.isAvailable is false, the first display statement is skipped, but the program still reaches:

Plain text
Roster review complete.

A simple if controls whether its block runs. It does not automatically stop the rest of the method.

Read the Code as Two Possible Paths

For this code:

C#
if (player1.isAvailable)
{
    System.Console.WriteLine("Player is available.");
}

System.Console.WriteLine("Roster review complete.");

the two paths are:

When the condition is true

Plain text
Evaluate condition
      ↓
Run the if block
      ↓
Continue after the if

When the condition is false

Plain text
Evaluate condition
      ↓
Skip the if block
      ↓
Continue after the if

Both paths eventually continue after the if.

Connect the Code to an Activity Diagram

The C#:

C#
if (player1.isAvailable)
{
    System.Console.WriteLine("Player is available.");
}

can be connected to a simple activity-diagram idea:

Plain text
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.

The Condition Does Not Need == true

Because player1.isAvailable is already Boolean, this is enough:

C#
if (player1.isAvailable)
{
    System.Console.WriteLine("Player is available.");
}

You may sometimes encounter code written as:

C#
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.

Indentation Helps Humans See the Structure

Compare:

C#
if (player1.isAvailable)
{
    System.Console.WriteLine("Player is available.");
}

with poorly arranged code:

C#
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.

Use the Debugger to Observe the Path

A breakpoint placed before the if lets you inspect the Boolean value before the decision occurs.

Suppose the debugger shows:

Plain text
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:

Plain text
player1.isAvailable = false

the program should skip the block and continue after the if.

This connects three forms of evidence:

Keep the Meaning Simple

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.