11. Loops and Arrays I. 0 IS ALREADY < 4?

#1

My code works, but I don't understand why. Can someone help me understand this statement from the lesson: "We see i starts off at value 0. The for loop runs until i < 4." I'm confused because 0 is ALREADY less than 4, so the stop condition seems like it's already met when the first loop runs, and therefore shouldn't run at all. It seems like the stop condition should be i > cities.length. What am I failing to understand? Thanks in advance.

#2

This is pretty weird wording, and it's an odd concept.
The only way I know how to explain it is that the first number is the starting point, the second number is the stopping point, and the third number is the increment.
You obviously understand the starting point, but the ending point is sort of backwards. What the instructions were saying, was that WHILE I is less than 4, it will run. I don't know that this helps answer your question, but hopefully it makes more sense.
an example is this:

``````for(var counter = 0; counter < 50; counter += 5) {
console.log(counter);
}``````

This is going to print the numbers 1 through fifty, but counting by 5s. It stopped at 50, because the condition is that counter has to be less than 50.
Hope I could help :)))))

#3

Thanks Kylea! Definitely answers my question. Thinking of it, as you say, as being while the stop condition is true, makes perfect sense. I thought it might be an error in the Code Academy explanation, and it seems like it is just poor wording. You're a gem and are gonna go far!

#4

I'm so glad I could help at least a little, :))
It's always good to ask questions about things you don't understand, because when I started loops, I had ridiculous trouble. I think it's poor wording as well, at least for teaching people.

Thank you x 1000

#5

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