University of Glasgow · FACULTY OF COMPUTER SCIENCE

COMPSCI4039 Chap.5 Selection and Boolean Conditions

- one subject, every graph, every model, every mark
7 Chapters4-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 5 of 10 · COMPSCI4039

Selection and Boolean Conditions

Selection and Boolean Conditions gives programs the ability to act differently depending on the data they hold. A selection statement asks a question of the program's state and runs a block only when the answer is True.

A lone if gives two outcomes, its block or nothing; adding else guarantees that one of two blocks runs; an elif chain chooses exactly one of several options, and n mutually exclusive options need n minus 1 conditions. Python checks a chain from the top and stops at the first true condition, so the order of conditions carries meaning, and the strictest or most specific test must come first.

Conditions are built from comparison operators such as ==, !=, <, >, <= and >=, combined with and, or and not, and their True or False results can be stored in boolean variables. Indentation defines which lines belong to each branch.

The chapter's central exercise is the leap-year rule, whose overlapping exceptions show why specific cases must be tested before general ones, and why good test cases include one input from every region of the rule.

In this chapter

What this chapter covers

  • 01

    Conditions as questions about program state

  • 02

    The if, else and elif statements

  • 03

    First-true-wins and the order of tests

  • 04

    Comparison operators and equality with ==

  • 05

    Logical operators and boolean variables

  • 06

    Nested selection and tracing one path

  • 07

    Leap years and choosing test cases

Worked example · free

Label two temperatures with an elif chain

Q [5 marks]. A function returns "freezing" if t < 0, "cool" if t < 15, "mild" if t < 25 and "hot" otherwise, tested in that order. What does it return for 15 and for -2? (Marks here are a study weighting of our own, not an official university scheme.)
The chain checks each condition from the top.The chain checks each condition from the top. For a mark of 64 the first test fails, the second succeeds and returns B, and the remaining tests and the else branch are never reached.mark >= 70Amark >= 60Bmark >= 50Cmark >= 40Delse: failTrueband(64) stops here
The chain checks each condition from the top. For a mark of 64 the first test fails, the second succeeds and returns B, and the remaining tests and the else branch are never reached.
  • 1For 15, the test 15 < 0 is False.
  • 115 < 15 is also False, because the comparison is strict.
  • 115 < 25 is True, so the function returns mild and stops checking.
  • 1For -2, the very first test -2 < 0 is True.
  • 1The chain stops at once and returns freezing.
The function returns mild for 15 and freezing for -2.
Sia tip — Test boundary values exactly: a strict less-than excludes the number it compares against.
Glossary

Key terms

Selection statement
A statement that chooses which block of code runs based on a condition.
Boolean variable
A variable holding True or False, often the stored result of a comparison.
Test case
A chosen input with a known expected result, used to check that a program behaves correctly.
FAQ

Selection and Boolean Conditions FAQ

Why does my elif chain give the wrong grade?

Python runs only the first branch whose condition is True and skips the rest. If a looser test sits above a stricter one, the looser branch catches values meant for later branches. Order the conditions from most specific to least specific.

Why is equality written with two equals signs?

A single equals sign assigns a value to a name, while == compares two values and produces True or False. Writing one sign inside an if condition is a mistake, so read every condition aloud to confirm it asks a question.

How many conditions do I need for four outcomes?

Three. With an if, two elif branches and a final else, exactly one of four blocks runs, and the else catches whatever the three conditions did not. In general, n exclusive outcomes need n minus 1 conditions.

Why does comparing input with a number fail?

input always returns a string, and Python will not order a string against an int, so testing whether raw input is less than 17 stops with a type error. Cast with int when reading the value.

Why should I test boundary values?

Boundaries are where strict and non-strict comparisons part company. A mark of exactly 70 passes a test written as at least 70 but fails one written as more than 70. Choosing one input on each side of every threshold, plus the threshold itself, reveals comparison mistakes that ordinary middle-of-the-range inputs would never expose.

Study strategy

Exam move

Treat every selection question as a path-finding exercise. Before tracing, number the conditions in the order Python will test them, then follow one input at a time and stop at the first True. Practise with boundary values in particular, such as a mark exactly on a grade threshold, because strict and non-strict comparisons behave differently there.

For writing practice, take a plain-English rule with exceptions, like the leap-year rule or a ticket-pricing scheme, draw the overlapping cases as nested regions, and write the chain from the innermost region outward. Then choose one test input from each region and predict the result before running the code.

Rewrite at least one condition using the opposite operators and and or, and check that your two versions agree, as the lecture's driving-age examples do. Finally, read indentation as carefully as conditions: misplace one line and an instruction escapes its branch. When you meet nested decisions, write down the list of conditions that held along the route; that list is the explanation a marker wants to see.

Working through Selection and Boolean Conditions in COMPSCI4039? Sia is AskSia’s AI Computer Science tutor — ask any COMPSCI4039 Selection and Boolean Conditions question and get a clear, step-by-step explanation grounded in how COMPSCI4039 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.

A+Everything unlocked
Unlocks this Bible + all 26 of your subjects at University of Glasgow, plus 1,000+ more Bibles across the AskSia library.
Sia - your COMPSCI4039 tutor, unlimited, worked the way the exam marks it
The full 4-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works