Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Jun Dong
Jun Dong
4,754 Points

What does continue; do

while(true)
{    
    continue;
    Console.WriteLine("Ribbit")
}

Console.WriteLine("Croak");

This is one of the quiz questions i don't understand why this code doesn't print anything

1 Answer

cbekter
cbekter
7,232 Points

Continue allows you to control the flow of your loop by skipping the current iteration and returning back to the beginning of the loop. In your example, when you get into the loop, you are immediately taken out of it because of continue, however, because true is still "true", you are in an infinite loop that just keeps taking you out the iteration and starting back at the beginning. If something were to, for example, change the true value to false, it would go back to the beginning of loop, see that true is now false and be able to print out "Croak".

bool example = true;

            while (example)
            {
                example = false;
                continue;
                Console.WriteLine("Ribbit");

            }

            Console.WriteLine("Croak");