Results 1 to 4 of 4

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

  1. #1
    Frenzied Member
    Join Date
    Jan 09
    Location
    Watch Window(Shift+f9)
    Posts
    1,431

    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 05
    Location
    Philippines
    Posts
    10,229

    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;
             }
    
        }
    }

  3. #3
    Frenzied Member
    Join Date
    Jan 09
    Location
    Watch Window(Shift+f9)
    Posts
    1,431

    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
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,860

    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.

Posting Permissions

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