if–else Statement

An if–else statement is used to check a condition with two possible outcomes.

  • If the condition is True → one block runs
  • If the condition is False → another block runs

It uses the keywords if and else

Explanation

First, the program checks the condition.

  • If True → executes if block
  • If False → executes else block

✔ Only one block will run

It is useful for yes/no decisions.


Real-Time Scenarios 

  • If you studypass, else → fail
  • If switch is ONlight glows, else → OFF
  • If ticket is availabletravel, else → stay
  • If number is evendo one task, else → another task
  • If password is correctlogin, else → error

Syntax

if condition:
    # True block
else:
    # False block

Example 1

num = 10
if num % 2 == 0:
    print("Even")
else:
    print("Odd")

Output:

Even

Example 2 

age = 15
if age >= 18:
    print("Adult")
else:
    print("Minor")

Output:

Minor

Explanation

  • num % 2 == 0 → checks if number is even
  • age >= 18 → checks age condition
  • Based on True/False, Python executes one block

Common Beginner Mistakes

  •  Missing colon (:)
  • Wrong indentation
  • Using = instead of ==
  • Writing unclear conditions

Why if–else is Important

  • Helps in decision-making
  • Used in real-world programs
  • Makes apps and games smarter
  • Controls program flow

➤ Key Points

  • Always has two outcomes
  • Only one block executes
  • Improves decision-making logic

➤ Tips

  • Use for binary decisions (Yes/No)
  • Avoid writing long conditions
  • Keep else block meaningful

Check your knowledge

Quickly verify what you've learned from this tutorial.

Question 1

What does an if–else statement do?

if–else is used for decision-making.

Question 2

How many outcomes does if–else have?

It has two outcomes (True/False).

Question 3

What is the output?

num = 5

if num % 2 == 0:
    print("Even")
else:
    print("Odd")

5 is not divisible by 2 → Odd.

Question 4

What is the output?

x = 10

if x > 20:
    print("A")
else:
    print("B")

Condition is False → else block runs.

Question 5

What is the output?

x = 7

if x % 2 == 0:
    print("Even")
else:
    print("Odd")
print("Done")

First prints Odd, then prints Done.

Congratulations!

You've successfully mastered the knowledge check for "if–else Statement."

For more questions and practice, click the link below:

Practice More Questions
Previous Topic if Statement Next Topic if–elif–else Statement