FAQ: Loops - Putting it All Together

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)
}
2 Likes

Now unwind the last one…

do {
    console.log(--i)
} while (i > 0)

The two loops combined are akin to a series of pushes followed by a series of pops which is akin to a stack as it winds up, and winds down.

2 Likes

I think “i” stands for “Iteration”, not “Index”.

Thanks for sharing this. This is very helpful for me to read the cord.

Hello @chipjumper27457 and welcome to the Codecademy Forums!

In this case, i refers to a temporary variable that stores a different value in every iteration of the loop. The topic below provides a more in-depth explanation:

Question about placing i++. I noticed that in the for loop, the i++ is placed within the parenthesis ()

for (i = 0; i < 10; i++) {
placeTile(‘mint’)
placeTile(‘orange’)
placeTile(‘mint’)

}

and in the while loop the i++ is placed within the curly brackets at the end {}

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

What’s the logic that determines the placement for the incremental increase? Also, apologies if this has been asked already.

I found this explanation, right at the bottom of the table

https://techdifferences.com/differenece-between-for-and-while-loop.html#:~:text=In%20while%20loop%20if%20initialization,each%20time%20the%20loop%20iterate.&text=In%20’for’%20loop%20iteration%20statement,written%20anywhere%20in%20the%20loop.

@mtf

the symbol you used ‘’<= ‘’ why did you use that, first i thought it as an arrow but after told in your explanation as relation symbol

can we use this’’=>’’ in its place too?

Those are symbols, only, but they indicate from right to left the transition from expression to expression (syntax), each time reduced to simpler form.

what a crap lesson this one was. Why would they throw terminology like i++ at us without explaining in depth wtf that is supposed to mean. Thanks to MTF for explaining it even though in his explanation he used examples that no noob like myself would understand.

3 Likes

I’m having trouble figuring out the third command in the insurrections for this section. " 3. Edit the condition in the while loop so that the loop is executed twice (only 6 tiles should be placed)."
How on earth do I edit the condition in the loop to execute twice?

This is the code you pasted in:

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

The condition is, i < 10, so when we consider that i starts at zero and counts up to 10 (then stops iteration), how would we make it count only up to 2?

um, I’m guessing
(i < 2) ?

That’s a pretty good guess. Have you reasoned it out? Did you try it?

1 Like

Just did and it worked! Thank you!

1 Like

Im completely new at this i read what booga wrote and i think i kind of get it but not completely. I think my issue is that i dont fully remember what increment and iterations actually means so if someone could explain that it would be great . I eventually succeeded in making it so that only only 6 tiles showed up but i did it by changing (i < 10) to random numbers and eventually ended up with (i < 2) but there is clearly something im not getting because i dont understand how im supposed to figure out 2 will make it so that there are 6 tiles ?

This was very helpful. Thank you.

shouldn’t i be:
i =< 10?? Since the iteration has to happen 10 times? No LESS than 10 times?

I am very new and am confused about the i < 10 part.

In my mind, i should be i =< 10, since the loop has to Happen AT LEAST 10 times. No less?

Unless iteration 0 counts as the first iteration??

Someone help my little mind please haha, it would be appreciated :slight_smile:

1 Like