FAQ: Loops - Putting it All Together

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

Yes, 0 is the first iteration. The while loop condition is checked (is i less than 10), if it evaluates to True then the body of the loop executes, once complete the loop condition is checked again repeating until the condition evaluates to False.

So the first one has i set to 0, with each new iteration increased by one at the end of the loop. On the final iteration the value of i starts at 9 (this is actually the tenth iteration since we started with i = 0). The final ++i statement increases i to 10 so the next time the condition is checked it fails and it ends after ten iterations.

Ten iterations and stop actually requires 11 checks of the while condition since the final one has to evaluate to False.

2 Likes

Ahh it makes more sense now looking at it this way, by asking myself is i still < 10? True, then run the body code. Until it is False. Thank you @tgrtim !! I appreciate your help.

1 Like

I honestly thought that i<10 meant that i was less that 10

Which is the condition that must be true for the loop to continue. i < 10 is a relational operation that always evaluates to a boolean.

1 Like