Results 1 to 4 of 4

Thread: [RESOLVED] Some Explanation at switch case in c#

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Resolved [RESOLVED] Some Explanation at switch case in c#

    Can anybody tell me ???why this following code is not working .Kindly let me know the idea.Any help would be highly appreciated.
    Code:
    using System;
    class Example
    {
        static void Main()
          {
            int choice;
             choice =1;
             switch (choice)
             {
                 case 1:
                     Console.WriteLine("You Choose 1");
                 case 2:
                     Console.WriteLine("You choose 2");
                 case 3:
                     Console.WriteLine("You choose 3");
                 default: 
                     Console.WriteLine ("This is the defauld Option");
                     break;
             }
    
    
        }
    }

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Some Explanation at switch case in c#

    You should put a BREAK in each CASE, otherwise it will read all cases starting on the first until a BREAK is reached just like in your case.
    Code:
    using System;
    class Example
    {
        static void Main()
          {
            int choice;
             choice =1;
             switch (choice)
             {
                 case 1:
                     Console.WriteLine("You Choose 1");
    		 break;
                 case 2:
                     Console.WriteLine("You choose 2");
    		 break;
                 case 3:
                     Console.WriteLine("You choose 3");
    		 break;
                 default: 
                     Console.WriteLine ("This is the defauld Option");
                     break;
             }
    
        }
    }
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Some Explanation at switch case in c#

    Thank You.
    Last edited by firoz.raj; Jul 11th, 2010 at 12:30 AM.

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

    Re: Some Explanation at switch case in c#

    You should be ALWAYS reading the documentation first. If you'd read the documentation for the 'switch' statement then you'd have found out about 'break' for yourself. Now you can read the documentation for the 'break' statement.
    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

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