4.1.1.2, 4.1.1.4, 4.1.1.5 - Selection and Boolean conditions (C#)

4.1.1.2, 4.1.1.4, 4.1.1.5 - Selection and Boolean conditions (C#)

Selection chooses which statements a program performs (4.1.1.2). Build decisions from relational comparisons (4.1.1.4) and Boolean operators (4.1.1.5), then trace nested decisions and test their boundaries.

C# route status: incomplete: runnable support pending. This lesson uses written native source with no Run control. Paper 1 practical readiness still requires runnable support.

Relational comparisons

A relational comparison produces a Boolean value that can control a decision.

MeaningC# operatorExample, with score = 60
Equal to==score == 60 is true
Not equal to!=score != 60 is false
Less than<score < 60 is false
Greater than>score > 60 is false
Less than or equal to<=score <= 60 is true
Greater than or equal to>=score >= 60 is true

bool passed = score >= 60; stores the comparison result. A threshold includes equality only when its operator does: >= 60 admits 60, whereas > 60 does not. That one boundary case can distinguish correct code from an off-by-one error.

In an if statement, the expression in parentheses must be Boolean. The block in braces executes only when it is true. An else block executes when it is false; exactly one of the two is taken.

C#
if (score >= 60)
{
    Console.WriteLine("Pass");
}
else
{
    Console.WriteLine("Retry");
}

This fragment assumes an integer score and using System;. score = 60 is assignment, not equality. C# does not permit an integer assignment result as an if condition. Braces make the intended block clear and prevent a later added statement from accidentally sitting outside the branch.

Boolean conditions

Boolean operators combine or reverse conditions so that a decision can depend on several facts.

Boolean expression

An expression whose evaluated result is true or false.

For Boolean operands a and b, C# uses !a for NOT, a && b for AND, a || b for OR and a ^ b for XOR.

| a | b | a && b | a || b | a ^ b |
| --- | --- | --- | --- | --- |
| false | false | false | false | false |
| false | true | false | true | true |
| true | false | false | true | true |
| true | true | true | true | false |

AND requires both; OR permits either or both; XOR requires exactly one. NOT reverses one value. A club admits someone when their age is at least 16 and they have either a member card or a guest pass:

C#
bool admit = age >= 16 && (hasMemberCard || hasGuestPass);

The parentheses group the alternatives within the age restriction. Without them, age >= 16 && hasMemberCard || hasGuestPass would admit any guest-pass holder, including someone under 16. Avoid 16 <= age <= 25: C# does not chain comparisons that way. Write age >= 16 && age <= 25.

&& and || short-circuit. AND skips its right operand when the left is false; OR skips it when the left is true. text != null && text.Length > 0 safely avoids reading the length of a null string. Boolean & and | also exist but evaluate both operands. Here use && and || for normal conditions. With integer operands ^ performs bitwise XOR; with Boolean operands it performs the logical XOR shown in the table. It is never exponentiation.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account