[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;
}
}
}
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;
}
}
}
Re: Some Explanation at switch case in c#
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.