In decision making statement the control will flow only in one direction. Based upon the condition it will execute either True block or False block, here condition means relation between two operators. Decision statement “control” the flow of execution, they are also known as control statement.
There are two block’s if block and else block, if part is a true block and else part is a false block. The condition and expression will be written in if block.
Some logical conditions which are commonly used in Decision making statements are,
|Equals||==||a == b|
|Not Equals||!=||a != b|
|Less than||<||a < b|
|Less than or equal to||<=||a <= b|
|Greater than||>||a > b|
|Greater than or equal to||=||a >= b|
Python provides following decision making statements.
|if statement||If the condition is true the block of code will be executed.|
|If else Statement||If the condition is true the true block will execute, if the condition becomes false the else block code will execute.|
|nested if statements||When an if statement contains another if statement then it is a nested if statement.|
If is an conditional statement, if the condition becomes true then the block of code will be executed. In if statement we use if as keyword.
if expression: statement
a=30 b=20 if a>b: print("A is greater than B")
A is greater than B
If the condition becomes true the true block will execute, if the condition becomes false the else block will execute.
if condition: True block statements else: False block statements
a = 10 b = 20 if a > b: print("A is greater than B") else: print("B is greater than A")
B is greater than A
nested if statements
When an if statement contains another if statement then it is a nested if statement. The nested if statement allows us to check multiple conditions and will execute based upon the condition.
if expression_1: statement if expression_2: statement elif expression_3: statement else: statement
age = 21 if age < 18: print(" You are Minor and you are not eligible to work") else: if age >= 18 and age <= 60: print(" You are Eligible to Work ") else: print(" You are too old to work as per the Government rules")
You are Eligible to Work