Conditional
statements
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
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
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.
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")
if-else
The else statement allows you to
execute a different block of code if the
if condition is False
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)
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.
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")
Nested if
Nested if statements mean an if
statement inside another if statement.
Let us code..

flow of control python.pdf

  • 1.
  • 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 Theselection 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 usto 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 checkeligibility of vote age = int(input("Enter your age ")) if age >= 18: # use ‘:’ to indicate end of condition. print("Eligible to vote")
  • 6.
    if-else The else statementallows you to execute a different block of code if the if condition is False
  • 7.
    Example:- Program tosubtract 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 statementallows you to check multiple conditions in sequence, and execute different code blocks depending on which condition is true.
  • 9.
    Example:- Check whethera 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 ifstatements mean an if statement inside another if statement.
  • 11.