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

Else statement is not printing.

I'm working on getting the program to print out a statement when the 'else' condition is entered, but it prints nothing to the screen. Here's the section of code:

int numberValue = 1;
                try 
                {
                    int numberYay = int.Parse(numberEntry);                        
                    while (numberValue <= numberYay)
                    {     
                      if (numberYay >=1)
                      {
                        Console.WriteLine("Yay!");
                         numberValue += 1;
                      }
                        else 
                        {
                            Console.WriteLine("Please enter a positive number.");
                            break;
                        }

                    }  

                }
Program.cs
using System;

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


            int numberValue = 1;
                try 
                {
                    int numberYay = int.Parse(numberEntry);                        
                    while (numberValue <= numberYay)
                    {     
                      if (numberYay >=1)
                      {
                        Console.WriteLine("Yay!");
                         numberValue += 1;
                      }
                        else 
                        {
                            Console.WriteLine("Please enter a positive number.");
                            break;
                        }

                    }  

                }
                catch(FormatException)
                    {
                      Console.WriteLine("You must enter a whole number. ");  
                    }
        }
    }
}

1 Answer

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

If I read this correctly:

// this will ONLY run WHILE numberValue <= numberYay
// the else-statement looks miss-placed
while (numberValue <= numberYay)  // while 'true'
{     
  if (numberYay >=1) // if 'everything is okay'
  {
    Console.WriteLine("Yay!"); // print 'Yay!'
    numberValue += 1;  // then increment
  }
  else 
  {
    // you don't want this message when it's done printing 'Yay!'
    Console.WriteLine("Please enter a positive number.");
    break;
  }
}

Thank you!! I changed the 'else' to an 'if' and moved it outside of the 'try' statement and it worked.