C#.Net find output programs (switch statement) | set 3

Find the output of C#.Net programs | switch statement | Set 3: Enhance the knowledge of C#.Net switch statement concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 06, 2021

Question 1:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            string STR = "hello";

            switch (STR[0])
            { 
                case 'h':
                    Console.WriteLine("$$$$$$$$$$$$$$$");
                    break;
                case 'H':
                    Console.WriteLine("%%%%%%%%%%%%%%%%");
                    break;
                default:
                    Console.WriteLine("###############");
                    break;
            }
        }
    }
}

Output:

$$$$$$$$$$$$$$$
Press any key to continue . . .

Explanation:

In the above program, we declared a string variable STR, which is initialized with "hello" and we pass STR[0] in the switch block, it will access the first character from the string that is 'h', that's why " case 'h' " will execute and print "$$$$$$$$$$$$$$$" on the console screen.

Question 2:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int A = 65;

            switch (A)
            { 
                case 'A':
                    Console.WriteLine("$$$$$$$$$$$$$$$");
                    break;
                case 'a':
                    Console.WriteLine("%%%%%%%%%%%%%%%%");
                    break;
                default:
                    Console.WriteLine("###############");
                    break;
            }
        }
    }
}

Output:

$$$$$$$$$$$$$$$
Press any key to continue . . .

Explanation:

In the above program, we created an integer variable, which is initialized with 65, and we know that the 65 is the ASCII value of 'A', then "case 'A'" will execute and print "$$$$$$$$$$$$$$$" on the console screen.

Question 3:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int A = 0x0B;

            switch (A)
            { 
                case 10:
                    Console.WriteLine("$$$$$$$$$$$$$$$");
                    break;
                case 11:
                    Console.WriteLine("%%%%%%%%%%%%%%%%");
                    break;
                default:
                    Console.WriteLine("###############");
                    break;
            }
        }
    }
}

Output:

%%%%%%%%%%%%%%%%
Press any key to continue . . .

Explanation:

In the above program, we created an integer value which is initialized with hex value 0x0B, The hex value 0x0B is equivalent to 11 in decimal numbers. That's why "case 11" will execute and print "%%%%%%%%%%%%%%%%" on the console screen.

Question 4:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int A = 0x10;
            int B = 0x12;

            switch (A)
            { 
                case 10:
                    switch(B)
                    {
                        case 12:
                            Console.WriteLine("111111111111111111");
                            break;
                        case 15:
                            Console.WriteLine("222222222222222222");
                            break;
                        case 18:
                            Console.WriteLine("333333333333333333");
                            break;
                    }
                    break;
                case 16:
                    switch (B)
                    {
                        case 12:
                            Console.WriteLine("AAAAAAAAAAAAAAAAAA");
                            break;
                        case 15:
                            Console.WriteLine("BBBBBBBBBBBBBBBBBBB");
                            break;
                        case 18:
                            Console.WriteLine("CCCCCCCCCCCCCCCCCCC");
                            break;
                    }
                    break;
                default:
                    Console.WriteLine("###############");
                    break;
            }
        }
    }
}

Output:

CCCCCCCCCCCCCCCCCCC
Press any key to continue . . .

Explanation:

In the above program, we created two integer variables A and B that are initialized with 0x10 and 0x12 respectively.

HEX		DECIMAL
0x10		16
0x12		18

Here, we used nested switch block, 16 is equivalent to 0x10 then "case 16" will execute, and in the inner switch block "case 18" will execute and print "CCCCCCCCCCCCCCCCCCC" on the console screen.

Question 5:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int A = 0x13;
            int B = 0x15;

            switch (A)
            { 
                case 18:
                    switch(B)
                    {
                        case 12:
                            Console.WriteLine("111111111111111111");
                            break;
                        case 15:
                            Console.WriteLine("222222222222222222");
                            break;
                        case 21:
                            Console.WriteLine("333333333333333333");
                            break;
                    }
                    break;
                case 16:
                    switch (B)
                    {
                        case 12:
                            Console.WriteLine("AAAAAAAAAAAAAAAAAA");
                            break;
                        case 15:
                            Console.WriteLine("BBBBBBBBBBBBBBBBBBB");
                            break;
                        case 18:
                            Console.WriteLine("CCCCCCCCCCCCCCCCCCC");
                            break:
                    }
                    break;
                default:
                    Console.WriteLine("###############");
                    break;
            }
        }
    }
}

Output:

main.cs(40,33): error CS1525: Unexpected symbol `:', expecting `;'

Explanation:

The above program will generate syntax errors due to the below statement,

break:

In the above break statement we used colon ':' instead of semicolon ';' that's why error will be generated in the above program.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.