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!

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

C# C# Basics (Retired) Perfect break and continue

Matthew Musni
Matthew Musni
610 Points

I dont get the logic of the 3rd question in this quiz

Can someone explain why the answer is 2.

isnt it that counter = 0 then 0+1 = the new value for counter then print the value of counter : why is 2 instead of 1 what am i missing?

1 Answer

28,552 Points

You are missing that the counter += 1; line is within a loop, that means that it will run multiple times until either the condition of the loop becomes false (the condition is counter < 5) or it encounters a break statement.

Since there is a if statement within the loop that checks if counter equals 2 and that if statement contains a break statement the counter ends up equal to 2 before it gets printed, since the print line is outside the loop. If the break statement had not been there then it would have ended up as 5.