1.5.12 Writing if and if/else Statements

Begin with the Requirement

Before writing syntax, identify the decision the program must make.

Example requirement:

That contains:

  • a Boolean condition;
  • an action that occurs when the condition is true.
Write a Simple if

Suppose Player contains:

public bool isAvailable;

A matching decision is:

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

The condition goes inside parentheses.

The controlled statements go inside braces.

Write from Meaning to Code

A useful sequence is:

requirement
   ↓
Boolean question
   ↓
C# condition
   ↓
controlled action

For:

the condition is:

team1.score >= 3

The complete code is:

if (team1.score >= 3)
{
    System.Console.WriteLine("Three or more goals.");
}
Use Equality When the Requirement Means Exactly

Requirement:

if (player1.position == "Goalkeeper")
{
    System.Console.WriteLine("Use goalkeeper instructions.");
}

The equality operator compares the current string value with the expected text.

Use a Null Check When the Decision Depends on a Missing Reference

Requirement:

if (player1.team == null)
{
    System.Console.WriteLine("No team assigned.");
}

The condition reads the reference state.

It does not create or remove a Team.

Add else for a Required Alternate Action

Requirement:

if (player1.isAvailable)
{
    System.Console.WriteLine("Available");
}
else
{
    System.Console.WriteLine("Unavailable");
}

The else block runs when the if condition is false.

Do Not Add an Unneeded else

If the requirement says only:

then:

if (player1.position == "Goalkeeper")
{
    System.Console.WriteLine("Use goalkeeper instructions.");
}

is enough.

Do not invent a second action when the requirement does not specify one.

Match the Operator to the Boundary

Requirement:

Use:

if (team1.score >= 3)
{
    System.Console.WriteLine("High scoring");
}

The phrase:

at least

includes 3 itself.

Use Braces and Indentation Consistently

Readable source:

if (team1.score >= 3)
{
    System.Console.WriteLine("High scoring");
}
else
{
    System.Console.WriteLine("Below 3");
}

The visual structure makes the controlled blocks clear.

Translate an Activity Diagram into C#

Suppose the diagram says:

Is the player available?
   true  → Display "Available"
   false → Display "Unavailable"

The C# can be:

if (player1.isAvailable)
{
    System.Console.WriteLine("Available");
}
else
{
    System.Console.WriteLine("Unavailable");
}

The diagram and source should describe the same behavior.

Read the Finished Code Back as a Sentence

For:

if (player1.position == "Goalkeeper")
{
    System.Console.WriteLine("Goalkeeper");
}
else
{
    System.Console.WriteLine("Field player");
}

read:

If that sentence does not match the requirement, revise the code.

Current Learning Boundary

For CO-045, write:

  • one simple if;
  • or one simple if/else.

Writing chained and nested conditional structures is taught in the next batch.

Key Patterns
if
if (booleanExpression)
{
    // action when true
}
if/else
if (booleanExpression)
{
    // action when true
}
else
{
    // action when false
}