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

Jun Dong
Jun Dong
2,496 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

CC Bekter
CC Bekter
Full Stack JavaScript Techdegree Student 5,249 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");