+ 1
What exactly is the difference between "The For Loop", "The While Loop" and "The Do....While Loop"
2 Respostas
+ 4
● The 'for' loop allows Initialization, Evaluation, and "Action" stages all placed within the loop declaration. It also supports locally scoped iterator & variables (memory efficiency, safety), something other types of loops such as the 'while' and 'do..while' loop doesn't. In the case of 'while' and 'do..while' loop, Initialization needs to be placed outside, only Evaluation is placed in loop declaration, and "Action" is placed inside loop body.
● The 'while' loop evaluates loop condition BEFORE it executes the instructions inside loop body, on the other hand ...
● The 'do..while' loop evaluates loop condition AFTER it executes instructions inside loop body (at least once).
√ The 'do..while' loop is most suitable for situations where you need the loop to run once, at least, before condition is evaluated. e.g. prompt for verifiable input, ask user login info, showing a menu etc.