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.
Which C# statement is used to execute code only if a specific condition is true?
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.
What is the correct order of sections in a C# for loop declaration?
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.
In C#, which control flow statement is commonly used instead of multiple if-else blocks for matching a variable to several possible values?
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.
Which type of C# loop guarantees that its body will execute at least once, even if the condition is false initially?
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.
What is the effect of the 'break' statement inside a loop in C#?
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.
Which C# loop is most suitable for iterating over every item in an array or list?
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.
When using if-else conditionals in C#, what does the 'else' block do?
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.
In a C# loop, what does the 'continue' statement do?
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.'
In a switch statement, what is the purpose of the 'default' case?
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.
Given 'for (int i = 1; i u003C= 3; i++) { Console.WriteLine(i); }', what numbers are printed?
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.