Tuesday 16 January 2024

If / elif / else

  • The if statement checks whether condition_1 is true. If it is true, the code block under if is executed, and the program skips the elif and else blocks.

  • The elif statement (optional) allows you to check additional conditions if the preceding if condition is false. If the elif condition is true, the code block under elif is executed.

  • The else statement (also optional) provides a code block that is executed when none of the previous conditions (the if and elif conditions) is true.

Here's an example to illustrate:

x = 10 if x > 15: (# Condition 1) print("x is greater than 15") elif x > 5: (# Condition 2) print("x is greater than 5 but not greater than 15") else: (# Code to be excuted if condition 1 and 2 is false) print("x is 5 or less")


In this example, x is assigned the value of 10. The program checks the conditions in order: first, if x is greater than 15, then if it's greater than 5, and finally, if neither of those conditions is true, it executes the code under else. In this case, the output will be "x is greater than 5 but not greater than 15."

No comments:

Post a Comment

How to connect to VPS with Private Key on Mac OS

1. Use the cd command to navigate to the directory where your private key file is stored. For example, if your private key is in the Downlo...