Quiz: If-Statements and Loops in C#

Quiz: If-Statements and Loops in C#



1 / 10

1. What is the purpose of an if-statement?



2 / 10

2. What will the following code print?

int x = 5;
if (x > 3)
{
Console.WriteLine("x is greater than 3");
}
else
{
Console.WriteLine("x is 3 or less");
}



3 / 10

3. What is the difference between an if and else if statement?



4 / 10

5. What is the output of this while loop?

int i = 1;
while (i <= 3)
{
Console.WriteLine("Loop iteration: " + i);
i++;
}



5 / 10

4. How many times will the following for loop run?

for (int i = 0; i < 4; i++)
{
Console.WriteLine(i);
}



6 / 10

6. Which type of loop guarantees that the code inside the loop will run at least once?



7 / 10

7. What does the following code do?

int i = 0;
do
{
Console.WriteLine(i);
i++;
} while (i < 3);



8 / 10

8. How would you make the following loop run exactly 10 times?

for (int i = 0; ___; i++)
{
Console.WriteLine(i);
}



9 / 10

9. What happens if you forget to increase the counter inside a while loop (e.g., i++)?



10 / 10

10. What is the purpose of the break statement in a loop?



Your score is

The average score is 0%

0%