# FAQ: Loops - The For Loop

This community-built FAQ covers the “The For Loop” exercise from the lesson “Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

## FAQs on the exercise The For Loop

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

## Join the Discussion. Help a fellow learner on their journey.

Agree with a comment or answer? Like () to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I created a “for” loop as below:

for (let num = 5; num < 11; num+1) {
console.log(num)
};

The expected result was a short list of numbers: 5, 6, 7, 8, 9, 10
Yet, the returned result was a lot of “5” digits.

To me, the iteration statement “num+1” is synonymous with “num++” and “num+=1”; the latter two worked fine.
I don’t know why I got different results using the three statements.

Can anyone do me a favor and answer this question?

they are not the same, `num + 1` never updates the num variable. We can see this:

``````let i = 5;
console.log(i + 1) // 6
console.log(i) // 5, because variable didn't update.
i = i + 1
console.log(i) // 6
``````

`i = i + 1` does update the variable, `+=` is just a shorthand, and `++` is even shorter shorthand to increase the variable by one

`i = i + 1` looks a bit weird for new programmers, because you assign the current value + 1 in the same variable.

5 Likes

Why doesn’t this work?

``````for (let counter = 5; counter <11 || counter >4; counter++) {
console.log(counter);
}
``````
2 Likes

you can’t use the `or` operator in the for loop. Besides, why would you? `counter` starts out as 5, and only increases, so counter > 4 is redundant.

1 Like

Why is this not an accepted answer?

``````// Write your code below
for (i = 5; i <= 10; i++){
console.log(i)
}

``````
2 Likes

that seems correct, and when i run your code in the exercise it get green light

Nope not for me… restart the exercise and try it again, it will pass after using the original solution proved by codeacademy, and it will then accept this version above.

I just did it again, it fails.

2 Likes

It may be expecting `let i = 5` in the loop parameters.

3 Likes

Bingo! That was it! Thank you mtf!

1 Like

5 posts were split to a new topic: ‘function’ instead of ‘for’

Just a quick and possibly a silly question but what is the difference between using an IF statement vs the For Loop, they both sound a little similar.

1 Like

`if` checks a condition, while for loop allows us to loop over array, strings and more:

``````let x = 3;
if (x < 5){
console.log("3 is smaller then 5");
}

const myArray = [1, 2, 3, 4, 5, 6, 7, 8]
for (let i = 0; i < myArray.length; i++)
{
console.log(i);
}
``````

we can even combine loop and if, for example to check all the values in the list:

``````const myArray = [1, 2, 3, 4, 5, 6, 7, 8]
for (let i = 0; i < myArray.length; i++){
if ( myArray[i] < 5 ){
console.log(`\${myArray[i]} is smaller then 5`);
} else {
console.log(`\${myArray[i]} is NOT smaller then 5`);
}
}
``````
3 Likes

But is this “proper” syntax? Why are they instructing us to use ‘let’ when the code runs fine without it?

When starting with programming, its really difficult to grasp when multiple approaches seem to work, which one is “best” and why.

i would absolute recommend using `let`, which has block scope:

``````for (let i = 0; i < 5; i++){
console.log(i);
}
// i variable only exist within the for loop, which we can see:
console.log(typeof i);
``````

if we wouldn’t use `let` (and not even `var`), the variable would exist outside the needed scope:

``````for (i = 0; i < 5; i++){
console.log(i);
}

console.log(i);
``````

this pollutes the global namespace, more change of conflicting variable names and so forth.

6 Likes

This exercise is not functioning properly.
I entered:

for (let counter = 5; counter < 11; counter++) {
console.log(counter);
}
Asked for the solution and was given:

for (let counter = 5; counter < 11; counter++) {
console.log(counter);
}

On LOOPS
The For Loop lesson it begins with this:

a stopping condition is the condition that the iterator variable is evaluated against— if the condition evaluates to `true` the code block will run, and if it evaluates to `false` the code will stop.

So, the condition that we use needs to always be true?

Correct. That is what controls the iterations. Once no longer true, iteration ceases.

At first I tried this approach to solve the exercise.
Why can’t the stopping condition equal to 10? It gives me an eternal loop.
But since it was a ++, it should go to 11 afterwards and stop, no?

for(let counter = 5; counter = 10; counter++)
{
console.log(counter);
}

here:

``````counter = 10
``````

you don’t do a comparison, you do an assignment. After the assignment, JS will check if 10 is a truthy value (which it is), so now 10 is evaluated as truthy every iteration of the loop causing an infinity loop

1 Like