1.4.18 Relational Operators in Conditional Expressions

Relational Operators Compare Numeric Values

A program often needs to ask questions such as:

C# uses relational operators to compare values.

Common relational operators include:

>   greater than
<   less than
>=  greater than or equal to
<=  less than or equal to

Each comparison produces:

true

or:

false
Greater Than

Consider:

homeScore > awayScore

If:

homeScore = 3
awayScore = 1

the expression evaluates to:

true

because 3 is greater than 1.

You can use that result in an if statement:

if (homeScore > awayScore)
{
    System.Console.WriteLine("Home team is ahead.");
}
Less Than

Suppose:

double distanceToBall = 4.5;

The condition:

distanceToBall < 5.0

asks:

If the answer is true, the controlled block runs.

Greater Than or Equal To

Consider:

if (playerAge >= 18)
{
    System.Console.WriteLine("Adult division.");
}

The operator:

>=

includes equality.

The condition is true when the age is greater than 18 or exactly 18.

Less Than or Equal To

For:

if (fouls <= 2)
{
    System.Console.WriteLine("Within the current limit.");
}

the expression is true when the value is below or equal to 2.

Relational Expressions Are Boolean Expressions

This:

homeScore > awayScore

is not an assignment.

It does not change either score.

It asks a question.

The result is a Boolean value:

true

or:

false

That is why it can appear inside an if.

Compare the Operators Carefully

These are different questions:

score > 5
score >= 5

If:

score = 5

then the first expression is false and the second is true.

One symbol changes the boundary of the condition.

Use the Operator that Matches the Requirement

Suppose a requirement says:

The phrase:

at least

includes 10 itself.

A fitting comparison is:

score >= 10

Using:

score > 10

would exclude the value 10 and would not match the requirement.

Boundary Values Deserve Attention

Whenever a condition uses a relational operator, identify the boundary.

For:

score >= 10

the boundary is:

10

Ask:

This habit helps prevent boundary mistakes.

Relational Operators Can Compare Parameters

Suppose:

public void DisplayScoreStatus(int score)
{
    if (score >= 3)
    {
        System.Console.WriteLine("Three or more goals.");
    }
}

The parameter value becomes part of the Boolean expression.

The flow is:

Activity diagram showing argument, then score parameter, then score = 3, then true or false, then if path.
Argument.
  • New
  • Changed
  • Removed
Relational Operators Can Compare Local Variables

For example:

int difference = homeScore - awayScore;

if (difference > 0)
{
    System.Console.WriteLine("Home team is ahead.");
}

The calculation happens first.

The comparison then asks a question about the calculated value.

Relational Operators and Equality Ask Different Questions

This:

score == 5

asks:

This:

score >= 5

asks:

Choose based on the requirement.

Keep Conditions Readable

A focused condition such as:

distanceToBall < 5.0

communicates one clear question.

More complex combinations of several Boolean conditions are taught later.

For now, master one comparison at a time.

The Main Idea

Relational operators turn numeric relationships into Boolean results:

Activity diagram showing numeric values, then comparison, then true or false, then conditional path.
NumericValues.
  • New
  • Changed
  • Removed