+ 1

For vs while?

So are for loops basically the same thing as while loops? My understanding is For(int, when it’s this, do this) While(when it’s this){ Do this } That’s almost exactly the same thing? So what’s the point of while loops when for loops exist and vice versa?

16th Oct 2019, 5:06 PM
Matin Qurbanzadeh
Matin Qurbanzadeh - avatar
2 Answers
+ 1
For loops are incredibly useful to eventually track an index when you want to loop through an entire array: for (int i=0; i < array.size (); i++) { array [i] = 10; // or any operation you need } While loops on the other hand do not track any index, and will just repeat over until the condition of your loop returns false.
16th Oct 2019, 5:14 PM
Sora
Sora - avatar
0
For loops are really useful if you want to iterate through something and you know exactly how often it'll have to run. For any other conditions or permanent loops you can use while. I hope that this was helpful.
16th Oct 2019, 8:21 PM
Jannik MĂŒller
Jannik MĂŒller - avatar