if Statement

 

if Statement

Sometimes the programmer needs to check the evaluation of certain expression(s), whether the expression(s) evaluate to True or False. If the expression evaluates to False, then the program execution follows a different path then it would have if the expression had evaluated to True.


Based on this, the conditional statements are further classified into following types; if, if……else, elif, nested if.

 

if Statement:

A simple if statement works on following principle,


  • execute the block of code inside if statement if the expression evaluates True.
  • ignore the block of code inside if statement if the expression evaluates False and return to the code outside 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.