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.
Begin with:
if (player1.isAvailable)
{
}This establishes the first required condition.
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.
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.
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.
For:
if (player1.isAvailable)
{
if (player1.position == "Goalkeeper")
{
System.Console.WriteLine("Goalkeeper is available.");
}
}read:
That sentence should match the requirement.
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.
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.
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.
Suppose the diagram shows:
Is player available?
true → Is player a goalkeeper?
true → Display messageThe 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.
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.
For the availability/position example:
outer true
inner true
message runsouter true
inner false
message skippedouter false
inner not evaluated
message skippedIf your source produces those paths, the nested structure matches the stated logic.
if (outerCondition)
{
if (innerCondition)
{
// action requiring both conditions
}
}Use nesting when the inner question should exist only after the outer condition selects that path.