Blog Post #26: What if It’s False? Adding an else Block

In Post #25, we took a major step in controlling our program’s flow with the if statement. We learned how to execute a block of code only when a condition is True. This is powerful, but it leaves a question unanswered: what happens if the condition is False?

Currently, our program just silently skips the if block and moves on. In this post, we will learn how to create an alternative path for when the condition is not met by using the else keyword.

The if Statement’s Other Half

The else statement provides a block of code that is guaranteed to run when the if statement’s condition evaluates to False. Think of it as the “otherwise” or “if not” branch of your logic. It creates a true two-way split:

  • If the condition is True, the if block runs.
  • If the condition is False, the else block runs.

One and only one of these blocks will ever be executed.

The if-else Syntax

The structure is a natural extension of the if statement.

if condition:
    # This code runs if the condition is True.
else:
    # This code runs if the condition is False.

There are two key syntax points to remember:

  1. The else keyword must be at the same indentation level as its corresponding if keyword.
  2. The else keyword is also followed by a colon (:), which introduces its own indented block of code.

if-else in Action

Let’s see how this improves our previous examples.

Example 1: The Voting Age Revisited

In our last post, we checked if a user was old enough to vote, but we didn’t have a message for those who were too young. Let’s fix that.

age = 16

if age >= 18:
    print("You are old enough to vote.")
else:
    print("You are not yet old enough to vote.")

If you run this with age = 16, the if condition (16 >= 18) is False, so the else block is executed, printing the second message. If you change age to 20, the if condition becomes True, and the first message is printed instead.

Example 2: A Simple Password Check

The if-else structure is perfect for situations with two clear outcomes, like checking a password.

correct_password = "python123"

user_input = input("Enter the password: ")

if user_input == correct_password:
    print("Access granted. Welcome!")
else:
    print("Access denied. Incorrect password.")

print("Program finished.")

This program now gives clear feedback to the user whether their input was correct or incorrect. It provides a complete experience by handling both possibilities.

What’s Next?

You’ve now upgraded your decision-making tool. The if-else structure allows you to create a clear, two-path road for your program’s logic, ensuring that one of two actions is always taken based on a condition.

This is perfect for “either/or” situations. But what if you have more than two possible outcomes? For example, what if a movie ticket costs different amounts for a child, an adult, and a senior? To handle these multi-path decisions, we need another tool. In Post #27, we will learn how to check for multiple different conditions in a row using the elif keyword.

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment