2. control flow
statements
1. Sequential - default mode
2. Selection - used for decisions
and branching
3. Repetition - used for looping,
i.e., repeating a piece of code
multiple times
3. Selection/Decision control statements
The selection statements are also known as Decision control statements or branching
statements.
It allows a program to test several conditions and execute instructions based on which
condition is true.
Some decision control statements are:
if
if-else
If-elif-else
nested if
4. if
It help us to run a particular code, but
only when a certain condition is met or
satisfied. A if only has one condition to
check.
5. Example:- To check eligibility of vote
age = int(input("Enter your age "))
if age >= 18: # use ‘:’ to indicate end of condition.
print("Eligible to vote")
7. Example:- Program to subtract smaller number from the larger
number and display the difference.
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if num1 > num2:
diff = num1 - num2
else:
diff = num2 - num1
print("The difference of",num1,"and",num2,"is",diff)
8. if-elif-else
The elif statement allows you to check
multiple conditions in sequence, and
execute different code blocks
depending on which condition is true.
9. Example:- Check whether a number is positive,negative, or zero.
number = int(input("Enter a number: ")
if number > 0:
print("Number is positive")
elif number < 0:
print("Number is negative")
else:
print("Number is zero")
10. Nested if
Nested if statements mean an if
statement inside another if statement.