What's the tradeoff in using "While" loop instead of "For" loop? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 2

What's the tradeoff in using "While" loop instead of "For" loop?

What is the tradeoff in using the "while" loop instead of the "for" loop?

22nd Jul 2018, 3:21 PM
Matt Elliott
Matt Elliott - avatar
4 ответов
+ 6
for loop used widely when you know the initial value, final value and number of times that you do the statements, on the other hand.. (while) is conditional loop, which means it executed if the condition is true, otherwise it stops (number of times that you do the statements is depend on the condition)
22nd Jul 2018, 10:15 PM
ADIL GAAFER
ADIL GAAFER - avatar
+ 5
While loop is better for unknown count of iterations. For example in case you executing something in function or condition, which returns boolean value. while (stack.Count > 0) { stack.Pop(); }
22nd Jul 2018, 3:46 PM
Martin Bulíř
+ 1
You use one which best fit your needs.
22nd Jul 2018, 3:34 PM
Lstiti
+ 1
you can use every loop for everything, they are all interchangeable. use the one that you like most or that fits your needs the most
22nd Jul 2018, 3:59 PM
Jeremy
Jeremy - avatar