If-Else

  • The If-Else condition in Python is used to make decisions in a program. It allows the program to execute different blocks of code based on whether a condition is True or False. It is one of the most important decision-making statements in Python and is widely used in real-world applications like login systems, grading systems, and validations.

  • What is the if-else Statement?

    The if-else statement is a decision-making structure in Python that allows a program to choose between two different blocks of code.

    • If the condition is True, the if block is executed

    • If the condition is False, the else block is executed

    This ensures one of the two blocks always runs.

    Why Use if-else?

    The if-else statement is used when:

    • A program must make a yes / no decision

    • One action is required for True

    • Another action is required for False

    • Alternative logic is necessary

    Real-Life Examples

    Situation

    Logic

    If marks ≥ 35 → Pass

    Else → Fail

    If age ≥ 18 → Vote

    Else → Not allowed

    If password correct → Login

    Else → Error

    If balance sufficient → Withdraw

    Else → Insufficient balance

    Syntax of if-else Statement

    Basic Syntax

    if condition:

        statement(s)

    else:

        statement(s)

    Syntax Explanation

    Part

    Description

    if

    Checks the condition

    condition

    Expression that returns True or False

    :

    Starts the block

    Indentation

    Defines the scope of blocks

    else

    Executes when condition is False

    Important Rule

    • else cannot exist without if

    • else has no condition

    Indentation Rules in if-else

    • Python uses indentation instead of braces

    • Both if and else blocks must be aligned

    • Each block must have consistent spacing

    ❌ Incorrect indentation causes an error

    Using Variables in if-else

    Variables store values that are tested using conditions.

Checking Student Result Using if-else

This program checks whether a student has passed or failed based on marks.

marks = 32

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

    • Variable: marks

    • Condition: marks >= 35

    • If False → else block executes

    Multiple Statements in if-else

    Both if and else blocks can contain multiple statements.

Displaying Multiple Messages Using if-else

This program prints multiple messages depending on the condition result.

age = 16

if age >= 18:
    print("Eligible to vote")
    print("Please collect your voter ID")
else:
    print("Not eligible to vote")
    print("Wait until you turn 18")
  • Using Comparison Operators

    Common operators used in if-else:

    Operator

    Meaning

    ==

    Equal

    !=

    Not equal

    >

    Greater

    <

    Less

    >=

    Greater or equal

    <=

    Less or equal

Comparing Two Numbers Using if-else

This program compares two numbers and prints the greater value.

a = 15
b = 20

if a > b:
    print("a is greater")
else:
    print("b is greater")
  • Using Boolean Variables

    Boolean variables hold True or False.


User Login Validation Using Boolean Variable

This program checks login status using a Boolean value.

is_logged_in = False

if is_logged_in:
    print("Login successful")
else:
    print("Please log in first")
  • Flow of Execution 

    1. Python reads the if condition

    2. Condition is evaluated

    3. If True → if block runs

    4. If False → else block runs

    5. Program continues after else

    Where if-else is Used?

    • Authentication systems

    • Result evaluation

    • ATM and banking logic

    • Validation in forms

    • Game win/lose logic