Home »
C#.Net »
C#.Net find output programs
C#.Net find output programs (switch statement) | set 2
Find the output of C#.Net programs | switch statement | Set 2: 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)
{
int num = 10;
switch (-4)
{
case 1-5:
Console.WriteLine("@@@@@@@@@@@@@@@");
break;
case 21-11:
Console.WriteLine("$$$$$$$$$$$$$$$");
break;
case 31-15:
Console.WriteLine("$$$$$$$$$$$$$$$");
break;
default:
Console.WriteLine("###############");
break;
}
}
}
}
Output:
@@@@@@@@@@@@@@@
Press any key to continue . . .
Explanation:
In the above program, we created a variable num, which is initialized with 10. After evaluating case statements, code will be like this:
switch (-4)
{
case -4:
Console.WriteLine("@@@@@@@@@@@@@@@");
break;
case 10:
Console.WriteLine("$$$$$$$$$$$$$$$");
break;
case 16:
Console.WriteLine("$$$$$$$$$$$$$$$");
break;
default:
Console.WriteLine("###############");
break;
}
Here, we passed -4 in switch then case -4 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)
{
double NUM = 2.34;
switch (NUM)
{
case 2.34:
Console.WriteLine("@@@@@@@@@@@@@@@");
break;
case 1.23:
Console.WriteLine("$$$$$$$$$$$$$$$");
break;
case 4.2:
Console.WriteLine("$$$$$$$$$$$$$$$");
break;
default:
Console.WriteLine("###############");
break;
}
}
}
}
Output:
main.cs(12,13): error CS0151: A switch expression of type `double' cannot be
converted to an integral type, bool, char, string, enum or nullable type
Explanation:
The above program will generate syntax error because we cannot use floating-point values in switch case.
Question 3:
using System;
namespace Demo
{
class Program
{
//Entry point of the program
static void Main(string[] args)
{
switch ((int)2.34)
{
case 2:
Console.WriteLine("@@@@@@@@@@@@@@@");
break;
case 1:
Console.WriteLine("$$$$$$$$$$$$$$$");
break;
case 4:
Console.WriteLine("$$$$$$$$$$$$$$$");
break;
default:
Console.WriteLine("###############");
break;
}
}
}
}
Output:
@@@@@@@@@@@@@@@
Press any key to continue . . .
Explanation:
In the above program, we passed (int) 2.34 in the switch block, then it will be "2" after typecast, then case 2 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)
{
string STR = "hello";
switch (STR)
{
case "HELLO":
Console.WriteLine("@@@@@@@@@@@@@@@");
break;
case "hello":
Console.WriteLine("$$$$$$$$$$$$$$$");
break;
case "STR":
Console.WriteLine("$$$$$$$$$$$$$$$");
break;
default:
Console.WriteLine("###############");
break;
}
}
}
}
Output:
$$$$$$$$$$$$$$$
Press any key to continue . . .
Explanation:
In the above program, we created a string variable STR, which is initialized with "hello". Then (case "hello") will execute and print "$$$$$$$$$$$$$$$" on the console screen.
Question 5:
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;
case 'H':
Console.WriteLine("%%%%%%%%%%%%%%%");
break;
default:
Console.WriteLine("###############");
break;
}
}
}
}
Output:
main.cs(14,22): error CS0029: Cannot implicitly convert type `string' to `char'
Explanation:
The above program will generate syntax error, because we used string instead of a character in (case "h" ), here we passed character in the switch block.
Note: One or more character(s) are enclosed with double quotes, is known as a string. And character is represented using single quotes C#.