DEV Community

Romulo Gatto
Romulo Gatto

Posted on

Conditional Statements (if, else, elif)

Conditional Statements (if, else, elif)

In Python programming, conditional statements are used to execute different code blocks based on certain conditions. The three main types of conditional statements in Python are if, else, and elif.

The if Statement

The if statement is used to check a specific condition and execute a block of code only if the condition is true. Here's the basic syntax of an if statement:

if condition:
    # Code to be executed when the condition is true
Enter fullscreen mode Exit fullscreen mode

Let's look at an example:

age = 18

if age >= 18:
    print("You are eligible to vote!")
Enter fullscreen mode Exit fullscreen mode

In this example, we're checking whether the variable age is greater than or equal to 18. If it is true, "You are eligible to vote!" will be printed.

The else Statement

The else statement allows for executing a block of code when the if condition evaluates to false. It complements the if statement and provides an alternative action if the initial condition was not met.

Here's how you can use it:

age = 16

if age >= 18:
    print("You are eligible to vote!")
else:
    print("You cannot vote yet.")
Enter fullscreen mode Exit fullscreen mode

In this case, since the value of age is less than 18, "You cannot vote yet." will be printed as output.

The elif Statement

elif stands for "else if" which means that we can have multiple branching options using this keyword. It allows us to check additional conditions after an initial if statement without having nested ifs.

Here's an example that demonstrates its usage:

num = int(input("Enter a number: "))

if num > 0:
    print("Positive")
elif num < 0:
    print("Negative")
else:
    print("Zero")
Enter fullscreen mode Exit fullscreen mode

In this example, the user is prompted to enter a number. The program then checks if the number is greater than zero. If true, "Positive" will be printed. If not, it moves on to the next condition and checks if the number is less than zero using elif. Finally, if neither condition is true (meaning the number must be zero), "Zero" will be printed.

Nested Conditional Statements

You can also have nested conditional statements by placing one if statement inside another. This allows for additional levels of branching when necessary.

For instance:

num = int(input("Enter a number: "))

if num > 0:
    if num % 2 == 0:
        print("Positive even")
    else:
        print("Positive odd")
elif num < 0:
    if abs(num) % 2 == 0:
        print("Negative even")
    else:
        print("Negative odd")
else:
    print("Zero")
Enter fullscreen mode Exit fullscreen mode

In this example, depending on whether the value of num is positive or negative and odd or even, a different message will be displayed accordingly.

Conditional statements are incredibly useful in programming as they allow us to control flow based on certain conditions. Keep experimenting with them to unlock their full potential in your Python programs!

Top comments (0)