Results 1 to 4 of 4

Thread: switch

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    switch

    Heya;

    Im a newbie in C# and Im trying to loop 5 times in a console app. However the program exits on the second entry. The problem seems to be the For control . Anyone can point out to me the mistake pls/



    Code:
    namespace ConsoleSwitch
    {
        class Program
        {
            static void Main(string[] args)
            {
                int x;
                string colour,str;
                string Blue,Red, Yellow ;
                Console.WriteLine("Enter the primary colours colour");
                colour = Console.ReadLine();
                for (x = 1; x <= 5; x++) ;
    
                switch (colour)
                {
                    case  "Blue":
                        Console.WriteLine("Blue is a primary colour");
                        //str = Console.ReadLine();
                        break;
    
                    case  "Red":
                        Console.WriteLine("Red is a primary colour");
                        //str = Console.ReadLine();
                        break;
    
                    case "Yellow":
                        Console.WriteLine("Yellow is a primary colour");
                        break;
                    case "White":
                        Console.WriteLine("White is a primary colour");
                        break;
                    case "Black":
                        Console.WriteLine("Black is a primary colour");
                        break;
                    
                    default:
                        Console.WriteLine("Colour is NOT a primary colour");
                        //str = Console.ReadLine();
                        break;
                        
                 } //end of switch
                str = Console.ReadLine();
              } //end of For
            
           }
    Last edited by angelica; Jan 3rd, 2009 at 11:36 PM.
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  2. #2
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: switch

    You have an empty 'for' loop - the semi-colon at the end of the statement is an empty statement that is the only subject of the loop.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: switch

    As David says. The semicolon after your 'for' statement is the equivalent of Next in VB. If the 'for' loop contains more than one statement then you must enclose those statements in braces to indicate that they constitute a single block.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Re: switch

    Righto, Many thanks.
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width