Computer Science Principles
Course Progress
0/0
Objectives in LxD
3.4 Strings
3.4 Strings
3.3 Mathematical Expressions
3.3 Math Expressions
3.2 Data Abstractions
3.2 Data Abstractions
3.1 Variables & Assignments
3.1 Variables and Assignments
3.1 Variables and Assignments (Sample)
Intro to Python
Intro to Javascript
Variables and HTML DOM (Sample)
3.5 Boolean Expressions (PY)
3.5 Boolean Expressions (JS)
3.8 Iterations
3.7 Nested Conditionals
3.6 Conditionals
3.8 Iterations
3.7 Nested Conditionals
3.6 Conditionals
3.13 Developing Procedures
3.12 Calling Procedures
3.10 Lists
3.13 Developing Procedures
3.10 Lists
3.9 Developing Algorithms
3.17 Algorithmic Efficiency
3.9 Algorithms
3.17 Algorithmic Efficiency
3.15 Random Numbers (pseudocode)
3.15 Random Numbers (js)
3.15 Random Numbers (py)
BI 3 Review
Sprint View
Week 5
3.6 Conditionals
3.6 Conditionals
2 min read
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
elifforelse ifelifis only used in Pythonelse ifis only used in JavascriptPython:
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!");
}
5. Comparison & Logical Operators
- Comparison:
==equal!=not equal<,>,<=,>=
- Logical:
- Python →
and,or,not - JavaScript →
&&,||,!Python:
- 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");
}
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?
%%js
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?
%%js
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