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 trialAlan Mattan贸
Courses Plus Student 12,188 PointsWhat is the difference between break and continue in C# ?
I'm a bit lost here. In C#, what is the difference between using: break; and continue;
At the quiz exame, the code
while(true)
{
continue;
Console.WriteLine("Ribbit")
}
Console.WriteLine("Croak");
Continue do not print Croak, why?
2 Answers
bothxp
16,510 PointsContinue is used to trigger the next iteration of the loop. In the example:
while(true)
{
continue;
Console.WriteLine("Ribbit")
}
Console.WriteLine("Croak");
When you get to the continue you will then jump back up to the test in the While. So in this case you will never get to the Rabbit line and while(true) will never evaluate to false so you will never exit the loop to get to the Croak.
Break exits the loop and moves straight on to the line immediately after the loop.
--
Break will exit the loop completely, Continue will just skip the current iteration and then test the while statement again.
Andy Swinford
8,152 PointsContinue goes on to the next iteration of a loop whereas break ends the loop and moves on to the next step.
Alan Mattan贸
Courses Plus Student 12,188 PointsAlan Mattan贸
Courses Plus Student 12,188 PointsThanks Bothxp so far. Perfect explanation, now is more clear also the purpose of the code!
If is possible, can you add 2 extra examples that help using both in common situations?