Conditional or Decision Making Statement
Program that causes a jump of control from one block to the other is called conditional statements.
There are four types:
- if
- if…else
- if…elif…else
- nested if
if statement
If the test expression is true then the statement block or “body of if” will be executed.
Syntax:
if text expression:
statement (s) or body of if
Flow chart:
Example
A = 20
B = 30
if A<B:
print(“A is less than B”)
Output
A is less than B
if…else statement
If the test expression is true then “body of if” will be executed. Otherwise if the test expression is false then “body of else” will be excecuted
Syntax:
if text expression:
body of if
else:
body of else
Flow chart:
Example
A = 20
B = 30
if A<B:
print(“A is less than B”)
else:
print(“A is not less than B”)
Output
A is less than B
if…elif…else statement
If there are more than two possibilities then we need more than two branches. Each condition is checked in order. If the first condition is false then the next condition is checked and son on. If anyone of the condition is true then the corresponding branch will be executed
Syntax:
if text expression:
body of if
elif test expressin:
body of elif
elif test expressin:
body of elif
else:
body of else
Flow chart:
Example
A = 20
B = 30
if A!=B:
print(“A is not equal to B”)
elif A<B:
print(“A is less than B”)
else:
print(“A is not less than B”)
Output
A is not equal to B
Nested if statement
If statement within the another if statement is called nested if statement
Syntax:
if text expression:
body of if
if test expressin:
body of if
else:
body of else
else:
body of else
Flow chart:
Example
A = 20
B = 30
if A!=B:
print(“A is not equal to B”)
if A<B:
print(“A is less than B”)
else:
print(“A is not less than B”)
else:
print(“A is equal to B”)
Output
A is not equal to B
A is less than B





No comments:
Post a Comment