BORING EDUCATION — LIVE EXPERIMENTProduct is live. Experiments are running. Feedback is being collected. Improvements are underway.Welcome to Boring Education. We're glad you're here while we're figuring out how to make learning better.
BORINGEDUCATION

Python Control Structures (if, elif, else, while and for): PECTAA Class 11 Computer Science, Punjab Board

25 October 2026

Every useful program has to decide and repeat: print a warning only when it is hot, greet each friend in a list, count until a limit is reached. Python does this with a small set of control structures, and the book teaches all of them in one section.

Chapter 2 of the PECTAA Computer Science 11 book introduces Python. After operators and expressions (section 2.3), section 2.4 covers control structures: decision making and looping. This note is for FSc Part 1 (1st year, Class 11) students in Lahore, Faisalabad, Sargodha, Rawalpindi, Multan, Gujranwala and the other Punjab boards. Gujrat district comes under the Gujranwala board. PECTAA writes the books. Your own board sets your paper.

What to know

Control structures change the flow of a program: they choose between actions or repeat actions. The book names two main types.

Decision making (2.4.1).

  • if statement. Runs a block only when its condition is true.
temperature = 35
if temperature > 30:
    print("It is a hot day")
  • if-else statement. One block for true, another for false. With temperature = 15, the book's example prints "It's not a hot day".
  • Short-hand if-else. A one-line form: action_if_true if condition else action_if_false.
temperature = 15
m = "It's a hot day" if (temperature > 30) else "It's not a hot day"
print(m)
  • if-elif-else statement. Checks several conditions in turn and runs the first block whose condition is true; else catches the rest.
weather = "cloudy"
if weather == "sunny":
    print("Wear sunglasses")
elif weather == "rainy":
    print("Take an umbrella")
else:
    print("Enjoy your day!")

With weather = "cloudy" the output is "Enjoy your day!".

Looping (2.4.2). Loops repeat actions and make code shorter and easier to read.

  • while loop. Runs as long as its condition is true, checking it before each pass.
number = 1
while number < 10:
    print(number)
    number += 1
  • for loop. Repeats a block for each element of a sequence (list, tuple or string), or for each number from range().
friends = ["Ahmad", "Ali", "Hassan"]
for friend in friends:
    print("Hello", friend)

This prints a greeting for each of the three friends.

Indentation. Python uses indentation, not brackets, to mark blocks. The body of every if, elif, else, while and for is indented under a line ending in a colon. Wrong indentation changes the meaning or gives an error.

Key points

  • Two families: decision making and looping.
  • Conditions use comparison operators such as >, < and == (not =).
  • elif lets you test more than two cases.
  • A while loop needs a variable that changes, or it runs forever.
  • range(start, stop, step) stops before stop.

Worked examples

Example 1 (book class activity: even or odd).

n = 7
if n % 2 == 0:
    print("Even")
else:
    print("Odd")
print("Even" if n % 2 == 0 else "Odd")

Both lines print "Odd", since 7 % 2 is 1.

Example 2 (book class activity: positive, negative or zero).

n = -4
if n > 0:
    print("Positive")
elif n < 0:
    print("Negative")
else:
    print("Zero")

Output: Negative.

Example 3 (book class activity: even numbers 2 to 10 with range).

for i in range(2, 11, 2):
    print(i)

Prints 2, 4, 6, 8, 10. The stop value is 11 so that 10 is included.

Example 4 (book class activity: first 10 multiples of 3).

for i in range(1, 11):
    print(3 * i)

Prints 3, 6, 9, up to 30.

Example 5 (book class activity: print the even numbers and count the odd numbers from 1 to 20 with a while loop).

n = 1
odd_count = 0
while n <= 20:
    if n % 2 == 0:
        print(n)
    else:
        odd_count += 1
    n += 1
print("Odd numbers:", odd_count)

It prints 2, 4, 6 and so on up to 20, then "Odd numbers: 10". The condition uses <= so that 20 is included. Note how an if-else sits inside the loop: decision making and looping are often combined.

Tracing the book's while loop. Write a small table with columns for number, the condition and the output:

number number < 10? printed
1 True 1
2 True 2
... ... ...
9 True 9
10 False (loop ends)

The loop stops when number becomes 10, so 10 itself is never printed. To print 1 to 10, change the condition to number <= 10.

Common mistakes in the exam

  1. Using = instead of == in a condition.
  2. Forgetting the colon after if, elif, else, while or for.
  3. Splitting a short-hand if-else over two lines without brackets; it gives a SyntaxError.
  4. Not updating the loop variable in a while loop.
  5. Off-by-one with range(): range(2, 10, 2) misses 10.
  6. Mixing tabs and spaces for indentation; keep one style throughout.

Exam technique

  • When asked for output, trace the code line by line in a small table of variable values.
  • Indent loop and if bodies clearly in written answers.
  • Name the structure you used: if-elif-else, while or for.
  • Check your board's notice for the sections in your paper.

Practice questions

  1. What does print("Yes" if 5 > 3 else "No") print? Answer: Yes.
  2. How many lines does the book's while loop print? Answer: Nine (1 to 9).
  3. Write range() for 5, 10, 15, 20. Answer: range(5, 21, 5).
  4. Which statement handles "none of the above" in a chain of conditions? Answer: else.
  5. Name the two kinds of loop in Python. Answer: while and for.

Ready to practise this? Continue in 1st Year Computer Science.

Quick answers

What are the two main types of control structures?

Decision making (if, if-else, short-hand if-else, if-elif-else) and looping (while and for loops).

What is the difference between a while loop and a for loop?

A while loop repeats as long as its condition is true and checks the condition before each pass. A for loop goes through each item of a sequence such as a list, tuple, string or range.

How is a short-hand if-else written?

On one line: value_if_true if condition else value_if_false, for example m = "It's a hot day" if temperature > 30 else "It's not a hot day".

Why does my while loop never stop?

The condition never becomes false, usually because the variable is not updated inside the loop. Add a line such as number += 1.

Which board sets my paper if I study in Gujrat?

Gujrat district is listed under the Board of Intermediate and Secondary Education, Gujranwala. PECTAA writes the books. Your board sets the paper and the dates, so follow its notices.

Learn this properly, step by step, free.

Continue in 1st Year Computer Science

Free right now. No ads. Tell us what to fix.

More notes

All notes →