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

Charles Szvetecz
Charles Szvetecz
1,278 Points

Task #1 of final code challenge for C# Basic Module

I'm stuck on this task. Apparently I need to add a counter to a loop but I don't recall and can't find where that was demonstrated in the course videos. I believe the solution is a combination of a While loop combined with If/Else statement??? So I'm looking for a hint and/or direction on this aspect of the solution. However, it would also be helpful to see a final "working" version of the code so I can move on if need to. Want to figure it out but also want to be able to move on with the track as well. Thanks!

Program.cs
using System;

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

            while (keepGoing)
            if (loopCount <= entry)
            {
                Console.Write("Yay!");
            }
            else 
            loopCount = (loopCount + 1);



        }
    }
}

1 Answer

Steven Thacher
Steven Thacher
3,548 Points

A for loop is often your best option in this situation. If you want to iterate up to some number, a for loop in that range often works best.

You won't need an extra 'keepGoing' variable, as the for loop will take care of that logic for you. Not only that, it also handles the loop counter for you.

for example:

for (int i = 0; i < entry; i++) { //print yay }

The above loop will start at 0, then in each iteration it will increase 'i' by 1. And it will loop while 'i' is less than entry (or i is equal to entry - 1).