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 trial

C# C# Basics (Retired) Perfect Final

Toshi Sosa
Toshi Sosa
8,004 Points

I don't understand why I get this error, "error CS0139: No enclosing loop out of which to break or continue"

I tried to put "break; " here or there, but none of my trial did not work. What I did do wrong or what is missing?

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
           try
            { 
              Console.Write("Enter the number of times to print \"Yay!\": ");

              int x = int.Parse(Console.ReadLine());
              var i = 0;
              while( x-i>0 )
                {
                  Console.WriteLine("Yay! ");
                  i += 1;
                }
            }  
              catch(FormatException)  
            {
                  Console.WriteLine("This is not valid input ");
                  continue;
            }        
        }
    }
}
Toshi Sosa
Toshi Sosa
8,004 Points

It worked! Thank you both of you. I was so close to giving up. Now I can move on.

3 Answers

Steven Parker
Steven Parker
231,248 Points

Max is right about the continue needing to be removed. But you have another issue as well.

When the parse creates an exception you give the message "This is not valid input ". But the challenge says you must issue the message “You must enter a whole number.”. They are sticklers for things like that.

Maximillian Fox
PLUS
Maximillian Fox
Courses Plus Student 9,236 Points

I'm no c# expert but this is a similar situation in other languages like Java and PHP.

The continue; in your catch block is causing the error, because it is not inside a for loop.

You should be able to safely remove this line and you code should then work fine :)

leiberr vergara
leiberr vergara
950 Points

so..continue doesn't work with the loops(while. for)?, because I understood that the continue is for go back in the first line of the Try and then ask again for a value and if we remove it...doesn't work at all. I coded this:

    public static void Main(){
         int number;
            try{



            Console.Write("Enter the number of times to print \"Yay!\": ");
                 number = Convert.ToInt32(Console.ReadLine());

                  for (int i=1;i<=number;i++){

            Console.Write("Yay!");

    }



            }
            catch(FormatException){
          Console.Write("You must enter a whole number.");

           //continue;
            }




        }