FAQ: Loops - Putting it All Together

One last thing I should mention about the increment/decrement operators. They have two operation modes…

prefix   =>  ++i

postfix  =>  i++

I’ll leave you to play around and see their difference. Remember, the operation takes place immediately.

i = 0
while (i < 10) {
    console.log(++i)    // increment before logging
}
1
2
3
4
5
6
7
8
9
10
i = 0
while (i < 10) {
    console.log(i++)   // increment after logging
}
0
1
2
3
4
5
6
7
8
9
1 Like

Oh wow, this is very interesting !! Again, I am extremely grateful !!! Writing this down in my notes, I will practice & explore to better understand. Very helpful, just opened up my mind further to these awesome operators.

1 Like

can someone explain to me what i++ means?

1 Like

https://duckduckgo.com/?q=mdn+operators

1 Like

Hey, I’m confused. Why the ‘i++’ was placed at the top of the ‘For loop’ and at the bottom of the ‘while loop’ ?? Plz help!!!

1 Like

thanks this clarified!

I am confused on this question If anyone is out their to help.followed
for(i=0;i<10;i++){
placeTile(‘mint’)
placeTile(‘orange’)
placeTile(‘mint’)
nothing happens when run any help

hi, i am confused as to what the first line of this code means. what does “let i = 0” mean exactly? didnt see any mention of “let” until now.

let i = 0;
while (i < 10) {
placeTile(‘mint’)
placeTile(‘orange’)
placeTile(‘mint’)
i++
}

It is a declaration combined with an assignment.

let i

is the declatation.

i = 0

is the assignment. i is now bound to the the numeric value, 0 (zero).

This is done to initialize the variable for the purpose of accumulating as a counter. Just as we would count, 1, 2, 3, …, i will be bound to each of these counting values in turn, starting with 0.

It is prudent to note that zero is an appropriate value to begin with, since it means, none. “Yes, we have no bananas.” Once things get started, that number changes as would normally progress, just like counting. We never start with zero, but one. 1, 2, 3, …

Because we use increments zero is the best place to start for counting sequences. Having gone through a code segment, we can increment by 1 the number of times we did that. Hence the need for counting the number of times we do it.

I am having a hard time understanding why someone would use a count-controlled loop versus a condition-controlled loop in this instance.
I understand that count means “do this 10 times” and condition means “do this while you have done it less than 10 times”, but since they have the same result, what is the difference between them in this case? When would a count-controlled loop be more useful than a condition-controlled loop?

Thank you!

When we know the count. It’s when we only know the termination condition that we use while. While is not a counting loop, for is.

Hello, in the while loop, why do I have to use let i = 0 if simply i = 0 runs exactly the same?

let gives block scope to the variables we declare inside any block. {...block body...}. This keeps the variables from leaking into outer scope.

If the while loop is inside a function and the variable is not declared with let (or var) it will automatically get hoisted to the top of global scope. This is something we don’t want as it can collide with variables in that scope. This is called, polluting global scope.

2 Likes

Hey so I started using MATLAB a few months ago and I saw that for loops went like : for ii=1: 1: 10. But in this case, I see that the for loops goes like : for (i = 0; i < 10; i++) and the increment is after the specified limit (10). Is there any reason for this or does the order really matter

As the language construct goes, yes it does matter. JS will try its hardest to make something work. It’s a do-or-die kind of language. It’s really up to us to give it guidelines and directives. But step out of the syntax and it won’t work. Simple as that. Just like any language.

Do you know why it is i++? Why is it not i+ for a single increment of iteration?

The, +, operator has several purposes but increment is not one of them. The special unary, ++ and its counterpart, -- are increment and decrement, respectively. We’ll probably find that this nomenclature is inherited from C, the grand daddy of many of today’s programming languages.

The magic of these operators is that they are in situ, or in place operators and need only one operand. They can be used as prefix or postfix, meaning they can be applied before the variable is used in another operation, or after.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators

Thank you very much for explanation. It is absolutely on point.

The changes can be made on the code with i = 1 or i = 2.

However, when I made the changes to i++ and made it ++i the results were the same.

// Copy code here and run it!

let i = 0;

while (i < 10) {

placeTile(‘mint’)

placeTile(‘orange’)

placeTile(‘purple’)

++i

}

This Code is exactly the same when used i++

Why is that?

Just for s&g try,

i = 0
while (++i < 10) {
    console.log(i)
}

and then,

i = 0
while (i++ < 10) {
    console.log(i)
}