Facebook Pixel

if Statement

Sometimes the programmer needs to check the evaluation of certain expressions and determine whether they evaluate to True or False. If the expression evaluates to False, the program execution follows a different path than it would if the expression had evaluated to True.

Based on this, conditional statements are classified into the following types: if, if...else, elif, and nested if.

if Statement:

A simple if statement works on the following principle:

  • Execute the block of code inside the if statement if the expression evaluates to True.
  • Ignore the block of code inside the if statement if the expression evaluates to False and return to the code outside the if statement.

if statement

Example:

applePrice = 180
budget = 200
if applePrice <= budget:
    print("Alexa, add 1kg Apples to the cart.")

Output:

Alexa, add 1kg Apples to the cart.