Python control flow :Understanding and using if/Elif / else statements

Control flow in programming refers to the order in which the computer executes the instructions in a program.

In Python, one way to control the flow of a program is by using if/elif/else statements. These statements allow the program to make decisions and choose different paths of execution based on certain conditions.

This blog post will cover the basics of if/elif/else statements in Python, including syntax, usage, and examples to help you understand how to use these statements in your own programs.

If statement

the basic syntax of an if statement is:

if codition:
    # code to be executed if the statement is true

In Python, the "if" statement is used to only run a block of code if a certain condition is met.

If the condition is True, the code block that follows the if statement will be executed. If the condition is False, the program will skip the code block in question and instead move to any other code that follows the if statement.

For example, if you want to check if a variable "age" is greater than 18, and if so, let the user know that they are eligible to vote:

age = 19
if age > 18:
    print("You are eligible to vote.")

The code block within the if statement will run since the age is more than 18, printing "You are eligible to vote" to the console.

age = 17
if age > 18:
    print("You are eligible to vote.")

In this case, the code block within the if statement will be ignored since the age is less than 18, so it will not print anything.

Elif statement

The syntax of the elif statement is similar to the if statement, with the keyword "elif" followed by a condition in parentheses.

if condition1:
    # code to execute if condition1 is true
elif condition2:
    # code to execute if condition1 is false and condition2 is true

The elif statement, short for "else if", is used to test multiple conditions in a program. It is used after the if statement and allows you to specify additional conditions that will be checked if the original condition of the if statement is not true.

For example, you can check if a variable "age" is equal to 18 and then print a message, and if not, print a different message.

age = 17

if age == 18:
    print("You are eligible to vote")
elif age < 18:
    print("You are not eligible t vote")

In this example, the if statement checks if the value of the variable "age" is equal to 18, if it is then it will print "You are eligible to vote". If the condition is false, the elif statement will check if the value of "age" is less than 18. If this condition is true, it will print "You are not eligible to vote".

Else statement

The basic syntax is as follows:

if condition:
    # code to execute if condition is True
else:
    # code to execute if condition is False

The else statement is used to specify a block of code that will be executed if none of the conditions in the preceding if or elif statements are true.

for example;

age = 25

if age < 18:
    print("You are not eligible to vote.")
elif age == 18:
    print("Congratulations on becoming eligible to vote!")
else:
    print("You are eligible to vote.")

In this example, the code checks the value of the variable "age" against three conditions:

  1. If the value age is less than 18, it will print "You are not eligible to vote."

  2. If the value age is equal to 18, it will print "Congratulations on becoming eligible to vote!"

  3. If the value age is greater than 18, it will print "You are eligible to vote."

So, if the first condition (age < 18) is met the subsequent conditions (elif and else) are not going to be checked as the code block is executed and the control flow jumps out of the if-elif-else structure.

Best practices for using if/elif/else statements

When using multiple if, elif, and else statements in a program, it's important to structure them in a way that is easy to understand and maintain.

Here are a few best practices to follow

  • Order conditions from most specific to least specific

  • Keep the number of conditions and levels of indentation to a minimum

  • Use clear and easy-to-read conditions

  • Use indentation consistently to indicate the different code blocks

  • Use the else statement as a catch-all

  • Use elif statement to check for additional conditions

Conclusion

This blog post explores If/Elif/Else statement's basic, syntax, usage and examples including their best practices. By following these best practices, you can create code that is easy to understand, less prone to bugs and more maintainable.

Reference

Thanks for reading. See you in my next blog article.