How to decide which loop to use?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to decide which loop to use??

14th Mar 2021, 3:47 PM
Bãrt Simpson
Bãrt Simpson - avatar
2 Answers
+ 8
If you have a specific range of numbers, use the for loop. If you want to run the loop as long as something is true, use the while loop and if you want the loop to run at least once, use the do - while loop. At the end of the day, you can use all of the loops for the same purpose, even though some are better suited for specific jobs than others
14th Mar 2021, 3:51 PM
Soumik
Soumik - avatar
+ 1
if you want to change elements of array or want to use array index, don't use enhanced for() loop.
14th Mar 2021, 10:45 PM
zemiak