Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
hi everyone
how i can use switch like this code


PS. this code Ada
case n is
      when 0 =>
         Put_Line ("You typed zero");
      when 1 | 9 =>
         Put_Line ("n is a perfect square");
      when 2 =>
         Put_Line ("n is a prime number");
         Put_Line ("n is an even number");
      when 3 | 5 | 7 =>
         Put_Line ("n is a prime number");
      when 4 =>
         Put_Line ("n is a perfect square");
         Put_Line ("n is an even number");
      when 6 | 8 =>
         Put_Line ("n is an even number");
   end case;


so how i can write switch by c# like this code

i want
int x=10;
            switch (x)
            {
                case 1 | x<2: // i  want check x if < 2 and > 20 ok 
                    Console.Write("1");
                    break;
                case (2) :
                    Console.Write("1");
                    break;
                case(22):
                    Console.Write("2");
                    break;
            }
this code give me error
in case 1 | x<2
thnx for any help
Posted
Updated 29-Oct-10 20:22pm
v2

switch(x)
{
case 0:
Console.Write("You typed zero");
break;
case 1 :
case 9 :
Console.Write("n is a perfect square");
break;
case 2 :
Console.Write("n is a prime number");
Console.Write("n is an even number");
break;
case 3: 
case 5: 
case 7:
Console.Write("n is a prime number");
break;
case 4 :
Console.Write("n is a perfect square");
Console.Write("n is an even number");
break;
case 6 :
case 8 :
Console.Write("n is an even number");
break;
}

int x=10;

switch (x)
{
case 2 :
Console.Write("1");
break;
case 22:
Console.Write("2");
break;
}
if(x<2 || x>20) // i want check x if < 2 and > 20 ok 
Console.Write("1");

i want check x if < 2 and > 20
it is not possible but x is < 2 Or x > 20 is possible.
there is no number that is less than 2 and gather than 20.

Please vote and Accept Answer if it Helped.
 
Share this answer
 
I think You Can Simply Use if else statements... It will be easy.

XML
case 1 
if x<2 and x>20 then // i want check x if < 2 and > 20 ok
Console.Write("1");
end if
break;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900