What exactly is the difference between "The For Loop", "The While Loop" and "The Do....While Loop" | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 1

What exactly is the difference between "The For Loop", "The While Loop" and "The Do....While Loop"

12th May 2020, 2:43 PM
ℤ𝕚𝕔𝕔𝕠
ℤ𝕚𝕔𝕔𝕠 - avatar
1 ответ
+ 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.
12th May 2020, 2:56 PM
Satyam Patel
Satyam Patel - avatar