Control Flow Mastery: if, else, and switch Quiz Quiz

Enhance your understanding of control flow statements including if, else, and switch by answering scenario-based questions designed to deepen your programming logic skills. This quiz targets concepts like statement structure, conditional priority, and correct use cases, supporting your mastery of essential control constructs.

  1. Order of Execution in if-else Chains

    Given the following code, which block will execute if x equals 10? Example: if (x u003E 10) { ... } else if (x == 10) { ... } else { ... }

    1. The else block
    2. All blocks will execute
    3. The if (x u003E 10) block
    4. The else if (x == 10) block

    Explanation: When x equals 10, the if (x u003E 10) condition fails, so the code checks the next condition in the chain else if (x == 10), which is true, and executes that block. The else block only executes if none of the preceding conditions are true, which is not the case here. Only one block executes in such if-else chains, not all blocks. Choosing the if (x u003E 10) block or all blocks demonstrates a misunderstanding of control flow order.

  2. Switch Statement Value Matching

    In a switch statement with integer cases, what happens if none of the case values match the switch expression and there is no default block?

    1. A syntax error is thrown
    2. The first case block executes by default
    3. No statements inside the switch block execute
    4. The last case block always executes

    Explanation: If no case values match and no default block is present, none of the inner case blocks will execute, so program control moves past the switch. The switch does not select the first or last case block as a backup, which makes those options inappropriate. No syntax error occurs simply because a default is missing. The correct behavior is that nothing inside the switch is run unless there is a matching case or a default.

  3. Assignment Inside Conditional Statements

    What is the main risk of using a single equals sign (=) instead of a double equals sign (==) inside an if statement condition, for example: if (a = 5)?

    1. There is no difference; both work identically
    2. The assignment always returns true if the value is non-zero
    3. It produces a syntax error every time
    4. It causes the program to exit immediately

    Explanation: Using = in a condition assigns a value rather than comparing, and in most languages, the result of the assignment is evaluated as the condition. For numbers, any non-zero value is treated as true, so statements like if (a = 5) will always execute the 'true' branch. It does not typically produce a syntax error (unless language rules block it), does not cause a program to exit, and is never identical to using ==, which tests equality. Therefore, the main risk is always treating the assignment result as true if non-zero.

  4. Default Case in a Switch Statement

    Why should a default case be included in a switch statement, for example, switch(day) { case 1: ...; default: ...; }?

    1. To speed up program execution
    2. To always execute regardless of the switch value
    3. To handle values not covered by explicit case labels
    4. To make code look symmetrical

    Explanation: A default block acts as a catch-all to cover situations where the switch expression doesn’t match any of the provided case labels, ensuring a graceful fallback behavior. Including default does not impact performance or program speed. It does not always execute—it only runs when no other case matches. While it can improve readability, its primary purpose is robust handling of unexpected values, not merely code style.

  5. if-else vs. Switch: Appropriate Use

    Which scenario best fits using a switch statement instead of an if-else chain?

    1. Making a decision with only one true or false condition
    2. Comparing complex boolean expressions
    3. Selecting behavior based on a variable with several discrete, known values
    4. Evaluating multiple unrelated variables together

    Explanation: Switch statements are ideal when a single variable is checked against several specific, known values, as this improves code clarity and scalability. If-else is better for single boolean expressions or when comparing different variables or conditions. Switch does not support evaluating multiple unrelated variables or combining complex logical conditions, so using it for those scenarios is inappropriate. The correct use case is for discrete, predictable comparisons on one variable.