How to use switch case statement with int value in C#

3 Answers

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = 3;

            switch (n)
            {
                case 1:
                    Console.WriteLine(1);
                    break;
                case 2:
                    Console.WriteLine(2);
                    break;
                case 3:
                    Console.WriteLine(3);
                    break;
                case 4:
                    Console.WriteLine(4);
                    break;
            }
        }
    }
}


/*
run:

3

*/

 



answered Dec 29, 2016 by avibootz
0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = 27;

            switch (n)
            {
                case 1:
                    Console.WriteLine(1);
                    break;
                case 2:
                    Console.WriteLine(2);
                    break;
                case 3:
                    Console.WriteLine(3);
                    break;
                case 4:
                    Console.WriteLine(4);
                    break;
                default:
                    Console.WriteLine("default");
                    break;
            }
        }
    }
}


/*
run:
 
default
 
*/

 



answered Dec 29, 2016 by avibootz
edited Dec 29, 2016 by avibootz
0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = 3;

            switch (n)
            {
                case 1:
                case 2:
                case 3:
                    Console.WriteLine("1 or 2 or 3");
                    break;
                case 4:
                    Console.WriteLine("4");
                    break;
                case 5:
                    Console.WriteLine("5");
                    break;
                default:
                    Console.WriteLine("default");
                    break;
            }
        }
    }
}


/*
run:
 
1 or 2 or 3
 
*/

 



answered Dec 29, 2016 by avibootz
edited Dec 29, 2016 by avibootz

Related questions

2 answers 249 views
1 answer 212 views
2 answers 258 views
3 answers 288 views
2 answers 243 views
2 answers 228 views
...