1.5.16 Writing Nested if Statements

Last Updated: 9/21/2026
Use Nesting When a Second Decision Matters Only Inside a Selected Path

A nested if is appropriate when one question should be asked only after another condition has led to a particular branch.

Suppose the requirement says:

The second question matters only when the player is available.

Write the Outer Decision First

Begin with:

if (player1.isAvailable)
{
}

This establishes the first required condition.

Place the Second Decision Inside the Correct Block

Now add:

if (player1.isAvailable)
{
    if (player1.position == "Goalkeeper")
    {
        System.Console.WriteLine("Goalkeeper is available.");
    }
}

The position comparison is evaluated only after the availability condition is true.

Build from the Requirement One Condition at a Time

Requirement:

First question:

Does the Player have a Team object?

C#:

if (player1.team != null)
{
}

Then the second question belongs inside that branch:

if (player1.team != null)
{
    if (player1.team.name == "Wildcats")
    {
        System.Console.WriteLine("Wildcats player");
    }
}

The nested structure protects the second access by making it occur only when the Team reference is not null.

Keep Each Condition Focused

Outer condition:

Can or should the program continue into this branch?

Inner condition:

What more specific question matters inside that branch?

If both conditions ask unrelated questions, reconsider whether nesting actually represents the requirement.

Read the Structure from the Outside In

For:

if (player1.isAvailable)
{
    if (player1.position == "Goalkeeper")
    {
        System.Console.WriteLine("Goalkeeper is available.");
    }
}

read:

That sentence should match the requirement.

Indent Nested Blocks Consistently

Readable nesting:

if (player1.isAvailable)
{
    if (player1.position == "Goalkeeper")
    {
        System.Console.WriteLine("Goalkeeper is available.");
    }
}

Indentation makes the ownership of each statement visible.

Braces define the actual blocks.

Add an else Only Where the Requirement Needs One

Suppose:

Then the inner decision may need an else:

if (player1.isAvailable)
{
    if (player1.position == "Goalkeeper")
    {
        System.Console.WriteLine("Available goalkeeper");
    }
    else
    {
        System.Console.WriteLine("Available field player");
    }
}

The else belongs to the inner decision.

It is not an alternate path for the outer availability condition.

Watch Which if an else Belongs To

Braces make this easier to see:

if (player1.isAvailable)
{
    if (player1.position == "Goalkeeper")
    {
        System.Console.WriteLine("Available goalkeeper");
    }
    else
    {
        System.Console.WriteLine("Available field player");
    }
}

The else pairs with:

if (player1.position == "Goalkeeper")

because both are inside the outer block.

Translate a Nested Activity Diagram into C#

Suppose the diagram shows:

Is player available?
   true → Is player a goalkeeper?
              true → Display message

The C# structure is:

if (player1.isAvailable)
{
    if (player1.position == "Goalkeeper")
    {
        System.Console.WriteLine("Goalkeeper is available.");
    }
}

The source should preserve the same dependency between decisions.

Avoid Nesting When a Chain Is the Real Structure

If the requirement describes mutually exclusive alternate categories tested one after another, an if/else if chain may be clearer.

Nested if is for a decision that exists inside another path.

Predict Several Paths

For the availability/position example:

Available goalkeeper
outer true
inner true
message runs
Available forward
outer true
inner false
message skipped
Unavailable goalkeeper
outer false
inner not evaluated
message skipped

If your source produces those paths, the nested structure matches the stated logic.

Key Pattern
if (outerCondition)
{
    if (innerCondition)
    {
        // action requiring both conditions
    }
}

Use nesting when the inner question should exist only after the outer condition selects that path.