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 trialjaneporter
Courses Plus Student 23,471 Pointscompiler error: Program.cs(14,17): error CS0139: No enclosing loop out of which to break or continue Compilation failed
I'm getting the following error on the first task of the last code challange in c# basics:
Program.cs(14,17): error CS0139: No enclosing loop out of which to break or continue
Compilation failed: 1 error(s), 0 warnings
here is my code for this task:
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
int total = 1;
Console.Write("Enter the number of times to print \"Yay!\": ");
string entry = Console.ReadLine();
int times = int.Parse(entry);
if (times <= 0)
{
Console.WriteLine(times + " is not a valid entry.");
continue;
}
else
{
while(total <= times)
{
Console.WriteLine("Yay!");
total += 1;
}
Console.WriteLine("You have printed \"Yay!\" " + total + " to the screen.");
}
}
}
}
what am i doing wrong?
2 Answers
Steven Parker
231,236 PointsYou have a "continue" statement that is not inside a loop.
It's about halfway down the code, inside the "if". Just remove it.
Once it compiles, you'll notice your loop prints more times than desired. To fix that you can change the comparison in the "while" to be just less-than "<
" instead of less-than-or-equal "<=
".
janeporter
Courses Plus Student 23,471 Pointsthat worked. thank you.