COMPUTER SCIENCE AND ENGINEERING
ALGORITHMS
Question
[CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
|
What is the advantage of using a while loop versus a for loop?
|
You can repeat the code continuously
|
|
There is a pre-defined number of repetitions
|
|
Either A or B
|
|
None of the above
|
Explanation:
Detailed explanation-1: -Use a for loop when you know the loop should execute n times. Use a while loop for reading a file into a variable. Use a while loop when asking for user input. Use a while loop when the increment value is nonstandard.
Detailed explanation-2: -When would it be more beneficial to use a while loop instead of a for loop? When you need your loop to be controlled by user input. Adding a tracking variable to count the number of times a loop executes can be an effective way to measure efficiency.
There is 1 question to complete.