Python if....elif....else statements

python if... Statements:

             python if.. statement is used for decision making operation.
     It contains the body of code which will run only when the conditions given in the if statement is true

if only ---- when condition True :

if.. flowchart


Example:
#if only ------ condition true
x = 100
y = 50
if x > y:
print("greater then y")
print("condition true")
Output:
greater then y
condition true

            In this example we have two variable x=100,y=50 and we create if expression [if x > y:] then we give statement [print("greater then y")]

   if condition expression is true then statement is pass .then outside of if statement print
will be execute.

if only ---- when condition False:

            In this example we have two variable x=100,y=50 and we create if expression [if x < y:] then we give  statement [print("less then x")] 

    if condition expression is false then statement is also fail .then outside of if statement print
will be execute. 

Example:
#if only ------ condition false
x = 100
y = 50
if x < y:
print("less then x")
print("condition false")
Output:
condition false

python if...elif Statements:

if....else ---- condition True:

if...else flowchart

Example:
#if else ----- condition true
x = 100
y = 50
if x > y:
print("greater then y")
else:
print("condition true")
Output:
greater then y
            In this example we have two variable x=100,y=50 and we create if..else expression [if x > y:] then we give statement [print("greater then y")] and else statement [print("condition true")]

if condition expression is true then statement is pass .then if statement will be execute.

if....else ----- when condition False:

            In this example we have two variable x=100,y=50 and we create if..else expression [if x < y:] then we give statement [print("condition false")] and else statement [print("less than x")]

    if condition expression is false then statement is also fail .then else statement will be execute.

Example:
#if only ------ condition false
x = 100
y = 50
if x < y:
print("condition false")
else:
print("less than x")
Output:
less than x

python if...elif...else Statements:

if....elif...else-----condition True:

if...elif...else flowchart


Example:
#if..elif...else ------ condition True
x = 100
y = 100
if x < y:
print("condition false")
elif x == y:
print("x and y is equal")
else:
print("less than x")

Output:
x and y is equal
            In this example we have two variable x=100,y=100 and we create if.. expression [if x < y:] then we give statement [print("condition false")] then we have create elif..expression [elif x == y:] then we give statement [print("x and y is equal")] and else statement [print("less than x")]

if condition expression is false and its go on another elif expression is true then statement will be execute.

if...elif...else condition False:

            In this example we have two variable x=100,y=50 and we create if.. expression [if x < y:] then we give statement [print("condition false")] then we have create elif..expression [elif x == y:] then we give statement [print("x and y is equal")] and else statement [print("less than x")]

            if  condition expression is false and its go on another elif expression is also false then its goes on else expression statement will be execute


Example:

#if..elif...else ------condition False
x = 100
y = 50
if x < y:
print("condition false")
elif x == y:
print("x and y is equal")
else:
print("less than x")
Output:
less than x




Comments

Popular posts from this blog

What is InnoDB - Why InnoDB use - ACID properties

Inheritance-Object Oriented Programming (OOPs) concept in python