Welcome to the C# Looping with do and while Statements Quiz! This quiz is designed to test your understanding of the while and do…while loops in C#. You will explore key concepts such as loop structure, conditional expressions, the differences between while and do…while loops, and the use of break and continue statements. These loops are essential tools for repeating code until specific conditions are met, and this quiz will help reinforce your knowledge on how and when to use them.
1.
What is the primary difference between a while loop and a do…while loop in C#?
2.
What happens if the condition in a while loop is initially false?
3.
What is the purpose of a do…while loop?
4.
Which of the following correctly defines the syntax of a C# while loop?
5.
When does a do…while loop terminate?
6.
In the following code, how many times will the loop execute?
int i = 10;
do {
i--;
} while (i > 0);
7.
What is the purpose of the break statement in a while or do…while loop?
8.
What happens when the continue statement is used in a while loop?
9.
In the following code, what value of i will trigger the break statement and exit the loop?
int i = 0;
int j = 5;
while (i < 100) {
i++;
if (i == j)
break;
}
10.
How many times will the following loop print the value of i?
int i = 1;
while (i < 20) {
i++;
if ((i % 2) != 0)
continue;
Console.WriteLine("i = " + i);
}
11.
Which loop should you use if you know that the code inside the loop must be executed at least once?
12.
When does a while loop skip its body entirely?