Flow of Control
Flow of Control
Introduction
• Concept of Sequence: Programs execute statements in order, from beginning to end.
• Flow of Control: The order in which statements are executed in a program.
• Control Structures: Python supports selection and repetition to control the flow of the
program.
Example: Program 1
[ ]: # Program to print the difference of two input numbers
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
diff = num1 - num2
print("The difference of", num1, "and", num2, "is", diff)
Explanation: This program takes two integer inputs from the user, calculates their difference, and
prints the result. It demonstrates a simple sequence of operations: input, calculation, and output.
Selection
• if Statement: Executes a block of code if the condition is true.
Example: Voting Eligibility
[ ]: age = int(input("Enter your age: "))
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
Explanation: This program checks if the user’s age is 18 or above to determine voting eligibility. If
the condition (age >= 18) is true, it prints “Eligible to vote”; otherwise, it prints “Not eligible to
vote.”
• if..else Statement: Provides two paths based on whether the condition is true or false.
Example: Positive Difference (Program 2)
[ ]: # Program to print the positive difference of two numbers
num1 = int(input("Enter first number: "))
1
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)
Explanation: This program calculates the positive difference between two input numbers by com-
paring them and subtracting the smaller number from the larger one. It demonstrates the use of an
if..else statement to choose between two actions.
• if..elif..else Statement: Checks multiple conditions.
Example: Simple Calculator (Program 3)
[ ]: # Program to create a four function calculator
result = 0
val1 = float(input("Enter value 1: "))
val2 = float(input("Enter value 2: "))
op = input("Enter any one of the operator (+,-,*,/): ")
if op == "+":
result = val1 + val2
elif op == "-":
result = val1 - val2
elif op == "*":
result = val1 * val2
elif op == "/":
if val2 == 0:
print("Error! Division by zero is not allowed. Program terminated")
else:
result = val1 / val2
else:
print("Wrong input, program terminated")
print("The result is", result)
_Explanation: This program performs basic arithmetic operations (+, -, *, /) on two input numbers
based on the user’s choice of operator. It demonstrates the use of if..elif..else statements to handle
multiple conditions._
Indentation
• Indentation: Python uses indentation to define blocks of code. Proper indentation is crucial
to avoid syntax errors.
Example: Larger Number (Program 4)
[ ]: # Program to find the larger of the two numbers
num1 = 5
num2 = 6
if num1 > num2:
print("First number is larger")
print("Bye")
else:
print("Second number is larger")
print("Bye Bye")
Explanation: This program compares two numbers and prints which one is larger. The example
emphasizes the importance of indentation in Python to define blocks of code within if and else
statements.
Repetition
• Iteration: Repetition of a set of statements using looping constructs.
• For Loop: Executes a block of code a specific number of times.
Example: For Loop (Program 6)
[ ]: # Print the characters in word PYTHON using for loop
for letter in 'PYTHON':
print(letter)
Explanation: This program uses a for loop to iterate over each character in the string ‘PYTHON’
and prints them. It demonstrates the basic usage of the for loop.
Example: Sequence of Numbers (Program 7)
[ ]: # Print the given sequence of numbers using for loop
count = [10, 20, 30, 40, 50]
for num in count:
print(num)
Explanation: This program iterates over a list of numbers and prints each number. It shows how
the for loop can be used to traverse elements in a list.
Example: Even Numbers (Program 8)
[ ]: # Print even numbers in the given sequence
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for num in numbers:
if (num % 2) == 0:
print(num, 'is an even number')
Explanation: This program checks each number in the list to see if it’s even and prints the even
numbers. It combines a for loop with an if statement to filter elements based on a condition.
Example: Multiples of 10 (Program 9)
[ ]: # Print multiples of 10 for numbers in a given range
for num in range(5):
if num > 0:
print(num * 10)
Explanation: This program prints the multiples of 10 for numbers from 1 to 4 using the range
function in a for loop. It demonstrates how to use the range function to generate a sequence of
numbers.
range() Function
• range() Function: Generates a sequence of numbers. Useful for looping a specific number
of times.
Example: Range Function (Program 10)
[ ]: # Using range function in a for loop
for i in range(1, 6):
print(i)
Explanation: This program uses the range function to print numbers from 1 to 5. The range
function generates a sequence of numbers from 1 to 5, which the for loop iterates over.
Example: Reverse Range (Program 11)
[ ]: # Using range function to generate a reverse sequence
for i in range(5, 0, -1):
print(i)
Explanation: This program prints numbers from 5 to 1 in reverse order using the range function with
a negative step. It demonstrates how the range function can be customized to generate sequences
with different steps.
while Loop
• while Loop: Executes a block of code as long as the condition is true. Useful for situations
where the number of iterations is not known in advance.
Example: Counting Down (Program 12)
[ ]: i = 5
while i > 0:
print(i)
i -= 1
Explanation: This program uses a while loop to count down from 5 to 1. The loop continues to
execute as long as the condition (i > 0) is true.
Example: Sum of First N Natural Numbers (Program 13)
[ ]: # Using while loop to find sum of first n natural numbers
n = int(input("Enter a number: "))
sum = 0
i = 1
while i <= n:
sum += i
i += 1
print("Sum of first", n, "natural numbers is", sum)
Explanation: This program calculates the sum of the first n natural numbers using a while loop.
The loop iterates from 1 to n, adding each number to the sum.
break and continue Statements
• break Statement: Terminates the loop prematurely. Useful for breaking out of a loop based
on a condition.
Example: break Statement (Program 14)
[ ]: # Using break statement in a loop
for i in range(1, 11):
if i == 6:
break
print(i)
Explanation: This program uses a for loop to print numbers from 1 to 10 but breaks out of the loop
when i equals 6. The break statement exits the loop when the condition (i == 6) is met.
• continue Statement: Skips the current iteration and continues with the next. Useful for
skipping specific iterations based on a condition.
Example: continue Statement (Program 15)
[ ]: # Using continue statement in a loop
for i in range(1, 11):
if i % 2 == 0:
continue
print(i)
Explanation: This program uses a for loop to print odd numbers from 1 to 10 by skipping even
numbers. The continue statement skips the current iteration if the condition (i % 2 == 0) is true.
Nested Loops A loop inside another loop. Useful for iterating over multi-dimensional
structures like matrices
Example: Nested for Loop (Program 16)
[ ]: # Using nested for loops to print a pattern
for i in range(1, 4):
for j in range(1, 4):
print(i, j)
Explanation: This program demonstrates nested loops by printing pairs of numbers from 1 to 3.
Example: Multiplication Table (Program 17)
[ ]: # Using nested loops to print multiplication table
for i in range(1, 6):
for j in range(1, 6):
print(i * j, end=' ')
print()
Explanation: This program uses nested loops to print a multiplication table from 1 to 5.
Exercises
1. Write a program to find the maximum of three numbers.
[ ]:
2. Create a program to check if a number is prime.
[ ]:
3. Write a program to print the Fibonacci sequence up to n terms.
[ ]:
4. Develop a program to calculate the factorial of a given number.
[ ]:
5. Create a program to print all Armstrong numbers in a given range.
[ ]: