Bird
Raised Fist0
Intro to Computingfundamentals~6 mins

Conditional logic (if-then decisions) in Intro to Computing - Full Explanation

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.
Explanation
Condition
A condition is a question or test that can be true or false. The computer checks this condition to decide what to do next. For example, it might check if the temperature is above 20 degrees.
Conditions are yes/no questions that guide decisions.
If statement
The if statement tells the computer to do something only when the condition is true. If the condition is false, the computer skips that action. This is like saying, 'If it is raining, take an umbrella.'
If statements run code only when the condition is true.
Else statement
The else statement gives the computer an alternative action when the condition is false. It answers the question, 'What should I do if the condition is not met?' For example, 'If it is not raining, wear sunglasses.'
Else statements provide a backup action when the condition is false.
Else if (elif) statement
Else if lets the computer check multiple conditions one after another. It tries each condition in order until one is true, then runs the matching action. This is like choosing clothes based on different weather types: hot, cold, or rainy.
Else if allows checking several conditions in sequence.
Real World Analogy

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.

Condition → Checking the weather to see if it is raining or sunny
If statement → Taking an umbrella only if it is raining
Else statement → Wearing sunglasses if it is not raining
Else if (elif) statement → Checking for sunny weather after checking for rain
Diagram
Diagram
┌───────────────┐
│ 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   │
    └───────┘
Flowchart showing decision steps for if, else if, and else conditions.
Key Facts
ConditionA test that can be true or false to guide decisions.
If statementRuns code only when its condition is true.
Else statementRuns code when the if condition is false.
Else if (elif) statementChecks multiple conditions in order until one is true.
Common Confusions
Believing else runs only when if is true.
Believing else runs only when if is true. Else runs only when the if condition is false, providing an alternative action.
Thinking multiple if statements are the same as else if.
Thinking multiple if statements are the same as else if. Multiple ifs check all conditions separately; else if checks conditions in order and stops at the first true one.
Summary
Conditional logic lets computers make choices by testing conditions that are true or false.
If statements run code when a condition is true; else statements run code when it is false.
Else if statements let the computer check several conditions one by one.

Practice

(1/5)
1.

What does an if statement do in a program?

easy
A. It stores data in a list.
B. It repeats code multiple times.
C. It checks a condition and runs code only if the condition is true.
D. It creates a new variable.

Solution

  1. Step 1: Understand the purpose of if

    An if statement asks a question that can be true or false.
  2. Step 2: Identify what happens when the condition is true

    If the condition is true, the program runs the code inside the if block; otherwise, it skips it.
  3. Final Answer:

    It checks a condition and runs code only if the condition is true. -> Option C
  4. Quick Check:

    if = condition check [OK]
Hint: Remember: if means 'only if true' run code [OK]
Common Mistakes:
  • Thinking if repeats code like a loop
  • Confusing if with data storage
  • Believing if creates variables
2.

Which of the following is the correct syntax for an if statement in Python?

___ x > 10:
    print("x is greater than 10")
easy
A. else
B. for
C. while
D. if

Solution

  1. Step 1: Identify the keyword for condition checking

    Python uses if to start a condition check.
  2. Step 2: Match the syntax with the code block

    The colon : after the condition is required to start the indented block.
  3. Final Answer:

    if -> Option D
  4. Quick Check:

    Python if syntax = if [OK]
Hint: If checking condition, start with 'if' keyword [OK]
Common Mistakes:
  • Using 'for' or 'while' instead of 'if'
  • Forgetting the colon after the condition
  • Using 'else' without an 'if'
3.

What will be printed by this Python code?

age = 20
if age >= 18:
    print("Adult")
else:
    print("Child")
medium
A. Adult
B. No output
C. Child
D. Error

Solution

  1. Step 1: Check the condition age >= 18

    Variable age is 20, which is greater than or equal to 18, so condition is true.
  2. Step 2: Determine which block runs

    Since condition is true, the print("Adult") line runs, printing "Adult".
  3. Final Answer:

    Adult -> Option A
  4. Quick Check:

    20 >= 18 = Adult [OK]
Hint: Check if condition is true to pick printed output [OK]
Common Mistakes:
  • Choosing 'Child' without checking condition
  • Thinking no output if condition true
  • Assuming syntax error
4.

Find the error in this code snippet:

if score > 50
    print("Pass")
else:
    print("Fail")
medium
A. Wrong indentation of print statements
B. Missing colon after if condition
C. Using else without if
D. Incorrect comparison operator

Solution

  1. Step 1: Check syntax of if statement

    Python requires a colon : at the end of the if condition line.
  2. Step 2: Identify the missing colon

    The line if score > 50 is missing the colon, causing a syntax error.
  3. Final Answer:

    Missing colon after if condition -> Option B
  4. Quick Check:

    Colon needed after if condition [OK]
Hint: Look for missing colon after if condition line [OK]
Common Mistakes:
  • Ignoring missing colon error
  • Thinking indentation is wrong here
  • Confusing else usage
5.

You 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")
hard
A. if
B. else if
C. elif
D. while

Solution

  1. Step 1: Choose the correct keyword for the first condition

    The first condition must start with if to check if hour < 12.
  2. Step 2: Use else for the alternative

    The else block runs if the if condition is false, printing "Good afternoon".
  3. Final Answer:

    if -> Option A
  4. Quick Check:

    Use if for first condition, else for alternative [OK]
Hint: Start conditions with if, alternatives with else [OK]
Common Mistakes:
  • Using 'else if' instead of 'if' in Python
  • Using 'elif' without a preceding if
  • Using 'while' instead of if for decisions