In: Computer Science
For each of the following answers, determine whether the answer
better describes a for loop or a while loop.
It performs one iteration for every value in a sequence.
It uses a loop variable.
It continues the loop until the condition becomes false.
It continues the loop until reaching the end of the sequence.
It can easily cause infinite loops.
The exact number of iterations are usally known when
starting the loop.
both for loops and while loops are using interchangeably.. for loop provide more readability than the while loop as we can initialize, check a condition and increment in a single line.. usually its best practice to use a for loop when we know how many times we want that loop to execute and while loop when we don't know how many iterations it takes to complete..
it performs one iteration for every value in a sequence:
it is for loop..
It uses a loop variable:
it's a for loop.. for loop always need a loop variable to create.. but while loop doesn't need it it can use the existing variable as a loop variable
It continues the loop until the condition becomes false:
While loop.. i know that both for and while run untill the condition becomes false.. but in case of while loop it deals with the actual condition not just some variable like for loop
It continues the loop until reaching the end of the sequence:
it's a for loop.
It can easily cause infinite loops:
While loop.. i know that both for and while loops can cause infinite loops but in while loop condition the chances are high at stake due to the condition variables can be used in program and they can be behave abruptly sometimes
The exact number of iterations are usally known when starting the loop:
its a for loop.. ya it's always best practice that to go with the for loop when we know how many iterations exactly we are going to do..