So....basically, for loop is a while loop with tidier presentation? Thx | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

So....basically, for loop is a while loop with tidier presentation? Thx

16th Oct 2016, 11:13 PM
Jeffrey Wong
Jeffrey Wong - avatar
2 Answers
+ 6
Both can achieve the same thing. A while loop can do what a for loop can, and a for loop can do what a while loop can. It's just how you use them. Generally, you'd use a for loop if you KNOW how many iterations you would like to go. For example, counting up ten numbers, and printing out each iteration. And that leaves the while loop to be used when you can't be sure how many iterations the loop will make. For example, a program that will continue to divide an integer by 2 until the answer becomes 0.
16th Oct 2016, 11:19 PM
Cohen Creber
Cohen Creber - avatar
+ 1
Wow, that 2 examples helped so much, thank you very much !
16th Oct 2016, 11:22 PM
Jeffrey Wong
Jeffrey Wong - avatar