Courses / Computer Science I
Programming I

Conditionals and Logical Branching

Computer Science I 236 words Free to read

Making Decisions

A program that always does the same thing is not very useful. Conditionals let code choose a path based on whether a condition is true, using if and optional else blocks.

The condition must be a boolean expression evaluating to true or false. Comparison operators build these:

OperatorMeaningExample
==Equalx == 5
!=Not equalx != 5
<, >Less / Greaterx > 0
<=, >=Less/Greater-equalx <= 10

Common pitfall: Confusing == (comparison) with = (assignment). Writing if x = 5 tries to assign a value instead of comparing it, causing a syntax or logic error.

Comparison operators query `x`; assignment overwrites it -- shown as a

Combining Conditions

Boolean operators combine multiple expressions. Short-circuiting means evaluation stops early if the outcome is already known.

OperatorRuleShort-Circuit Behavior
andTrue if both sides are trueIf left is false, stops and returns false
orTrue if at least one side is trueIf left is true, stops and returns true
notFlips the boolean valueN/A

Chain cases using else if (or elif). The first matching branch runs, and all subsequent branches are skipped entirely.

Practise this lesson

The explanation above is free to read. The graded practice for this lesson lives in the Tryals app.

11practice questions
2interactive scenes

Programming I