3.2.2, 3.2.4, 3.2.5 - Selection and Boolean conditions (C#)

3.2.2, 3.2.4, 3.2.5 - Selection and Boolean conditions (C#)

Compare data, combine true/false conditions and choose which statements a C# program executes. Written tasks use native C# with no Run. C# execution is unavailable here; the runnable activity is an optional Python demonstration.

Comparing values

A relational operation compares two values and produces a Boolean result: either true or false.

MeaningC# operatorExample when score is 10
Equal to==score == 10 is true
Not equal to!=score != 10 is false
Less than<score < 10 is false
Greater than>score > 10 is false
Less than or equal to<=score <= 10 is true
Greater than or equal to>=score >= 10 is true

The equality comparison == asks whether values match. The assignment = stores a value; they do different jobs. An inclusive limit needs equality as well: a mark of 10 qualifies under score >= 10, but fails under score > 10.

AQA supplied algorithms may use different symbols, such as = for comparison and ≠ for not equal. Read them according to the algorithm's convention and write native operators when producing C#.

Combining Boolean conditions

Boolean operators combine or reverse conditions so a program can express a rule involving more than one requirement.

C# && means AND: both conditions must be true. || means OR: at least one condition must be true, including when both are true. ! means NOT: it reverses a Boolean value.

C#
bool validQuantity = quantity >= 1 && quantity <= 12;
bool concession = isStudent || isSenior;
bool mayEnter = !isClosed && hasTicket;

For quantity 13, the lower-limit test is true but the upper-limit test is false, so validQuantity is false. Replacing AND with OR would incorrectly accept every integer: 13 meets the lower limit and 0 meets the upper limit. To identify an invalid quantity instead, use quantity < 1 || quantity > 12.

Parentheses expose grouping. !(isStudent || isSenior) means the person is neither a student nor a senior. It differs from !isStudent || isSenior, which can be true for a senior. The same Boolean conditions can control selection or determine whether a loop continues; the operators' meanings do not change.

Use AND when all requirements must hold, OR when alternatives are allowed, and NOT when a condition must be reversed. Check the exact permitted boundary values.

Choosing which statements run

Selection changes the flow by executing a branch only when its condition has the required truth value.

C#
if (quantity >= 1 && quantity <= 12)
{
    Console.WriteLine("Accepted");
}
else
{
    Console.WriteLine("Rejected");
}
Console.WriteLine("Check finished");

Exactly one of the two branches runs. The final statement is outside both braces, so it runs whichever branch was chosen. An if without else skips its body when false; it does not stop the whole program. For several alternatives, an else if chain checks the next condition only if previous conditions failed.

C#
if (score >= 80)
{
    Console.WriteLine("Gold");
}
else if (score >= 50)
{
    Console.WriteLine("Silver");
}
else
{
    Console.WriteLine("Bronze");
}

A score of 85 displays only Gold. Separate if statements for both thresholds would also display Silver, which would implement a different rule. Selection is the choice principle used alongside sequence and iteration.

Predict the optional Python demonstration for quantities 0, 1, 12 and 13. Run it, then change the upper comparison from <= 12 to < 12 and rerun. Identify the single output that changes.

Optional Python demonstration: inclusive limits

{"code":"for quantity in [0, 1, 12, 13]:\n if quantity >= 1 and quantity <= 12:\n print(quantity, "Accepted")\n else:\n print(quantity, "Rejected")","instructions":"Predict the four outputs and run. Change quantity <= 12 to quantity < 12 and run again."}