UNIT1 - Conditional Statments
UNIT1 - Conditional Statments
Conditional Statement
o A conditional statement as the name suggests itself, is used to handle conditions in
your program.
o These statements guide the program while making decisions based on the
conditions encountered by the program.
o Conditional statements in Python support the usual logical conditions
from mathematics.
o For example:
o Equals: a == b
o Not Equals: a != b
o Less than: a < b
o Less than or equal to: a <= b
o Greater than: a > b
o Greater than or equal to : a >= b
if Statement:
o The if statement is the simplest decision-making statement.
o It is used to decide whether a certain statement or block of statements will be
executed or not.
Syntax:
if condition:
statement1
.
statementn
The statement consists of a header line that ends with the colon character (:) followed by
an indented block.
Example:
a=5
if a > 0:
print(“a is positive”)
Output:
a is positive
Explanation: In this code, a variable value assign as 5 then check the condition value of a>0
(i.e 5>0), checks result is true, so the output of the code is “a is positive”.
if….else statement
The if statement alone tells us that if a condition is true it will execute a block of statements and
if the condition is false it won’t. But if we want to do something else if the condition is false, we
can use the else statement with the if statement to execute a block of code when the if condition
is false.
It is used to execute the statement(s) as per two possibilities and the conditions.
Syntax of if…else statement
if condition:
Statement1
.
Statementn
else:
Statement1
.
Statementn
Since the condition must either be true or false, exactly one of the alternatives will be
executed. The alternatives are called branches, because they are branches in the flow of
execution.
Example:
a=9
if a > 0:
print('a is positive')
else:
print("a is negative")
Output:
a is positive
Flowchart:
Explanation: In this code, a variable value assign as 9, then checks the condition value of a>0
(i.e 9>0). The condition result is true, so the output of the code is “an is positive”.
Here, a user can decide among multiple options. The if statements are executed from the top
down.
As soon as one of the conditions controlling the if is true, the statement associated with that if is
executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final
“else” statement will be executed.
Syntax of if..elif..elif ..else statement
if condition:
Statement1
.
Statementn
elif:
Statement1
.
Statementn
.
.
.
else:
Statement1
.
Statementn
Example:
a=0
if a > 0:
print('a is positive')
elif a<0:
print("a is negative")
else:
print('a is zero')
Output:
a is zero
Flowchart
Explanation: In this code, a variable value assign as 0 then check the condition value of a>0 (i.e
0>0), condition result is False, then check the next condition value of a<0(i.e 0<0), again condition
result is False, then it display else part statement as output “a is zero”.
Nested Condition
Nested conditions comprise condition statements contained within the definition
of other condition statements.
We can use an if statement inside of an if statement. This is known as a nested if
statement.
Example
a=5
if a>=0:
if a==0:
print("a is zero")
else:
print("a is positive")
else:
print("a is negative")
Output:
a is positive
Flowchart: