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

ali raafat
ali raafat
444 Points

i got a var error

i got var error

Program.cs
using System;

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

               while (loop < times)
               {

                    var loop = 0
                   var entry = Console.ReadLine();
                   loop += 1 ;
               }


        }
    }
}

2 Answers

Steven Parker
Steven Parker
231,108 Points

That's the first of a few issues you'll need to fix. Here's some hints:

  • you declared loop inside the while loop, but you need to declare it before the while loop.
  • you forgot to end the line declaring loop with a semicolon (";")
  • you won't need to call ReadLine again
  • you still need to print "Yay!" inside the loop
ali raafat
ali raafat
444 Points

why should i be outside the loop. it worked but can you explain the question to me

Steven Parker
Steven Parker
231,108 Points

Declaration and initialization should only happen one time. If you put it inside the loop, it will happen over and over.

Also, if you have not already declared it before the while statement references it, it will cause a compiler error.

Even more important, if you set the value to 0 inside the loop, then increment it to 1, it will always be smaller than the times value, and the loop will never end.