University of Melbourne · FACULTY OF COMPUTER SCIENCE

COMP90059 Chap.3 Boolean Logic and Conditionals

- one subject, every graph, every model, every mark
12 Chapters3-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 3 of 9 · COMP90059

Boolean Logic and Conditionals

A conditional evaluates a truth-valued expression and selects one suite of statements. Reliable branching begins by partitioning the input space: identify regions, decide where equality belongs and order overlapping tests so the first true branch is the intended one. Boolean operators combine claims, while short-circuit evaluation can guard an unsafe operation by testing its precondition first.

Truthiness gives empty and zero values false-like behaviour, but an explicit comparison may be clearer when zero has a separate meaning. Indentation defines branch membership, so tracing must bracket each suite before following state. Boundary tests immediately below, on and above a threshold are stronger than one comfortable middle value.

In this chapter

What this chapter covers

  • 01

    Boolean literals represent true and false directly

  • 02

    Comparison operators produce Boolean results

  • 03

    Chained comparisons express bounded intervals

  • 04

    If, elif and else select the first matching branch

  • 05

    And, or and not combine or negate conditions

  • 06

    Short-circuit evaluation can protect a risky expression

  • 07

    Truthiness classifies zero and empty values as false-like

  • 08

    Boundary tables reveal gaps and overlapping conditions

  • 09

    De Morgan transformations preserve meaning while changing grouping

  • 10

    Guard clauses handle invalid states before the main decision

  • 11

    Mutually exclusive branches should partition the intended input space

  • 12

    Condition traces record operand values before recording the final truth value

Worked example · free

Guard division with a conjunction

Q [4 marks]. Explain count != 0 and total / count > 4 when count may be zero. The marks used below structure self-review and are not official assessment weighting.
A branch evaluates one condition, executes one route, and rejoins the surrounding program afterward.A branch evaluates one condition, executes one route, and rejoins the surrounding program afterward.condition?true suitefalse suiteTrueFalse
A branch evaluates one condition, executes one route, and rejoins the surrounding program afterward.
  • 1Python evaluates count != 0 first.
  • 1When count is zero, the first operand is false and the division is skipped.
  • 1When count is nonzero, Python evaluates the ratio safely.
  • 1The guard must appear before the risky division because evaluation proceeds left to right.
The condition is safe because a false guard determines the conjunction before division is attempted. Reversing the operands would remove that protection.
Sia tip — Read every conjunction as a left-to-right dependency and ask whether the second operand has a precondition.
Glossary

Key terms

Boolean value
One of the two logical values True and False, used directly or produced by a comparison.
Chained comparison
A compact interval test in which one middle value is compared against both bounds.
Short circuit
Stopping Boolean evaluation once the final truth value is already determined.
Truthiness
The rule by which non-Boolean objects are interpreted in conditions, including false-like zero and empty values.
Branch suite
The indented statements controlled by one if, elif or else header.
Boundary case
An input located at or beside a threshold where equality and branch order determine the result.
Guard clause
An early conditional that rejects or handles an exceptional state so later code can assume a simpler, valid precondition.
Decision partition
A collection of input regions designed so every valid case belongs to the intended branch without accidental gaps or overlaps.
De Morgan law
A Boolean equivalence that moves negation across and or or while negating each operand, useful for checking complex conditions.
FAQ

Boolean Logic and Conditionals FAQ

Why does branch order matter?

An if and elif chain stops at the first true condition. A broad earlier condition can capture values intended for a narrower later condition before that specific test is reached.

When should I use a chained comparison?

Use it when one value must lie between two bounds. It states both endpoints clearly, evaluates the middle expression once and keeps the two boundary decisions visually adjacent.

How does short-circuit evaluation prevent errors?

A safe first operand can determine the outcome before Python reaches an unsafe second operand, such as division that requires a nonzero denominator, but the guard must appear in the correct operand order.

Is a final else always necessary?

No. Use it when the remaining region has a defined outcome or when it should signal invalid input. Do not add it without knowing what states it represents or whether silently accepting them is appropriate.

How can I prove a conditional covers every valid input?

Describe each branch as an interval or logical set, then test their union for full coverage and their intersections for unintended overlap. Include every exact threshold in the table, because equality errors usually hide where neighbouring regions meet.

How should I simplify a long Boolean expression?

Name meaningful intermediate conditions, state the intended truth rule in words and preserve grouping with parentheses. Build a truth table for the uncertain combinations, then compare any De Morgan rewrite row by row. A shorter expression is useful only when it preserves the same boundary behaviour and remains easier for a reader to verify.

Study strategy

Exam move

Draw a number line for every multi-band condition. Mark the thresholds, label whether each endpoint is included and select one test value from every region. Translate the prose rule into code, then translate the code back into prose. For Boolean expressions, parenthesise the grouping and trace operands left to right, noting where evaluation stops.

Build one safe guard for division, one membership check for an empty collection and one ordered grading chain. Test the exact thresholds and values immediately around them. If a result is wrong, inspect branch order, operand type, equality signs and indentation separately rather than changing several conditions at once.

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

A+Everything unlocked
Unlocks this Bible + all 177 of your University of Melbourne subjects - and 1,000+ Bibles across every Australian university.
Sia - your COMP90059 tutor, unlimited, worked the way the exam marks it
The full 3-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