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.
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;
}
}
}
}
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");
}
}
}
}
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;
}
}
}
}
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;
}
}
}
}
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;
}
}
}
}
Answer 1:
Output:
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.
Answer 2:
Output:
Explanation:
The above code will generate syntax errors because we cannot define case without a break statement in the switch block.
Answer 3:
Output:
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.
Answer 4:
Output:
Explanation:
The above program will generate syntax error because we cannot use to in the case of the switch block.
Answer 5:
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:
The value of num is 10, then default case will execute and print "###############" on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer