A little trouble with for loop


#1

why this working

for (var i=1;i<10;i+=1){
console.log("I love coding!");
}

while

for (var i=1;i=10;i+=1){
console.log("I love coding!");
}

this is wrong?


#2

Hi, here

for (var i=1;i=10;i+=1)

this part

i=10

you are storing 10 to the variable i and in the first code

i <10

you ptu the condition of the loop as long as i is less than 10


#3

You mean if I set an exact value to the second i then the loop wouldn't start, which means i would not add 1 automatically until it gets to 10?
So only comparison is accepted in for loop?


#4

So only comparison is accepted in for loop?
There are three parts in the for loop

for(statement1, statement2, statement3) {
    #do something
}

the statement1 is where you declare the variable, the statement2 is where you put the condition then the statement3 is where you increase the variable.


#5

Ok, I see. Thank you!


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.