How to use switch case statement with int array elements in C#

2 Answers

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = { 13, 42, 128, 331 };

            switch (arr[0])
            {
                case 1:
                    Console.WriteLine(1);
                    break;
                case 13:
                    Console.WriteLine(13);
                    break;
                case 42:
                    Console.WriteLine(42);
                    break;
                case 128:
                    Console.WriteLine(128);
                    break;
                default:
                    Console.WriteLine("default");
                    break;
            }
        }
    }
}


/*
run:
 
13
 
*/

 



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

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = { 13, 42, 128, 331 };

            switch (arr[0])
            {
                case 1:
                    Console.WriteLine(1);
                    break;
                case 13:
                    Console.WriteLine(13);
                    switch (arr[1])
                    {
                        case 42:
                            Console.WriteLine(42);
                            break;
                    }
                    break;
                case 128:
                    Console.WriteLine(128);
                    break;
                default:
                    Console.WriteLine("default");
                    break;
            }
        }
    }
}


/*
run:
 
13
42
 
*/

 



answered Dec 30, 2016 by avibootz

Related questions

3 answers 236 views
1 answer 212 views
2 answers 258 views
3 answers 287 views
2 answers 242 views
2 answers 228 views
...