Courses / Computer Science XI (HSSC-I) – FBISE Complete SLO-Based Course
3.3 Control Structures: Sequence, Selection and Repetition
Unit 3 – Programming Fundamentals · 90 min read · 6 MCQs to check yourself
Corey Schafer – conditionals and Booleans: if, elif, else
1. Sequence
Statements run one after another, top to bottom.
celsius = 37
fahrenheit = celsius * 9 / 5 + 32
print(fahrenheit)
Output:
98.6
2. Selection: if, if–else, if–elif–else
Python uses a colon : and indentation (4 spaces) to mark blocks. Comparison uses ==, assignment uses =.
ch = input("Enter an alphabet: ") # input: E
if ch.lower() in "aeiou":
print(ch, "is a vowel")
else:
print(ch, "is a consonant")
Output:
E is a vowel
marks = 73
if marks >= 80:
grade = "A"
elif marks >= 70:
grade = "B"
elif marks >= 60:
grade = "C"
else:
grade = "F"
print("Grade:", grade)
Output:
Grade: B
Nested if and logical operators:
year = 2024
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
print(year, "is a leap year")
else:
print(year, "is not a leap year")
Output:
2024 is a leap year
3. Repetition (loops)
| Loop | Use when | Syntax |
|---|---|---|
for |
The number of repetitions is known, or you loop over a list/string | for i in range(1, 11): |
while |
Repeat while a condition is true (unknown count) | while n > 0: |
range(start, stop, step) produces numbers from start up to but not including stop: range(1, 10, 2) → 1, 3, 5, 7, 9.
For loops and while loops in Python (with exercises)
n = 7
for i in range(1, 6):
print(n, "x", i, "=", n * i)
Output:
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
num = 4728
digits = 0
total = 0
while num > 0:
total += num % 10
num //= 10
digits += 1
print("Digits:", digits, "Sum of digits:", total)
Output:
Digits: 4 Sum of digits: 21
break and continue: break leaves the loop immediately; continue skips to the next repetition.
subjects = ["Computer", "English", "Physics", "Urdu"]
for x in subjects:
if x == "Physics":
break
print(x)
Output:
Computer
English
(This is the model-paper "complete the code that exits when x is Physics": the blanks are subjects, "Physics" and break.)
for i in range(1, 11):
if i % 3 == 0:
continue
print(i, end=" ")
Output:
1 2 4 5 7 8 10
Nested loops – a pattern:
for row in range(1, 5):
print("*" * row)
Output:
*
**
***
****
PBA tasks
- Input a number and print whether it is even or odd, positive, negative or zero.
- Print the table of any number from 1 to 10. 3. Find the factorial of n with a
forloop. - Keep asking for a password until the user types the correct one (while loop), allowing at most 3 tries.
- Print all prime numbers between 1 and 50.
Short and long questions
1. Write a Python program that takes an alphabet as input and prints whether it is a vowel or a consonant.
Model answer
ch = input("Enter an alphabet: ")
if ch.lower() in "aeiou":
print(ch, "is a vowel")
else:
print(ch, "is a consonant")
2. Complete the following Python code that exits when x is "Physics": subjects = ["Computer", "English", "Physics"]; for x in : if x == : ; print(x)
Model answer
subjects = ["Computer", "English", "Physics"]
for x in subjects:
if x == "Physics":
break
print(x)
Output: Computer, English.
3. Differentiate between for and while loops with an example of each.
Model answer
A for loop repeats a known number of times or over a sequence: for i in range(1, 6): print(i) prints 1–5.
A while loop repeats as long as a condition is True (count may be unknown): n = 5 · while n > 0: print(n); n -= 1 prints 5 to 1.
Use for for counting/iterating, while for condition-controlled repetition such as validating input.
4. Differentiate between break and continue statements.
Model answer
break immediately terminates the loop and control goes to the statement after the loop. continue skips the remaining statements of the current iteration and moves to the next iteration. Example: in for i in range(1, 6), if i == 3: break prints 1 2; if i == 3: continue prints 1 2 4 5.
5. List any six rules for naming variables in Python.
Model answer
(1) Must start with a letter or underscore. (2) Cannot start with a digit. (3) Only letters, digits and underscore allowed. (4) No spaces or special symbols (@, -, #). (5) Case-sensitive (Age ≠ age). (6) Cannot be a keyword such as if, for, class, True.
6. Write a Python program that inputs the marks of a student out of 100 and prints the grade (A+ ≥ 80, A ≥ 70, B ≥ 60, C ≥ 50, F otherwise). The program should reject invalid marks and keep asking until valid marks are entered. Explain the control structures used.
Model answer
marks = int(input("Enter marks (0-100): "))
while marks < 0 or marks > 100:
print("Invalid marks, try again.")
marks = int(input("Enter marks (0-100): "))
if marks >= 80:
grade = "A+"
elif marks >= 70:
grade = "A"
elif marks >= 60:
grade = "B"
elif marks >= 50:
grade = "C"
else:
grade = "F"
print("Grade:", grade)
Control structures: sequence – statements run in order (input, check, print); repetition – the while loop repeats input validation until marks are in range; selection – the if–elif–else chain chooses exactly one grade.
This lesson ends with a quiz of 6 MCQs. Open the lesson on PrepOasis to take it and save your progress.
Previous: 3.2 Python Basics: Input/Output, Variables, Data Types and Operators · Next: 3.4 Turtle Graphics: Drawing Shapes with Python
All lessons in Computer Science XI (HSSC-I) – FBISE Complete SLO-Based Course