Chapters
- 1.Computer System Organization
- 2.Data Representation and Boolean Logic
- 3.Getting Started With Python
- 4.Python Programming Fundamentals
- 5.Conditional And Looping Construct
- 6.Strings Manipulation In Python
- 7.List Manipulation In Python
- 8.Tuples and Dictionary
- 9.Introduction to Python Modules
- 10.Society, Law and Ethics
Chapter 5: Conditional and Looping Constructs
Introduction
The order in which statements are executed in a Python program is called the flow of execution. Python provides different types of execution flows to control how your program runs.
This chapter covers two important control structures: conditional statements for making decisions and looping constructs for repeating actions.
Types of Execution Flow
1. Sequential Execution
Statements are executed one after another in the order they appear, from top to bottom without any jumps.
age = 25
print("Hello")
print("Welcome")2. Selection/Conditional
Execution jumps to different blocks based on whether conditions are true or false.
if age >= 18:
print("Adult")
else:
print("Minor")3. Iteration/Looping
Execution repeats a block of code multiple times based on a condition.
for i in range(3):
print(i)
# Prints: 0 1 2Conditional Statements
1. if Statement
The simplest form of conditional statement. Executes a block of code only if a condition is true.
Syntax:
if condition:
StatementsExample:
age = int(input("Enter your Age: "))
if age >= 18:
print("Eligible to Vote")2. if-else Statement
Provides an alternative path when the condition is false. The else block executes if the condition is not met.
Syntax:
if condition:
Statements
else:
StatementsExample:
age = int(input("Enter your Age: "))
if age >= 18:
print("Eligible to Vote")
else:
print("Not Eligible to vote")3. if-elif-else Statement
Allows multiple conditions to be checked in sequence. Useful when you have more than two possibilities.
Syntax:
if condition:
Statements
elif condition:
Statements
else:
StatementsExample 1: Check if number is Positive or Negative
a = int(input("enter a Number: "))
if a > 0:
print("Number is Positive")
elif a < 0:
print("Number is Negative")
else:
print("Number is Zero")Example 2: Grading System
marks = int(input("Enter Your Mark: "))
if marks >= 90:
print("Grade A+")
elif marks >= 80:
print("Grade A")
elif marks >= 70:
print("Grade B")
elif marks >= 60:
print("Grade C")
elif marks >= 50:
print("Grade D")
else:
print("Grade F - Fail")4. Nested if Statements
An if statement inside another if statement (or other conditional statements). Used for complex decision-making.
Syntax:
if condition:
Statements
if condition:
Statements
elif condition:
Statements
else:
Statements
elif Condition:
Statements
else:
StatementsExample:
mark = int(input("Enter Your Mark: "))
if mark >= 80:
print("First Class")
if mark >= 90:
print("Rank Holder ")
elif mark <= 70 and mark >= 36:
print("Second Class")
else:
print("Fail")Looping Constructs
1. for Loop
Used to iterate over a sequence (list, tuple, string, range, etc.).
Syntax:
for variable in sequence:
statement(s)Examples:
# 1. Iterating over a range
for i in range(5):
print(i) # Prints 0, 1, 2, 3, 4
# 2. Iterating over a string
for char in "Python":
print(char)
# 3. Iterating over a list
fruits = ["apple", "banana", "orange"]
for fruit in fruits:
print(fruit)2. while Loop
Repeats a block of code as long as a condition is true.
Syntax:
while condition:
statement(s)Example:
count = 1
while count <= 5:
print(f"Count: {count}")
count += 1
# Example: Input validation
password = ""
while password != "secret":
password = input("Enter password: ")3. range() Function
Generates a sequence of numbers, commonly used with for loops.
Different forms of range():
# 1. range(stop)
for i in range(5):
print(i) # 0, 1, 2, 3, 4
# 2. range(start, stop)
for i in range(2, 8):
print(i) # 2, 3, 4, 5, 6, 7
# 3. range(start, stop, step)
for i in range(1, 10, 2):
print(i) # 1, 3, 5, 7, 9
# Reverse range
for i in range(10, 0, -1):
print(i) # 10, 9, 8, 7, 6, 5, 4, 3, 2, 14. Nested Loops
Loops inside other loops for handling multi-dimensional data or complex patterns.
Example:
# Example: Multiplication table
for i in range(1, 4):
for j in range(1, 4):
print(f"{i} x {j} = {i*j}")
print() # Empty line after each table
# Example: Pattern printing
for i in range(1, 6):
for j in range(i):
print("*", end="")
print() # New line after each rowLoop Control Statements
break Statement
Terminates the loop immediately when encountered.
for i in range(10):
if i == 5:
break
print(i) # Prints 0,1,2,3,4continue Statement
Skips the rest of the current iteration and moves to the next.
for i in range(5):
if i == 2:
continue
print(i) # Prints 0,1,3,4Key Points to Remember
- Python uses indentation to define code blocks (not braces)
- The
ifstatement executes code only when the condition is True - The
elseblock is optional and executes when condition is False elifallows checking multiple conditionsforloop is best for iterating over sequenceswhileloop is best when number of iterations is unknownbreakexits the loop completelycontinueskips to the next iteration- Always ensure while loops have a terminating condition to avoid infinite loops
Related Resources
- Computer Hardware Tutorial
- Operating Systems Basics
- Computer System Cheatsheet
- Practice MCQs
- Input/Output Devices Guide
- Memory Types Explained
Need Help?
Join our tuition classes for personalized guidance and doubt clearing.
Register for Classes →