What's the point of having three types of loops cause the all have the same function and don't seem so different from each other | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What's the point of having three types of loops cause the all have the same function and don't seem so different from each other

loops

12th May 2017, 5:35 PM
Shammah
Shammah - avatar
2 Answers
+ 13
All have their own specialties. do while loop runs at least once contrary to the others. for/while loop an be interchangeably used but there each of them are better in several occasions. For example, a for loop is better to iterate an array but a while loop is better for digit extraction from an int.
12th May 2017, 5:39 PM
Pixie
Pixie - avatar
+ 5
They are all different from eachother. There's also recursion and the enhanced for loop. Anywho, here's generally when to use them: While: Not sure how many times I'm going to loop. for: I always know how many times I'm going to loop do while: also while, but I also want the code to always run once no matter what. The for loop in most cases always has some kind of itteration that the condition depends on. While the while loop in most cases depends on the condition alone, which would be modified within the loop itself.
12th May 2017, 5:43 PM
Rrestoring faith
Rrestoring faith - avatar