CS Pathfinder Logo CS Pathfinder

Unit 2 · If-Else Statement

If-Else Statement

Learn how Python handles two-way decision-making using the if-else statement, its execution flow, and the one-line ternary form.

Introduction

A simple if statement is useful, but it has one limitation — it only tells Python what to do when a condition is True. It does not specify what should happen when the condition is False. In real life, almost every decision has an alternative outcome. For example:

  • If it rains, carry an umbrella; otherwise, don't.
  • If marks are above 40, the student passes; otherwise, the student fails.
  • If the ATM PIN is correct, allow withdrawal; otherwise, deny access.
  • If a number is even, print "Even"; otherwise, print "Odd".

To handle this second possibility, Python provides the if-else statement. It allows a program to choose between two alternative paths of execution — one path runs when the condition is True, and the other path runs when the condition is False.

Easy Definition

An if-else statement executes one block of code if a condition is True, and a different block of code if the condition is False.

Real-Life Example

  • Traffic Signal: If light is green, go; otherwise, stop.
  • Exam Result: If marks ≥ 40, pass; otherwise, fail.
  • Age Check: If age ≥ 18, eligible to vote; otherwise, not eligible.
  • Bank Withdrawal: If balance ≥ amount, allow; otherwise, reject.

Every situation has exactly two outcomes, and only one of them happens.

Examination Point of View

  • Definition and syntax of if-else are frequently asked.
  • Difference between if statement and if-else statement.
  • Tracing/predicting output of if-else code snippets.
  • Writing the ternary (conditional expression) form.
If-Else Statement Flow
Figure 2.3 — Two-way branching using an if-else structure. Exactly one path executes.

Table of Contents

Syntax of If-Else Statement

The general syntax of an if-else statement is:

if condition:
    # Executes if condition is True
    statement_block_1
else:
    # Executes if condition is False
    statement_block_2

Important Rules

  • The else keyword must align with the if keyword at the same indentation level.
  • else is always followed by a colon (:) — never a condition.
  • Each block (if and else) has its own indented set of statements.
  • An else block cannot exist without a preceding if.

Warning: Writing else condition: is a syntax error. else never takes a condition — it simply means "in every other case."

Execution Flow

When Python encounters an if-else statement, it works as follows:

  1. Python evaluates the condition next to if.
  2. If the condition is True, the if block executes, and the else block is completely skipped.
  3. If the condition is False, the if block is skipped, and the else block executes instead.
  4. Execution then continues with the first statement after the entire if-else structure.

Tip

Only one of the two blocks will ever run for a single pass through the statement. They are mutually exclusive — never both, never neither.

Flow Table

Condition Result Block Executed Other Block
True if block Skipped
False else block Skipped

Code Examples

1. Even or Odd Number

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

Output

17 is Odd

2. Pass or Fail Result

marks = 35
if marks >= 40:
    print("Result: Pass")
else:
    print("Result: Fail")

Output

Result: Fail

3. ATM PIN Verification

correct_pin = 4521
entered_pin = 4521

if entered_pin == correct_pin:
    print("Access Granted")
else:
    print("Access Denied")

Output

Access Granted

Real-Life Examples

Situation If Block Else Block
Voting Eligibility Age ≥ 18 → Eligible Age < 18 → Not Eligible
Online Shopping Stock available → Order placed Out of stock → Order rejected
Weather Raining → Carry umbrella Not raining → No umbrella

If vs If-Else

Feature if Statement if-else Statement
Number of paths One (True case only) Two (True and False cases)
When condition is False Nothing happens; program moves on else block runs
Use case Optional/one-sided action Two-way decision

Ternary Operator (Conditional Expressions)

Python supports a one-line version of the if-else statement known as a conditional expression or ternary operator. It is used to assign a value based on a condition in a single line, without needing multiple lines of code.

result = value_if_true if condition else value_if_false

Example

age = 20
status = "Adult" if age >= 18 else "Minor"
print(status)  # Adult

Memory Trick

Read it like plain English: "Adult IF age ≥ 18 ELSE Minor." The value that comes first is the one used when the condition is True.

Exam Point

"Rewrite the following if-else statement as a single-line conditional expression" is a very common question. Practice converting multi-line if-else blocks into ternary form and back.

Common Mistakes

  • Forgetting the colon (:) after if or else.
  • Misaligning the indentation of if and else blocks.
  • Writing a condition next to else (not allowed).
  • Assuming both blocks can run together — only one ever executes.
  • Confusing the order of value_if_true / value_if_false in the ternary form.

Important Examination Points

  • if-else provides exactly two mutually exclusive execution paths.
  • else never takes a condition of its own.
  • The ternary operator is a compact, single-line equivalent of if-else.
  • Tracing output of nested/if-else code is a very common exam question type.
  • if-else is the foundation for the elif ladder covered in the next chapter.

Interview Questions

  1. What is the difference between if and if-else?
  2. Can an if-else statement run both blocks together? Why or why not?
  3. What is a ternary operator in Python?
  4. Write the general syntax of if-else.
  5. Convert a given if-else block into a one-line conditional expression.

Practice MCQs

1. What is the output?

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

Answer: Even


2. Which keyword is used to define the alternative block in an if-else statement?

A) elif    B) else    C) otherwise    D) default

Answer: B) else


3. What is the ternary operator equivalent of:

if x > 5:
    result = "Big"
else:
    result = "Small"

Answer: result = "Big" if x > 5 else "Small"

Summary

The if-else statement chooses between two mutually exclusive executable blocks based on a conditional test — exactly one block runs, never both. Python also provides a compact ternary (conditional expression) form for writing simple if-else logic in a single, readable line of code.

Python Programming Handwritten Notes

Master Python Programming with Easy Handwritten Notes – Perfect for Interviews, Placements, GATE & Exams.