Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMax Botez
5,146 PointsMore about how loop [do while] works.
Hi,
Guys, who could clarify one more time how while, do while, for loops and I don't really get what does it mean js x +=1
I am doing the quizs but it seems that I am guessing and for me it means that I don't understand something.
Kind appreciate Max
3 Answers
Nick Renteria
10,258 PointsHi Max. To start, the i += 1 is the same as saying i = i + 1 (and the same as i++ as Isaac mentioned above). So this just means that, with i (or x) as your starting variable set at 0 at the beginning, each time through the loop the i variable has 1 added to it. So after the first loop i = 1, after the second loop i = 2, and so on. Now, while loops and do while loops are very similar. The main difference is that a do while loop will always run at least once, whereas a while loop could run 0 times if its initial condition is not true. For example:
var i = 0;
while ( i < 0 ) {
alert('This alert will not show.');
i += 1;
}
This while loop will not run, because its condition starts out false. The variable i is set to equal 0 above it, and since 0 is not less than 0, the condition is false and the while loop will not run. However, consider the following do while loop:
var i = 0;
do {
alert('This alert will show.');
i += 1;
} while ( i < 0 )
Here, the alert will show once because the 'do' part is executed before the while condition is checked. It doesn't matter that i is never less than 0, the do part is run at least once either way. Finally, a for loop is just a quick way to write out a while or do while loop. For example:
for ( let i = 0; i < 10; i += 1) {
alert('This alert will show 10 times.');
}
This above code allows us to do three things right away inside the parentheses after the word 'for': set a starting variable value for i, then give a condition saying that in this case the loop will run as long as i is less than 10, and then say that each time through the loop the value of i is increased by 1. It does the same thing but in less space. I really hope this helps!
Isaac Hong
115 Pointsfor loop syntax var i; for (i=0; i<9;i++){ document.write(i); } this prints from 0 to 8 it means document.write keeps printing numbers until the number is less than 9 . i++ means the programs adds 1 to the initial number, in this case which is 0 (i=0).
Max Botez
5,146 PointsThanks guys, totally made sense. Now let me tell my words how I got it.
So basically while, do while and for - do the same but with some small differences do while - do the same as while but it will run the loop at least ones and it doesn't matter if is true or false (if false then loop will stop)
for - allow us to do again, the same but will less code, you can run all in one or two lines of code.
i - name of the variable it could be you was mentioned x, z, ΠΈ - basically everything. i += 1 - will increase one up i -= 1 - will decrease one down (another question here lets say could I increase 2 or more the same time, i += 3)
Guys, do you know if there something/somewhere where you could do more practice with javascript and kind of to find in real live where and how its using.
Cheers, Max
Max Botez
5,146 PointsMax Botez
5,146 Pointsreally a big thank you for your time to write and explain it so clear