Control Structures MCQs for NSCT
Control structures are the statements that decide which code runs and how many times: if/else for choices, switch (or Python's match) for multi-way choices, loops for repetition, and break/continue to change a loop's flow. They are the fourth topic in the Programming (C++/Java/Python) area, which carries 10% of the NSCT according to HEC's Areas of Competencies. In 2026 candidates averaged 47.8% in Programming, per the HEC NSCT report and APP.
Most questions here are short traces: read a few lines and say what they print. The rules below are the ones those traces turn on.
If/else
An if statement runs a block only when its condition is true; else gives the alternative, and else if (Python: elif) chains further tests. A chain stops at the first true condition.
marks = 85
if marks >= 50:
grade = "C"
elif marks >= 80:
grade = "A"
print(grade) # C: the first true test wins
Two C++/Java rules come up often. The dangling-else rule pairs an unbraced else with the nearest unmatched if, whatever the indentation says. And if (x = 0) in C++ is an assignment, not a comparison: it sets x to 0 and tests false. Java refuses to compile it because an int is not a boolean.
Exam angle: when a chain gives a surprising result, check the order of the tests before anything else.
Switch
A switch compares one value against several constant case labels. In C++ and Java, a case without break falls through into the next case's statements.
int x = 2, y = 0;
switch (x) {
case 1: y += 1;
case 2: y += 2; // starts here
case 3: y += 3; break; // falls through to here
default: y += 10;
}
// y is 5
| Feature | C++ | Java | Python 3.10+ match |
JavaScript |
|---|---|---|---|---|
| Allowed values | integral and enum types | int-like types, enums, String |
any value or pattern | any value |
| Fall-through | yes, without break |
yes, without break |
no | yes, without break |
| Comparison | equality of integers | equals for String |
pattern matching | strict === |
Exam angle: count which cases run after the matching label, and stop only at break or the end of the switch.
Loops
A loop repeats a block. for and while are entry-controlled (tested before each pass, so they can run zero times); do-while is exit-controlled and always runs at least once. Python has no do-while; the usual stand-in is while True: with a break at the end.
count = 0
for i in range(5):
for j in range(i):
count += 1
print(count) # 0 + 1 + 2 + 3 + 4 = 10
Python's range(start, stop, step) excludes stop, so range(5, 0) is empty; a countdown needs range(5, 0, -1). Python loops can also have an else block, which runs only when the loop finishes without break.
Exam angle: for nested loops, add up the inner loop's passes for each outer value instead of multiplying blindly.
Break and continue
break ends the innermost loop at once; continue skips the rest of the current pass and moves to the next one. Java adds a labelled break outer; that leaves an outer loop; C++ and Python have no labelled break.
for i in range(1, 10):
if i % 2 == 0:
continue
if i > 6:
break
print(i, end=" ") # 1 3 5
Exam angle: a break inside a nested loop only leaves the inner loop, so the outer loop carries on.
Common traps
- A stray semicolon, as in
if (score > 50);orfor (...);, makes the body empty, so the next line runs anyway. - An
unsignedcounter in C++ is never negative, sofor (unsigned i = 5; i >= 0; i--)never ends. - Changing the loop variable inside a Python
forbody does not change the next value fromrange. - A Python
forvariable still exists after the loop, holding its last value; a C++/Javafor (int i ...)variable does not. - JavaScript
varin a loop is one shared variable, so callbacks created in the loop all see its final value;letgives a fresh one per pass.
Worked examples
1. Understanding. In Python, a while loop has an else clause. When does the else block run?
Answer: when the condition becomes false and the loop ends without break. If break ends the loop, the else is skipped.
2. Application. What does this Java code print?
int s = 0;
for (int i = 10; i > 0; i -= 4) s += i;
System.out.println(s);
Answer: 18. i takes 10, 6 and 2; the next value, -2, fails i > 0. 10 + 6 + 2 = 18.
3. Analysis. A student writes this Python code to stop at the first multiple of 7 in a list and print it, but it prints the last multiple instead. Why?
for n in nums:
if n % 7 == 0:
found = n
print(found)
Answer: there is no break, so the loop keeps going and found is overwritten by every later multiple of 7. Adding break after found = n fixes it. (If no multiple exists, found is never set and print raises NameError, so it should also be initialised first.)
Practise Control Structures MCQs
The quiz below has 98 questions that you can filter by tier and difficulty.
Practise: 98 questions
FAQs
Which loop should I use: for, while or do-while?
Use for when you know the number of passes or are walking through a collection. Use while when the loop depends on a condition that changes inside the body, such as reading until a sentinel value. Use do-while, or while True with break in Python, when the body must run at least once, as in a menu that shows before the first choice.
Is switch faster than an if/else chain?
Often, but not always. A C++ or Java compiler can turn a dense switch over integers into a jump table, which picks the case in constant time. A long if/else chain tests conditions one by one. For a few cases the difference is negligible, so choose whichever reads more clearly, and remember switch only tests equality.
Does Python have a switch statement?
Since Python 3.10 there is match, which compares a value against patterns. Unlike C++ and Java, it never falls through: the first matching case runs and the statement ends. Before 3.10, Python code used if/elif chains or a dictionary that maps keys to values or functions.
What is the difference between break and return inside a loop?
break leaves only the innermost loop, and execution continues with the next statement after it in the same function. return leaves the whole function at once, ending every loop in it, and hands a value back to the caller. In nested loops, return is a common way to stop everything when a search succeeds.
Next step
Continue with Functions / Methods, the next topic in HEC's order, or revise Operators & Expressions first. You can also browse all Programming MCQs or try an NSCT mock test.