How to use if-elif-else in Python?

Understanding If-Elif-Else in Python

In Python, the if-elif-else statement is a powerful control flow structure that allows you to make decisions based on different conditions. It enables your program to execute different blocks of code depending on the evaluation of one or more conditions.

The Basics of If-Elif-Else

The basic structure of the if-elif-else statement in Python is as follows:

if condition1:
    # code block 1
elif condition2:
    # code block 2
elif condition3:
    # code block 3
...
else:
    # code block n

Here's how it works:

  1. The if statement checks the first condition (condition1). If it is True, the code block 1 is executed, and the rest of the elif and else blocks are skipped.
  2. If condition1 is False, the program moves to the next elif condition (condition2). If condition2 is True, the code block 2 is executed, and the rest of the elif and else blocks are skipped.
  3. This process continues for each elif condition until one of them is True or the else block is reached.
  4. If none of the if or elif conditions are True, the else block is executed.

Here's an example:

age = 25

if age < 18:
    print("You are a minor.")
elif age >= 18 and age < 65:
    print("You are an adult.")
else:
    print("You are a senior.")

In this example, the program will print "You are an adult." because the second condition (age >= 18 and age < 65) is True for the given age value of 25.

Nested If-Elif-Else Statements

You can also use if-elif-else statements inside other if-elif-else statements, creating a nested structure. This can be useful when you need to make complex decisions based on multiple conditions.

Here's an example:

grade = 85
attendance = 90

if grade >= 90:
    print("You got an A!")
elif grade >= 80:
    if attendance >= 90:
        print("You got a B+.")
    else:
        print("You got a B.")
elif grade >= 70:
    print("You got a C.")
else:
    print("You failed the course.")

In this example, the program first checks the student's grade. If the grade is 90 or above, the student gets an A. If the grade is between 80 and 90, the program then checks the student's attendance. If the attendance is 90 or above, the student gets a B+; otherwise, they get a B. If the grade is between 70 and 80, the student gets a C. If the grade is below 70, the student fails the course.

Mermaid Diagram for If-Elif-Else

Here's a Mermaid diagram that visualizes the flow of an if-elif-else statement:

graph TD Start --> If[if condition1] If -- True --> CodeBlock1[code block 1] If -- False --> Elif1[elif condition2] Elif1 -- True --> CodeBlock2[code block 2] Elif1 -- False --> Elif2[elif condition3] Elif2 -- True --> CodeBlock3[code block 3] Elif2 -- False --> Else[else] Else --> CodeBlockN[code block n] CodeBlock1 --> End CodeBlock2 --> End CodeBlock3 --> End CodeBlockN --> End End[End]

This diagram shows the decision-making process of the if-elif-else statement, where the program evaluates each condition and executes the corresponding code block.

Conclusion

The if-elif-else statement is a fundamental control flow structure in Python that allows you to make decisions based on different conditions. By understanding how to use it effectively, you can write more sophisticated and dynamic programs that can adapt to various scenarios. Remember to use clear and descriptive variable names, and consider using nested if-elif-else statements when you need to make complex decisions.

0 Comments

no data
Be the first to share your comment!