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 trialAndre Kucharzyk
4,479 PointsRefactoring 'break' instead of 'keep going = false' doubts
It's unclear to me if, apart from readability, there is any difference between using 'break' instead of 'keep going = false'.
At one point in video, lecturer says that refactoring is purely made for order and readability, but after that, he implies that using 'break' instead of 'keep going = false' makes the loop exit immediately instead of waiting for loop to repeat.
Secondly, what is the difference between one and another? Break exits immediately and when using keepGoing = false; exits when? after
keepGoing = false }
??
1 Answer
Steven Parker
231,269 PointsA "break" exits the loop immediately. Changing a variable that is used in the loop's conditional expression causes the loop to end when it starts to repeat the next cycle.
Depending on how your code is structured, it might be effectively identical. But with a different structure it could make a difference of executing a significant number of code lines or not.