FC0-U61 · Question #569
Which of the following should a developer use to ensure a block of code executes a defined number of times?
The correct answer is A. for loop. A developer should use a 'for loop' to ensure a block of code executes a predetermined, defined number of times.
Question
Which of the following should a developer use to ensure a block of code executes a defined number of times?
Options
- Afor loop
- Bwhile loop
- Cif ... else statement
- Dswitch statement
How the community answered
(41 responses)- A88% (36)
- B7% (3)
- C2% (1)
- D2% (1)
Why each option
A developer should use a 'for loop' to ensure a block of code executes a predetermined, defined number of times.
A 'for loop' is specifically designed for iterating a block of code a fixed number of times, typically by initializing a counter, defining a condition for the loop to continue, and specifying an increment or decrement step, making it ideal when the number of repetitions is known in advance.
A 'while loop' executes a block of code repeatedly as long as a specified condition remains true, which is generally used when the number of iterations is not known beforehand.
An 'if...else statement' is a conditional construct that executes one block of code if a condition is true and another if it is false; it is not used for repetitive execution.
A 'switch statement' is used for multi-way branching, executing different code blocks based on the value of a single expression, rather than for iterating code a defined number of times.
Concept tested: Programming loop types - for loop
Source: https://learn.microsoft.com/dotnet/csharp/language-reference/statements/iteration-statements#the-for-statement
Topics
Community Discussion
No community discussion yet for this question.