0
So....basically, for loop is a while loop with tidier presentation? Thx
2 ответов
+ 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.
+ 1
Wow, that 2 examples helped so much, thank you very much !