C# Control Flow Essentials: Loops, Conditions, Switch u0026 More Quiz

Test your knowledge of C# control flow, including conditional statements, loops, switch statements, break and continue. This easy quiz helps you understand the basics of implementing control flow logic in C# for clean and effective code.

  1. Identifying Simple Conditionals

    Which C# statement is used to execute code only if a specific condition is true?

    1. break
    2. loop
    3. if
    4. switch

    Explanation: The 'if' statement executes code when a condition evaluates to true, making it the primary tool for decision-making. 'Loop' is not a C# statement and has no function in this context. 'Switch' is used for matching a value against multiple cases, not single-condition checking. 'Break' is used to exit loops or switch cases, not to check conditions.

  2. For Loop Syntax

    What is the correct order of sections in a C# for loop declaration?

    1. initialization; condition; iteration
    2. iteration; initialization; condition
    3. condition; initialization; iteration
    4. iteration; condition; initialization

    Explanation: A C# for loop is structured as for (initialization; condition; iteration), ensuring variables are set, checked, and updated in that order. The second and fourth options misplace the initialization or iteration, while the third starts with iteration, which is incorrect.

  3. Purpose of the 'switch' Statement

    In C#, which control flow statement is commonly used instead of multiple if-else blocks for matching a variable to several possible values?

    1. switch
    2. for
    3. continue
    4. while

    Explanation: The 'switch' statement helps compare a variable against various possible values in a concise way. 'For' and 'while' are used for looping, not value matching. 'Continue' affects loop iteration but is unrelated to value selection.

  4. While vs. Do-While Loops

    Which type of C# loop guarantees that its body will execute at least once, even if the condition is false initially?

    1. while
    2. do-while
    3. for
    4. foreach

    Explanation: A 'do-while' loop executes its block once before checking the condition, guaranteeing at least one execution. 'While' checks the condition first and may not run at all. 'For' and 'foreach' are not designed to guarantee a single execution if their conditions are not met.

  5. Using 'break' in Loops

    What is the effect of the 'break' statement inside a loop in C#?

    1. It ends the program
    2. It immediately exits the loop
    3. It skips the current iteration
    4. It starts the loop from the beginning

    Explanation: 'Break' terminates the current loop and resumes execution after it. 'Continue' is the statement that skips the current iteration. Restarting the loop is not the function of any standard control flow keyword, and 'break' does not end the whole program.

  6. Choosing the Correct Loop for Collections

    Which C# loop is most suitable for iterating over every item in an array or list?

    1. foreach
    2. break
    3. do-while
    4. switch

    Explanation: The 'foreach' loop is specifically designed for iterating over collections such as arrays or lists. 'Do-while' is meant for repeated execution based on a condition, not for item-by-item collection processing. 'Switch' is used for matching values, and 'break' only alters loop flow.

  7. Identifying an Else Statement

    When using if-else conditionals in C#, what does the 'else' block do?

    1. Repeats the previous statement
    2. Checks a new condition
    3. Breaks out of the loop
    4. Executes if all prior conditions are false

    Explanation: The 'else' block runs when none of the previous conditions in the 'if' or 'else if' statements are true. It does not check a new condition, as 'else if' does. 'Break' is unrelated, and it does not repeat any statements.

  8. Function of the 'continue' Statement

    In a C# loop, what does the 'continue' statement do?

    1. Ends the program immediately
    2. Restarts the loop from the beginning
    3. Exits the loop entirely
    4. Skips the rest of the current iteration

    Explanation: 'Continue' skips all remaining code in the loop's current cycle and moves to the next iteration. To exit a loop entirely, the 'break' statement is used. Restarting the loop from the beginning or ending the program are not behaviors provided by 'continue.'

  9. Default Case in Switch Statements

    In a switch statement, what is the purpose of the 'default' case?

    1. Repeats the last case
    2. Breaks out of the loop
    3. Executes if no other case matches
    4. Executes before all cases

    Explanation: The 'default' case is used to handle any values not specifically matched by other 'case' labels in a switch statement. It does not execute before all cases; instead, it only runs if no matches are found. There is no functionality for repeating the last case, and 'break' is a separate statement.

  10. Loop Execution Example

    Given 'for (int i = 1; i u003C= 3; i++) { Console.WriteLine(i); }', what numbers are printed?

    1. 2, 3, 4
    2. 1, 2, 3
    3. 1, 2, 3, 4
    4. 0, 1, 2

    Explanation: The loop starts with i = 1 and continues while i u003C= 3, so it prints 1, 2, and 3. The second option starts incorrectly at 0. The third option uses values outside the specified range, and the fourth option goes beyond the upper limit (i = 4), which is never reached.