Class XI · Chapter 6Unit 2, Python Programming (45 marks)5 min read
Chapter 6: Flow of Control
CBSE Unit: Unit 2, Python Programming (45 marks) Marks Weightage: ~8-10 marks (if-else, loops are heavily tested) Priority: CRITICAL, core programming concepts, programs asked in every exam
Key Concepts
6.1 Flow of Control
The order of execution of statements in a program. Three types:
- Sequence: Statements executed one after another
- Selection: Choosing a path based on condition
- Repetition: Repeating a block of code
6.2 Selection Statements
if Statement
if condition:
statement(s) # executed if condition is True
if-else Statement
if condition:
statement(s) # if True
else:
statement(s) # if False
if-elif-else Statement
if condition1:
statement(s)
elif condition2:
statement(s)
elif condition3:
statement(s)
else:
statement(s) # if none of the above are True
Nested if
if condition1:
if condition2:
statement(s)
else:
statement(s)
else:
statement(s)
6.3 Indentation, Python uses indentation (spaces/tabs) to define blocks of code, All statements in a block must have the same indentation level
- Standard: 4 spaces per indentation level
- IndentationError raised for incorrect indentation
6.4 Repetition (Loops)
for Loop
for variable in sequence:
statement(s)
# Examples:
for i in range(5): # 0, 1, 2, 3, 4
print(i)
for i in range(1, 6): # 1, 2, 3, 4, 5
print(i)
for i in range(1, 10, 2): # 1, 3, 5, 7, 9
print(i)
for ch in "Hello": # H, e, l, l, o
print(ch)
for item in [10, 20, 30]: # 10, 20, 30
print(item)
range() function: range(start, stop, step)
range(n)= 0 to n-1range(a, b)= a to b-1range(a, b, c)= a to b-1 with step crange(10, 0, -1)= 10, 9, 8, ..., 1
while Loop
while condition:
statement(s)
# update loop variable (to avoid infinite loop)
# Example:
i = 1
while i <= 5:
print(i)
i += 1
6.5 break and continue
break Statement
- Terminates the loop immediately, Control moves to the statement after the loop
for i in range(1, 11):
if i == 5:
break
print(i) # Prints: 1, 2, 3, 4
continue Statement
- Skips the current iteration and moves to next iteration
for i in range(1, 11):
if i == 5:
continue
print(i) # Prints: 1, 2, 3, 4, 6, 7, 8, 9, 10
6.6 Nested Loops
for i in range(1, 4):
for j in range(1, 4):
print(i * j, end=" ")
print()
# Output:
# 1 2 3
# 2 4 6
# 3 6 9
6.7 Loop else
Python allows else with loops, executes when loop completes normally (not via break).
for i in range(5):
print(i)
else:
print("Loop completed") # Executes after loop finishes
Programs/Code Examples
Check Even/Odd
num = int(input("Enter number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
Find Largest of Three Numbers
a = int(input("Enter first: "))
b = int(input("Enter second: "))
c = int(input("Enter third: "))
if a >= b and a >= c:
print(a, "is largest")
elif b >= a and b >= c:
print(b, "is largest")
else:
print(c, "is largest")
Sum of First N Natural Numbers
n = int(input("Enter N: "))
total = 0
for i in range(1, n + 1):
total += i
print("Sum =", total)
Factorial of a Number
n = int(input("Enter number: "))
fact = 1
for i in range(1, n + 1):
fact *= i
print("Factorial =", fact)
Print Multiplication Table
num = int(input("Enter number: "))
for i in range(1, 11):
print(num, "x", i, "=", num * i)
Check Prime Number
num = int(input("Enter number: "))
if num > 1:
for i in range(2, num):
if num % i == 0:
print("Not prime")
break
else:
print("Prime")
else:
print("Not prime")
Pattern Printing (Star Triangle)
n = int(input("Enter rows: "))
for i in range(1, n + 1):
print("*" * i)
Common Board Exam Question Patterns
- Find output of if-elif-else code (2-3 marks)
- Find output of for/while loop (2-4 marks)
- Write a program using loops (3-5 marks): factorial, sum, pattern, prime check
- Rewrite code using for instead of while or vice versa (2 marks)
- break vs continue (2 marks): Difference with example
- range() function output (1-2 marks)
- Identify errors in loop code (1-2 marks)
- Nested loop output (2-3 marks)
- Pattern printing programs (3-4 marks)
Key Points Students Miss
range(5)gives 0,1,2,3,4 (NOT 1 to 5 and NOT including 5)- Indentation defines the block, wrong indentation = wrong logic
breakexits the entire loop;continueskips current iteration onlywhileloop needs manual update of loop variable (else infinite loop)for-elseandwhile-else: else block runs only if loop completes without break- In nested loops,
breakonly exits the innermost loop range()does not include the stop value (range(1,5) = 1,2,3,4)- An
ifwithoutelseis valid, the else part is optional elifis used for multiple conditions (notelse if, that's not Python syntax)- Empty block needs
passstatement:if x > 0: pass
Prefer watching over reading?
Subscribe for free.