+ 1
What exactly is the difference between "The For Loop", "The While Loop" and "The Do....While Loop"
2 Answers
+ 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.