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

Li Yu
Li Yu
2,067 Points

C# How could I fix the following error: Program.cs(12,36): error CS1525: Unexpected symbol `}'

I wonder why the compiler thinks that there is an unexpected symbol, and how could I fix the error. Thanks for the reply.(I have post a similar question before. There are some nice advice, but I still fail to pass the same challenge after trying.)

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            string enter = Console.ReadLine();      //type in one time only
            int number = int.Parse(enter);
            if(true)
            {            
                for(int t = 0; t < number)
                {
                    Console.WriteLine("Yay!");
                    t = t + 1;
                    continue;
                }
                 break;
            } 
            else
            {
                Console.Write("error");
            }

        }
    }
}

1 Answer

Steven Parker
Steven Parker
231,269 Points

:point_right: Your for loop is incomplete.

A for loop has three parts (initializer, condition, and iterator) separated by semicolons. Your loop above has only two. The parts themselves are optional, but the separators are not.

You have "t = t + 1" on a separate line to do the job of the iterator, and while it is unconventional not to include the iterator directly in the for, you can resolve your immediate issue just by adding the missing separator:

                for (int t = 0; t < number; )    // <-- note added semicolon

A more conventional solution would involve moving that iterator code into the for:

                for (int t = 0; t < number; t = t + 1)    // or t += 1,  or t++

I also noticed a few other issues:

  • the "break" should be removed as it is not allowed outside of a loop
  • the statement and code block following the "else" can be removed as it will never be executed
  • the statement "if (true)" can be removed, since the following code will always be executed

The other hints I gave you previously may still come in handy when you get to task 3.

Li Yu
Li Yu
2,067 Points

Thank you, Steven Parker. I tried a few times and pass the challenge. Thanks a lot.