Control Structures MCQs for NSCT

By the NSCT Exam Editorial Team · Updated 2026-11-24 · Facts checked against official sources on 2026-11-24

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

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

Interactive quiz loading. The first 10 questions are shown below.

  1. In C++ and Java, which loop always runs its body at least once, even when the condition is false from the start?

    1. do-while
    2. while
    3. for
    4. Range-based for (for-each)
    Show answer

    Answer: A. do-while

    A do-while loop is exit-controlled: the body runs first and the condition is tested afterwards, so the body always executes at least once. while, for and for-each test before the body, so they can run zero times.

  2. In C++ and Java, what happens when a case inside a switch statement has no break?

    1. The compiler reports an error
    2. Execution falls through into the statements of the next case
    3. The switch exits automatically after that case
    4. The next case is skipped and the default runs
    Show answer

    Answer: B. Execution falls through into the statements of the next case

    Without break, control does not leave the switch; it continues into the next case's statements regardless of that case's label. This is called fall-through. It is legal in both languages (Java and C++17 compilers may only warn).

  3. In Python, what does the continue statement do inside a loop?

    1. Exits the loop completely
    2. Restarts the loop from its first iteration
    3. Returns from the enclosing function
    4. Skips the rest of the current iteration and moves to the next one
    Show answer

    Answer: D. Skips the rest of the current iteration and moves to the next one

    continue abandons the remaining statements of the current pass only. The loop then tests its condition (while) or takes the next item (for). break, not continue, ends the loop.

  4. Which Python statement does nothing and is used where a block is syntactically required but no action is needed?

    1. continue
    2. break
    3. skip
    4. pass
    Show answer

    Answer: D. pass

    pass is a null statement, for example in an empty if body or a placeholder loop. continue and break change loop flow, and skip is not a Python keyword at all.

  5. In Python, when does the else block attached to a for loop run?

    1. Only when the loop body never runs
    2. When the loop finishes normally without executing break
    3. Only when break is executed
    4. After every iteration of the loop
    Show answer

    Answer: B. When the loop finishes normally without executing break

    A loop's else clause runs when the loop ends because its items (or condition) ran out. If break ends the loop, the else is skipped. It also runs when the loop body runs zero times, because no break happened.

  6. Which type of expression can a standard C++ switch statement test?

    1. An integral or enumeration type, such as int, char or an enum
    2. Any type, including std::string
    3. Floating-point types such as double
    4. Only int, not char or enum
    Show answer

    Answer: A. An integral or enumeration type, such as int, char or an enum

    The C++ switch condition must be of integral or enumeration type (or a class type with a single conversion to one). std::string and double are not allowed; you need an if/else chain for those. char and enums are integral-like and are allowed.

  7. Since Java 7, a Java switch statement can test a value of which of these types, which a C++ switch cannot?

    1. double
    2. boolean
    3. String
    4. long
    Show answer

    Answer: C. String

    Java added String to switch in Java 7, alongside int, char, short, byte, their wrappers and enums. Java switch still does not accept long, float, double or boolean. C++ switch does not accept std::string.

  8. Which pair of C++ loops are both entry-controlled, meaning the condition is tested before the body runs?

    1. do-while and while
    2. do-while and for
    3. do-while only
    4. for and while
    Show answer

    Answer: D. for and while

    for and while both test the condition before each pass, so their bodies can run zero times. do-while is exit-controlled: it tests after the body.

  9. In C++ and Java, an else written without braces after two nested if statements pairs with which if?

    1. The outermost if
    2. The first if in the function
    3. Neither; the code is a compile error
    4. The nearest preceding if that does not already have an else
    Show answer

    Answer: D. The nearest preceding if that does not already have an else

    This is the dangling-else rule: an else binds to the closest unmatched if. Indentation does not matter to the compiler, so use braces to make the intended pairing explicit.

  10. How does Python's match statement (Python 3.10+) differ from a C++ or Java switch without break?

    1. Only the first matching case runs; there is no fall-through
    2. It falls through to the next case unless you write break
    3. It runs every case whose pattern matches
    4. It requires a default case or raises an error
    Show answer

    Answer: A. Only the first matching case runs; there is no fall-through

    In match, the first case whose pattern matches runs and the statement ends. There is no fall-through and no break is needed. A wildcard case _ is optional; if nothing matches, nothing happens.

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.