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 trial
Frank keane
8,074 PointsI don't get the 'while (true)'
I don't get the 'while (true)' - while what is true? does it declare that the following is true?
Also why does the 'break' stop the while loop if it is inside the if loop?
3 Answers
Frank keane
8,074 PointsThanks First! I get it now, true has the value of true so its like saying if 2+2=4. Nice one.
doesitmatter
12,885 PointsA while loop will repeat the code inside of its braces as long as the condition is true. If we say while(true) we're saying: infinitely repeat the code inside the braces. This of course would make any computer crash, so to avoid this, we have to include some kind of stop condition within the loop. something like if a > 2 then break out of the loop.
Frank keane
8,074 PointsGotcha, thanks!
doesitmatter
12,885 Pointsdoesitmatter
12,885 Pointsexactly while(2+2 === 4){ // do something} is equavalent to while(true){ // do something}