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

John Lukacs
John Lukacs
26,806 Points

Program.cs(19,21): error CS0139: No enclosing loop out of which to break or continue

How do I break out of a loop;
My code wont pass in the second question on the last challenge I think my code is looping without an end. Im down to one error I dont know what to do

Program.cs
using System;

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


            try{
                string numofTimes= Console.ReadLine();
            int num =int.Parse(numofTimes);
                 for(var i=0; i<num; i++){
         Console.WriteLine("\"Yay!\"");
                     break;
            }}
                catch(FormatException){
                Console.WriteLine("You must enter a whole number.");
                    continue;



            }
        }
    }
}

3 Answers

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

you don't need the continue or the break. the continue is not in a loop so it is not doing anything. the break is causing the program to end prematurely, before all the yays have been printed. just remove the break and the continue and the code passes tasks 1 and 2.

John Lukacs
John Lukacs
26,806 Points

Hi James I have tried that It still does not work

Kyle Jensen
PLUS
Kyle Jensen
Courses Plus Student 6,293 Points

You don't have a need for a break statement. Your program will end when 'i' hits the ceiling input provided from the user - the second arg in your loop. The reason the compiler complains about 'continue' is because 'continue' can only be implemented within the loop. The program is going to continue regardless. The point of the continue, within a loop, is to avoid execution upon a certain condition. Let's take a look at an example that assumes we're using System; and that the user entered 10 times.

for(var i = 0; i < 10; i++) { 
    //just for fun lets print the 7th iteration as its own thing
    if(i == 6) { 
        Console.WriteLine("I'm " + (i + 1) + " and I'm hungry - I'm going to eat number " + (i + 3) ); 
    } 
    //now lets see the continue statement in action by skipping printing the 9
    else if(i == 8) {
        continue; 
}
    else { 
        Console.WriteLine("iteration: " +  (i + 1)); 
    } 
}

if you were to take out the if( i == 6) line each iteration would print exactly the same except for the number 9. 9 is skipped because of the continue within our conditional.
I hope this helps!!