While loop vs for loop

Mohamed Nakhlawy 4 years ago 0 Comments

For Loops allow you to run through the loop when you know how many times you 'd like it to run through the problem such as 

for (var i; i < 10; i++); 

this will continually increase i until that condition returns false, any number can replace the 10 even a variable. but it will quit once the condition is no longer being met. This is best used again for loops that you know how when they should stop.

While Loops allow you a little more flexibility in what you put in it, and when it will stop such as 

while ( i < 10)

you can also substitute in a boolean(true/false) for 10 as well as many other types of variables.

The key difference between the two is organization between them, if you were going to increase to 10 it'd be a lot cleaner and more readable to use a for statement, but on the other hand if you were to use an existing variable in your program in your loop parameters it'd be cleaner to just wright a while loop. In the For loop you MUST create a new variable, that's not true for the While loop.

I hope this helps



0 comments

  • No, comments yet

Comments Closed