Class XI · Chapter 6Unit 2, Python Programming (45 marks)5 min read
Share:WhatsAppLinkedIn

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:

  1. Sequence: Statements executed one after another
  2. Selection: Choosing a path based on condition
  3. 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-1
  • range(a, b) = a to b-1
  • range(a, b, c) = a to b-1 with step c
  • range(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

  1. Find output of if-elif-else code (2-3 marks)
  2. Find output of for/while loop (2-4 marks)
  3. Write a program using loops (3-5 marks): factorial, sum, pattern, prime check
  4. Rewrite code using for instead of while or vice versa (2 marks)
  5. break vs continue (2 marks): Difference with example
  6. range() function output (1-2 marks)
  7. Identify errors in loop code (1-2 marks)
  8. Nested loop output (2-3 marks)
  9. Pattern printing programs (3-4 marks)

Key Points Students Miss

  1. range(5) gives 0,1,2,3,4 (NOT 1 to 5 and NOT including 5)
  2. Indentation defines the block, wrong indentation = wrong logic
  3. break exits the entire loop; continue skips current iteration only
  4. while loop needs manual update of loop variable (else infinite loop)
  5. for-else and while-else: else block runs only if loop completes without break
  6. In nested loops, break only exits the innermost loop
  7. range() does not include the stop value (range(1,5) = 1,2,3,4)
  8. An if without else is valid, the else part is optional
  9. elif is used for multiple conditions (not else if, that's not Python syntax)
  10. Empty block needs pass statement: if x > 0: pass

Test Your Knowledge

Take a quick quiz on this chapter

Start Quiz →

Prefer watching over reading?

Subscribe for free.

Subscribe on YouTube