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

Is there a loop that will perform on how many times you tell it to?

IS there a loop that you can give an int and it will execute that many times?

Program.cs
using System;

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

            string entry = Console.ReadLine();

            int times = int.Parse(entry);

            if(times)
            {
                Console.WriteLine("Yay!");
            }





        }
    }
}

3 Answers

Yes, you need to use a for loop.

Basic format of a for loop is like so...

for(int i = 1; i <= times; i++)
{
    statements you want to execute
}

You need to do exactly what I have shown in my first response and add the command to print the string Yay! to the screen inside the braces.

for(int i = 1; i <= times; i++)
{
    Console.WriteLine("Yay!");
}

So how could I write it in respect to the program. To write it as many times as the user wants