2. Conditional Statements (Decision Making)
Python language provide the following conditional (Decision making) statements.
If statements
Else statements
Elif statements
Nested if
statements
3. If Statement
In Python, If Statement is used for decision making.
It will run the body of code only when IF statement is true.
When you want to justify one condition while the other condition is not true, then
you
use "if statement".
Syntax:
5. What happen when "if condition" does not
meet
In this step, we will see what happens when your "if condition" does not
meet.
6. Code Line 5: We define two variables x, y = 8, 4
Code Line 7: The if Statement checks for condition x<y which is False in
this case
Code Line 8: The variable st is NOT set to "x is less than y."
Code Line 9: The line print st - is trying to print the value of a variable that was
never declared. Hence, we get an error.
7. How to use "else statement"
The "else statement" is usually used when you have to judge one statement on the
basis of other.
If one condition goes wrong, then there should be another condition that should
justify the statement or logic.
8. Code Line 5: We define two variables x, y = 8, 4
Code Line 7: The if Statement checks for condition x<y which is False in this case
Code Line 9: The flow of program control goes to else condition
Code Line 10: The variable st is set to "x is greater than y."
Code Line 11: The line print st will output the value of variable st which is "x is greater
than y"
9. When "else condition" does not work
There might be many instances when your "else condition" won't give you the desired result.
It will print out the wrong result as there is a mistake in program logic.
In most cases, this happens when you have to justify more than two statement or condition in a
program.
10. How to use "elif" statement
To correct the previous error made by "else condition", we can
use "elif" statement.
By using "elif" condition, you are telling the program to print
out the third
condition or possibility when the other condition goes wrong
or incorrect.
11. Code Line 5: We define two variables x, y = 8, 8
Code Line 7: The if Statement checks for condition x<y which is False in this case
Code Line 10: The flow of program control goes to the elseif condition. It checks
whether x==y which is true
Code Line 11: The variable st is set to "x is same as y."
Code Line 15: The flow of program control exits the if Statement (it will not
get to the else Statement).
And print the variable st. The output is "x is same as y" which is correct
12. How to execute conditional statement with
minimal code
Syntax:
A If B else C
Example:
13. Code Line 2: We define two variables x, y = 10, 8
Code Line 3: Variable st is set to "x is less than y "if x<y or
else it is set to "x is greater than or equal to y".
In this x>y variable st is set to "x is greater than or equal
to y."
Code Line 4: Prints the value of st and gives the correct output
14. Nested If Statement
When there is an if statement (or if..else or if..elif..else) is present inside another if
statement (or if..else or if..elif..else) then this is calling the nesting of control
statements.
Here we have a if statement inside another if..else statement block. Nesting control
statements makes us to check multiple conditions.
num = -99
if num >
0
pri
nt(
“po
siti
ve
nu
mb