+ 2

What is the diferent between "for" loop and "while" loop??

12th Dec 2017, 9:24 PM
Diego Lucas
Diego Lucas - avatar
2 Answers
+ 3
Not specifically for JavaScript but loops in general. A for loop is used when you know for certain how many iterations (how many times) the code in the body of loop is going to run for. Whereas a while loop is used when you don't know how many times it will run and will only stop if the stopping condition is met (when the header becomes false)
12th Dec 2017, 9:28 PM
Tarantino
Tarantino - avatar
+ 2
FOR LOOP ------> • It is not guaranteed that how many times loop body gets executed. WHILE LOOP ------> • It is guaranteed that loop body will get executed at least once. FOR LOOP -----> • It is Entry Control loop. { in which the control checks the condition before entering into the loop } WHILE LOOP ------> • It is Exit Control loop. { in which the control enters into the loop without any check and after 1 execution, the control checks the condition at the time of exit }
13th Dec 2017, 4:47 PM
Mayank Rampuriya
Mayank Rampuriya - avatar