CONTROL STRUCTURES
Control structures allow you to control the flow of execution in a program. There are three basic types of control structures in Python:
Types of Control Structures
1. Sequential
Default mode: Code statements are executed sequentially, one line after another.
2. Selection (Decision-Making Statements)
Used for decisions and branching: Choose between two or more alternative paths. Python offers the following types of selection statements:
if
if..else
if..elif
- Nested
if
3. Iteration (Loops)
Used for looping: Repeat a piece of code multiple times while a condition evaluates to True
.
for
while
IF Statement
Selection/Decision-Making Statements
Decision-making statements control the flow of the program by evaluating a Boolean expression and executing specific code blocks based on the result.
Example:
# IF Statement Example
num = 8
if num > 5:
print("Value is above 5")
else:
print("Value is less than 5")
Output:
Value is above 5
Concept of Indentation
Indentation refers to the spaces given at the beginning of the code. It is Python’s way of defining code blocks. All statements within the same block must have the same level of indentation. For example:
if True:
print("This is inside the if block")
print("Indented properly")
If..Else Statement
# If..Else Example
num = int(input("Enter a number: "))
if num % 2 == 0:
print(f"{num} is even")
else:
print(f"{num} is odd")
Output:
Enter a number: 4
4 is even
If..Elif Statement
# If..Elif Example
marks = int(input("Enter your marks: "))
if marks >= 90:
print("Grade: A")
elif marks >= 75:
print("Grade: B")
elif marks >= 50:
print("Grade: C")
else:
print("Grade: F")
Output:
Enter your marks: 85
Grade: B
Nested If Statement
# Nested If Example
age = int(input("Enter your age: "))
if age > 18:
if age < 60:
print("You are eligible to work")
else:
print("You are eligible for retirement")
else:
print("You are too young to work")
Output:
Enter your age: 45
You are eligible to work
Try It Yourself
Problem 1: Check Divisibility
Write a program to check whether a given number is divisible by both 3 and 5.
Show Code
num = int(input("Enter a number: "))
if num % 3 == 0 and num % 5 == 0:
print(f"{num} is divisible by both 3 and 5")
else:
print(f"{num} is not divisible by both 3 and 5")
Problem 2: Determine Leap Year
Write a program to determine whether a given year is a leap year or not.
Show Code
year = int(input("Enter a year: "))
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print(f"{year} is a leap year")
else:
print(f"{year} is not a leap year")
else:
print(f"{year} is a leap year")
else:
print(f"{year} is not a leap year")
Problem 3: Determine the Largest of Three Numbers
Write a program to find the largest among three numbers.
Show Code
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a >= b and a >= c:
print(f"The largest number is {a}")
elif b >= a and b >= c:
print(f"The largest number is {b}")
else:
print(f"The largest number is {c}")