+ 3

What is the difference between for loop and while loop?

kindly tell me where can we use for loop but not while loop and visa versa..plzz give me examples....

27th May 2017, 11:48 AM
Anshul Pawaskar
Anshul Pawaskar - avatar
3 Answers
+ 9
The while loop is usually used when you need to repeat something until a given condition is true: inputInvalid = true; while(inputInvalid) { //ask user for input invalidInput = checkValidInput(); } On the other hand, the for loop is usually used when you need to iterate a given number of times: for(var i = 0; i < 100; i++) { ...//do something for a 100 times. }
27th May 2017, 11:55 AM
BALAGURUNATHAN M
BALAGURUNATHAN M - avatar
0
but then in while also we can tell the number of steps
27th May 2017, 4:40 PM
Anshul Pawaskar
Anshul Pawaskar - avatar
0
example int I=0; while (I <100) { I++; }
27th May 2017, 4:41 PM
Anshul Pawaskar
Anshul Pawaskar - avatar