CONTROL STRUCTURES
Flow of control is implemented with three basic types of control structures:
-
Sequential: default mode. Sequential execution of code statements (one line after another)
-
Selection: used for decisions, branching -- choosing between 2 or more alternative paths. In Python, these are the types of selection statements:
-
if
-
if..else
-
if ..elif
-
Nested if
-
-
Iteration: used for looping, i.e. repeating a piece of code multiple times while condition evaluates to true. In Python, there are two types of loops:
-
for
-
while
-
IF Statement
Selection/ Decision making statements
These are decision-making statements decide which statement to execute and when. Decision-making statements evaluate the Boolean expression and control the program flow depending upon the result of the condition provided.
Example:
num=8
if num>5:
print("Value is above 5")
else:
print("Value is less than 5")
Output:
Value if above 5
Concept of indent
Indentation refers to the spaces given at the beginning of the code. It is a way of telling a Python interpreter that the group of statements belongs to a particular block of code.
If..else Statement
Code
Output
If..elif Statement
Code
Output
Nested IF Statement
Code
Output
Try It Yourself
Problem Statement 1 - Input the three numbers and find out the smallest number out of those three.
Problem Statement 2 - Input class and section as two variables. If class is either 11 or 12 and section is either 'C' or 'D' then display Stream as 'Commerce'. If section is 'E' then display stream as 'Humanities'.