Introduction
Imagine you want to decide what to wear based on the weather. You need a way to make choices depending on different situations. Conditional logic helps computers make decisions like this by checking if something is true or false.
Jump into concepts and practice - no test required
Imagine you are deciding what to wear before going outside. You first check if it is raining; if yes, you take an umbrella. If not, you check if it is sunny; if yes, you wear sunglasses. Otherwise, you just wear your regular clothes.
┌───────────────┐
│ Start │
└──────┬────────┘
│
▼
┌───────────────┐
│ Check condition│
│ (Is it raining?)│
└──────┬────────┘
│Yes
▼
┌───────────────┐
│ Take umbrella │
└──────┬────────┘
│
▼
┌───────┐
│ End │
└───────┘
│No
▼
┌───────────────┐
│ Check else if │
│ (Is it sunny?)│
└──────┬────────┘
│Yes
▼
┌───────────────┐
│ Wear sunglasses│
└──────┬────────┘
│
▼
┌───────┐
│ End │
└───────┘
│No
▼
┌───────────────┐
│ Wear regular │
│ clothes │
└──────┬────────┘
│
▼
┌───────┐
│ End │
└───────┘What does an if statement do in a program?
ifif statement asks a question that can be true or false.if block; otherwise, it skips it.if = condition check [OK]Which of the following is the correct syntax for an if statement in Python?
___ x > 10:
print("x is greater than 10")if to start a condition check.: after the condition is required to start the indented block.What will be printed by this Python code?
age = 20
if age >= 18:
print("Adult")
else:
print("Child")age >= 18age is 20, which is greater than or equal to 18, so condition is true.print("Adult") line runs, printing "Adult".Find the error in this code snippet:
if score > 50
print("Pass")
else:
print("Fail")if statement: at the end of the if condition line.if score > 50 is missing the colon, causing a syntax error.if condition -> Option BYou want to write a program that prints "Good morning" if the hour is less than 12, and "Good afternoon" otherwise. Which code correctly implements this?
hour = 10
___ hour < 12:
print("Good morning")
else:
print("Good afternoon")if to check if hour < 12.else for the alternativeelse block runs if the if condition is false, printing "Good afternoon".