Conditionals Lesson


1. What is a Conditional?

  • A conditional lets a program make decisions.
  • Example in plain English: “If it’s raining, bring an umbrella. Otherwise, don’t bring one.”

2. Basic if Statement:

Python:

x = 10

if x > 5:
    print("x is greater than 5")

JavaScript:

%%js

let x = 10;

if (x > 5) {
    console.log("x is greater than 5");
}

3. If ... else

Python:

age = 16

if age >= 18:
    print("You can vote")
else:
    print("You are too young to vote")

JavaScript:

%%js

let age = 16;

if (age >= 18) {
    console.log("You can vote");
} else {
    console.log("You are too young to vote");
}

4. If ... else if ... else (multiple conditions)

Common mistakes:

  • Mistaking elif for else if
    • elif is only used in Python
    • else if is only used in Javascript

      Python:

score = 85 

if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
else:
    print("Keep working hard!")

JavaScript:

%%js

let score = 85;

if (score >= 90) {
    console.log("Grade: A");
} else if (score >= 80) {
    console.log("Grade: B");
} else {
    console.log("Keep working hard!");
}
<IPython.core.display.Javascript object>

5. Comparison & Logical Operators

  • Comparison:
    • == equal
    • != not equal
    • <, >, <=, >=
  • Logical:
    • Python → and, or, not
    • JavaScript → &&, ||, !

      Python:

temperature = 72

if temperature > 60 and temperature < 80:
    print("Perfect weather!")

JavaScript:

%%js

let temperature = 72;

if (temperature > 60 && temperature < 80) {
    console.log("Perfect weather!");
}

6. Booleans in Conditionals

Python:

is_raining = True
if is_raining:
    print("Take an umbrella")
else:
    print("No umbrella needed")

Javascript

%%js

let isRaining = true;
if (isRaining) {
    console.log("Take an umbrella");
} else {
    console.log("No umbrella needed");
}
<IPython.core.display.Javascript object>

Conditions MC


1. Basic If Statement

What will this Python code print?

x = 10
if x > 5:
    print("x is greater than 5")
else:
    print("x is less than 5")
  • A) Nothing
  • B) x is greater than 5
  • C) x is less than 5
  • D) Error: missing else

2. Elif vs Else If

In which language do you use elif instead of else if?

  • A) JavaScript
  • B) Python
  • C) Both
  • D) Neither

3. Conditional Weather Check

What will the weather be?

let temperature = 72;
if (temperature > 60 && temperature < 80) {
    console.log("Perfect weather!");
} else if (temperature <= 60) {
    console.log("Horrible weather");
}
  • A) Perfect weather!
  • B) Error
  • C) Horrible weather
  • D) It depends on humidity

4. Multiple Conditions

What grade will you get?

let score = 85;
if (score >= 90) {
    console.log("Grade: A");
} else if (score >= 80) {
    console.log("Grade: B");
} else if (score >= 70) {
    console.log("You failed!");
}
  • A) Grade: A
  • B) Grade: B
  • C) You failed!
  • D) Error