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

Benneth Paredis
Benneth Paredis
4,597 Points

What is wrong with my code ?

what am i doing wrong with my code?

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            string inputS = Console.ReadLine();
            int input = int.Parse(inputS);
            while(true) 
            {
              int counter = 0;

                if(counter == input)
                 {
                   break;
                 }
               else
                {
                   counter++;
                   Console.WriteLine("Yay!");
                   continue;
                }

           }
        }
    }
}

1 Answer

Steven Parker
Steven Parker
231,269 Points

:point_right: It looks like you've created an infinite loop.

Inside your loop, you set counter to zero right before you compare it with the input. You probably should initialize the counter outside the loop.

Also, while your method can work, it might be clearer to put the counter test in the while expression. Then you won't need special code inside the loop to escape from it.

And a "continue" is never needed when it's the last thing in a loop.

Benneth Paredis
Benneth Paredis
4,597 Points

Thank you these are really simple things i always look over xD