C# C# Basics (Retired) Perfect Refactoring

Andre Kucharzyk
Andre Kucharzyk
4,479 Points

Refactoring '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
Steven Parker
171,904 Points

A "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.