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.
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;
}
}
}
}
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;
}
}
}
}
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;
}
}
}
}
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;
}
}
}
}
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;
}
}
}
}
Answer 1:
Output:
Explanation:
In the above program, we created a variable num, which is initialized with 10. After evaluating case statements, code will be like this:
Here, we passed -4 in switch then case -4 will execute and print "@@@@@@@@@@@@@@@" on the console screen.
Answer 2 :
Output:
Explanation:
The above program will generate syntax error because we cannot use floating-point values in switch case.
Answer 3:
Output:
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.
Answer 4:
Output:
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.
Answer 5:
Output:
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#.
need an explanation for this answer? contact us directly to get an explanation for this answer