usage of_ for loop and while ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

usage of_ for loop and while ?

can explain it in example ?

16th Sep 2016, 1:54 PM
Nima Karimi
Nima Karimi - avatar
3 Answers
+ 2
Loops are useful to browse arrays, or to do the same task several times in a row. var myArray = [100, 100, 80, 56, 42, 21, 49]; for (var i = 0; i < myArray.length; i++) { //Do something with myArray[i] } Equivalent while loop: i = 0; while (i < myArray.length) { //Do something with myArray[i] i++; } In general, when you know how many times you will loop, prefer using a for loop, and use a while loop otherwise.
16th Sep 2016, 2:01 PM
Zen
Zen - avatar
+ 1
Okay, there is three parts to a for loop. Which I sometimes refer to as setup, test, update. I.e for(int I = 0; I < 10 ; I++){ } The first part initialises the variable, the second part tests that variable against the operand and the third increments the variable "I". Finally I hope I have helped !! and as always I recommend people to browse the reference page for that particular language for further details. Example for JavaScript would be "w3schools"
16th Sep 2016, 10:20 PM
Ousmane Diaw
0
thanks i understand about while loop , in for lope whats doin 3 parameter in statement ?
16th Sep 2016, 9:12 PM
Nima Karimi
Nima Karimi - avatar