Which loop continues until a boolean flag becomes true?

Enhance your programming skills with the RECF Programming Test. Features flashcards and multiple choice questions with hints and explanations. Prepare for success!

Multiple Choice

Which loop continues until a boolean flag becomes true?

Explanation:
To keep looping until a condition becomes true, you want to test the negation of that condition at the start of each iteration. The form that does this is a while loop with the condition negated, so the loop runs only while the flag is false. Inside the loop you can change the flag to true when the desired condition is met, and the next check will stop the loop. This matches the idea of continuing until the flag becomes true, because the loop keeps going as long as the flag isn’t true, and stops as soon as it becomes true. A do-while variant would run the block at least once before checking, which can cause an unnecessary or incorrect extra execution if the flag starts true. A while loop that tests the opposite condition would loop while the flag is true, doing the opposite of what’s intended. An if block isn’t a loop at all.

To keep looping until a condition becomes true, you want to test the negation of that condition at the start of each iteration. The form that does this is a while loop with the condition negated, so the loop runs only while the flag is false. Inside the loop you can change the flag to true when the desired condition is met, and the next check will stop the loop.

This matches the idea of continuing until the flag becomes true, because the loop keeps going as long as the flag isn’t true, and stops as soon as it becomes true. A do-while variant would run the block at least once before checking, which can cause an unnecessary or incorrect extra execution if the flag starts true. A while loop that tests the opposite condition would loop while the flag is true, doing the opposite of what’s intended. An if block isn’t a loop at all.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy