Conditionals in Javascript and Python - Lesson
Apply your skills to basic algorithmic design with conditionals.
Before we start
What’s Happening Here:
- The game is listening for a key press.
ifchecks which key was pressed.- Depending on the condition, the game runs a different action:
- If
O→ start moving the pushers. - If
Escape→ stop them. - Otherwise → print a message.
- If
- The game also checks whether if the key’s direction is facing the lock and it is touching the lock
- if yes, go to the next level
- if no, continue with the game
Every time the player presses a key, the program runs this decision tree.
This is exactly what you’ll be learning to write in your own code today!
CSP 3.6 — Conditionals
Conditionals let a program choose what to do based on whether something is True or False.
If it rains → take an umbrella 🌧️; otherwise → wear sunglasses 😎.
🎯 Objectives
By the end, you will be able to
- Explain selection and conditions.
- Write
if,if/else, andif/elif/elsestatements. - Build boolean expressions using comparisons and
and/or/not. - Predict what branch of code will run given different inputs.
Key Vocabulary
- Condition: a test that is either True or False.
- Boolean: a value that is
TrueorFalse. - Branch: the path your program takes depending on the condition.
- Selection: choosing different actions based on conditions.
🧭 How selection flows
Start
│
▼
[ Evaluate condition ]
├── True → do Action A
└── False → do Action B
1) Boolean expressions & comparisons
A boolean expression always gives True or False.
Common comparisons :
>greater than<less than>=greater or equal<=less or equal==equal to!=not equal to
Logical operators (single level for this lesson): and, or, not
# Try it: change the values and re-run to see True/False
x, y = 7, 10
print("x > y:", x > y)
print("x == 7:", x == 7)
print("x != y:", x != y)
print("x in [0,10] AND y in [0,10]:", (0 <= x <= 10) and (0 <= y <= 10))
print("x in [0,10] OR y in [0,10]:", (0 <= x <= 10) or (0 <= y <= 10))
print("NOT (x > y):", not (x > y))
x > y: False
x == 7: True
x != y: True
x in [0,10] AND y in [0,10]: True
x in [0,10] OR y in [0,10]: True
NOT (x > y): True
2) if — run a block only when the condition is True
# Example: driving check
age = 18
if age >= 16:
print("You can drive!") # this line only runs if condition is True
You can drive!
let age = 18;
if (age >= 16) {
console.log("You can drive!") //this line only runs if condition is True
}
You can drive!
Think & Try:
- Change
ageto 10, 16, 20. When does the message print? - Explain the role of the colon
:and indentation in Python.
3) if / else — choose one of two branches
Exactly one branch runs.
# Example: simple gate
age = 14
if age >= 16:
print("You can drive!")
else:
print("You cannot drive yet.")
You cannot drive yet.
let age = 18;
if (age >= 16) {
console.log("You can drive!");
} else {
console.log("You cannot drive yet.");
}
You can drive!
Think & Try:
- Flip the comparison to
age > 15. Does it behave the same? - Reword messages to fit your style.
4) if / elif / else — multiple conditions
Python checks top to bottom and runs the first matching branch.
# Example: letter grade
score = 85
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
else:
print("D or F")
B
let score = 85;
if (score >= 90) {
console.log("A");
} else if (score >= 80) {
console.log("B");
} else if (score >= 70) {
console.log("C");
} else {
console.log("D or F");
}
B
Think & Try:
- Set
score = 90,80,79,70. Which branch triggers? - Why must
elif score >= 80come after the>= 90check?
5) Combining conditions with and / or / not
We can combine simple comparisons into a single (non-nested) boolean expression.
# Example: number in range [0, 10]
if (n >= 0) and (n <= 10):
print("n is in [0,10]")
else:
print("n is outside [0,10]")
//Javascript
let n = 7;
if (n >= 0 && n <= 10) {
console.log("n is in [0,10]");
} else {
console.log("n is outside [0,10]");
}
//Note: `&&` is JS AND, `||` is OR, `!` is NOT. In Python we write `and`, `or`, `not`.
<IPython.core.display.Javascript object>
Try
- Changing
nto-2,0,10,11. - Replacing the “and” with “or”. Think of situations when you would use “and” & situations when you would use “or”
Mini Practice: Predict, then Run
Before you run each cell, think and predict what it will print. Then run and compare.
# 1) What prints?
temp = 60
if temp > 70:
print("Warm")
else:
print("Chilly") # Predict before you run
Chilly
# 2) What prints?
water = "sparkling"
if water == "still":
print("Calm")
elif water == "sparkling":
print("Fizz")
else:
print("Unknown")
Fizz
# 3) What prints?
x = 3
y = 12
if x > 0 and y < 10:
print("Case A")
elif x > 0 and y >= 10:
print("Case B")
else:
print("Case C")
Case B
Common issues and solutions
- Missing colon
:afterif,elif, orelse. → Add it - Indentation: code inside a branch must be indented consistently (usually 4 spaces).
- Using
=vs==:=assigns a value;==tests for equality. - Case sensitivity:
"Yes"is not the same as"yes". Use.lower()for user inputs so that your program actually runs. - Exclusive vs inclusive:
>(strict) vs>=(includes equality). Choose carefully based on your situation
Review
| Concept | Purpose | Example |
|—|—|—|
| if | Run only when condition True | if age >= 16: print("Drive") |
| if/else | Choose between 2 paths | if x % 2 == 0: ... else: ... |
| if/elif/else | Many options in order | grades ladder |
| comparisons | build boolean tests | ==, !=, >, <, >=, <= |
| logic | test multiple requirements | and, or, not |
Exit Ticket
Write a one-sentence answer in a new cell:
- What does selection mean in programming?
- Give a real-world example.