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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Learning coding](https://uploads.teamtreehouse.com/production/profile-photos/6969382/micro_trees.jpg)
Learning coding
Front End Web Development Techdegree Student 9,937 PointsCan someone give more explanation why there is an infinite loop at 1.38?
I don't fully grasp this yet.
2 Answers
![andren](https://secure.gravatar.com/avatar/229e60726cd92951c31d7303e40be2cd?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
andren
28,558 PointsA while
loop will run as long as the condition you provide it evaluates to the Boolean value true
. Comparisons and other conditions like that actually evaluate to true
and false
based on the result of the comparison.
In the video the Boolean value true
is entered directly as the condition, meaning that the loop will always run since there is no situation where true
does not evaluate to true
. It's basically like providing a condition of 1 == 1
, it's a condition that will never be false
.
It's worth nothing though that creating a loop with true
as its condition is not always a bad idea. In fact its a pretty common practice when you want the loop to run an uncertain number of times, but when you do that you have to have a break
statement somewhere within the loop. break
will cause the while
loop to end regardless of what its condition evaluates to.
![Learning coding](https://uploads.teamtreehouse.com/production/profile-photos/6969382/micro_trees.jpg)
Learning coding
Front End Web Development Techdegree Student 9,937 PointsThanks, now I understand it a lot better.
Learning coding
Front End Web Development Techdegree Student 9,937 PointsLearning coding
Front End Web Development Techdegree Student 9,937 PointsSo in the video the word true is not a boolean, but a condition? That seems confusing since true is used mostly as a boolean. And what is the point of a condition that has no evaluation? Or is that what you are explaining in the third paragraph that even though the condition is not evaluated it is possible to break out the loop to prevent an endless loop. And can you also see the statement break as an evaluation? Thanks.
andren
28,558 Pointsandren
28,558 PointsNo, it is a Boolean, but it is provided as the condition of the loop. The condition of a
while
loop is just a value that has to ultimately evaluate to eithertrue
orfalse
.As I mentioned above all comparisons provided to a
while
loop is ultimately evaluated to a Boolean value anyway. For example 2 > 1 evaluates totrue
, 5 < 1 evaluates tofalse
and so on. It is those evaluations that thewhile
statement is actually looking at, it never really looks at the actual comparison, only the Boolean it results in.By supplying a
Boolean
directly you are just cutting out the step of actually evaluating a comparison. As far as a thewhile
loop is concerned a condition oftrue
and a condition of1 == 1
is the same thing, they will both ultimately result intrue
every time they are evaluated.And the point is indeed what I tried to explain in the third paragraph, by using
true
as a condition you do essentially end up with an infinite loop, but by usingbreak
you can end the loop even though the condition to run it technically never evaluates tofalse
which is normally what ends the loop.That is most commonly done in situations where you don't really control when a loop will end, for example in a situation where you are using a
while
loop to continually prompt a user for some input where you only want to stop once the user has performed a certain action. In that case you could have anif
statement inside thewhile
loop that checked if the action had actually been completed andbreak
if it had.