Home »
C#.Net »
C#.Net find output programs
C#.Net find output programs (switch statement) | set 1
Find the output of C#.Net programs | switch statement | Set 1: 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)
{
bool value;
value = (null == 0);
switch (value)
{
case true:
Console.WriteLine("www.includehelp.com");
break;
case false:
Console.WriteLine("www.google.com");
break;
}
}
}
}
Output:
www.google.com
Press any key to continue . . .
Explanation:
In the above program, we created a variable value, which is assigned by the below expression,
value = (null == 0);
Here we compare null wit 0 then it will return false and assigned to variable value. that's why case false will execute and print "www.google.com" on the console screen.
Question 2:
using System;
namespace Demo
{
class Program
{
//Entry point of the program
static void Main(string[] args)
{
bool value;
value = ((int)(Math.PI) == 3);
switch (value)
{
case true:
Console.WriteLine("www.includehelp.com");
case false:
Console.WriteLine("www.google.com");
}
}
}
}
Output:
main.cs(16,17): error CS0163: Control cannot fall through from one case
label `case true:' to another
main.cs(19,17): warning CS0162: Unreachable code detected
main.cs(19,17): error CS8070: Control cannot fall out of
switch statement through final case label `case false:'
Explanation:
The above code will generate syntax errors because we cannot define case without a break statement in the switch block.
Question 3:
using System;
namespace Demo
{
class Program
{
//Entry point of the program
static void Main(string[] args)
{
bool value;
value = ((int)(Math.PI) == 3);
switch (value)
{
default:
Console.WriteLine("###############");
break;
case true:
Console.WriteLine("@@@@@@@@@@@@@@@");
break;
case false:
Console.WriteLine("$$$$$$$$$$$$$$$");
break;
}
}
}
}
Output:
@@@@@@@@@@@@@@@
Press any key to continue . . .
Explanation:
In the above program, we created a boolean variable value, which is initialized with below expression,
value = ((int)(Math.PI) == 3);
value = ((int)(3.14159265358979) == 3);
value = (3 == 3);
value = true;
Then the case true 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 num = 10;
switch (num)
{
default:
Console.WriteLine("###############");
break;
case 1 to 5:
Console.WriteLine("@@@@@@@@@@@@@@@");
break;
case 6 to 10:
Console.WriteLine("$$$$$$$$$$$$$$$");
break;
case 11 to 15:
Console.WriteLine("$$$$$$$$$$$$$$$");
break;
}
}
}
}
Output:
main.cs(17,23): error CS1525: Unexpected symbol `to'
main.cs(20,23): error CS1525: Unexpected symbol `to'
main.cs(23,24): error CS1525: Unexpected symbol `to'
Explanation:
The above program will generate syntax error because we cannot use to in the case of the switch block.
Question 5:
using System;
namespace Demo
{
class Program
{
//Entry point of the program
static void Main(string[] args)
{
int num = 10;
switch (num)
{
case (11-5):
Console.WriteLine("@@@@@@@@@@@@@@@");
break;
case (21-10):
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 (num)
{
case 6:
Console.WriteLine("@@@@@@@@@@@@@@@");
break;
case 11:
Console.WriteLine("$$$$$$$$$$$$$$$");
break;
case 16:
Console.WriteLine("$$$$$$$$$$$$$$$");
break;
default:
Console.WriteLine("###############");
break;
}
The value of num is 10, then default case will execute and print "###############" on the console screen.