In this tutorial you will learn about some very interesting condition or decision making statements. You must have noticed usage of if or if else statement in the some examples of previous tutorials. If you have a programming backing then you would be familiar with the working of if ..else statement
The ‘if ’ statement evaluates a condition and returns true if the condition is true and false if the condition is false. In most cases an ‘else’ statement can be used in combination with the ‘if’ statement. If the condition is true block of code associated with the if statement is executed or else the block of code associated with the else block is executed.
The syntax for if…else statement is as follows:
if condition_true: do this else : do this
Try the following code:
var1 = 20 if (var1%2==0): print("var1 is even") else: print("var1 is odd")
elif statement is useful when you want to check multiple conditions. The syntax for elif condition is given below:
if condition_true: do this elif condition_true: do this elif condition_true: do this else: do this
Try out the following code:
var1 = 20 if (var1%3==0): print("var1 is divisible by 3") elif (var1%6==0): print("var1 is divisible by 6") elif (var1%7==0): print("var1 is divisible by 7") else : print("var1 is not divisible by 3, 6 and 7")