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

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Exiting Loops

Marcos Raj
PLUS
Marcos Raj
Courses Plus Student 5,979 Points

What is the use of Break, when you are increment the value of guessCount?

As in the second example, David gives the parameter to the while as guessCount < 10. and also use the break keyword to end the while loop.

David, when you are adding the guessCount with 1, then why did you use break keyword, without "break" also it may end, right? Please correct me if I missed anything?

1 Answer

Steven Parker
Steven Parker
231,269 Points

Dave explains this in the video.

He says, "Now the loop will either run ten times, or if the user guesses the correct number first, the loop ends because of the break statement."

So without the break it would still eventually end, but even if you guessed the right number along the way it would keep asking for more numbers until you entered 10 of them.