0% found this document useful (0 votes)
44 views

Switch - Case - Example

The document contains 7 examples of C# code using switch statements. The switch statements are used to: 1) Print the number corresponding to an integer input 2) Print a message corresponding to a character grade input 3) Check if a character is a vowel or not 4) Check for multiple vowel cases in one statement 5) Perform basic math operations based on operator character input 6) Print the weekday name for a number input 7) Print the month name for a number input

Uploaded by

Aram Shexbzeny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Switch - Case - Example

The document contains 7 examples of C# code using switch statements. The switch statements are used to: 1) Print the number corresponding to an integer input 2) Print a message corresponding to a character grade input 3) Check if a character is a vowel or not 4) Check for multiple vowel cases in one statement 5) Perform basic math operations based on operator character input 6) Print the weekday name for a number input 7) Print the month name for a number input

Uploaded by

Aram Shexbzeny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

- First Example:

using System;
class Test
{
static void Main(string[] args)
{
int i = 2;
switch (i)
{
case 1: Console.WriteLine("Number is 1");
break;
case 2: Console.WriteLine("Number is 2");
break;
default: Console.WriteLine("Number is neither 1 nor 2");
break;
} } }

- Second Example:
using System;
class Test
{
static void Main(string[] args)
{
char grade = 'B';
switch (grade)
{
case 'A': Console.WriteLine("Outstanding!");
break;
case 'B': Console.WriteLine("Excellent!");
break;
case 'C': Console.WriteLine("Good!");
break;
case 'D': Console.WriteLine("Can do better!");
break;
case 'E': Console.WriteLine("Just passes!");
break;
case 'F': Console.WriteLine("Failed!");
break;
default: Console.WriteLine("Invalid Grade!");
break;
} } }
- Third Example:
using System;
namespace Conditional
{
class SwitchCase
{
public static void Main(string[] args)
{
char ch;
Console.WriteLine("Enter an alphabet");
ch = Convert.ToChar(Console.ReadLine());
switch(Char.ToLower(ch))
{
case 'a': Console.WriteLine("Vowel");
break;
case 'e': Console.WriteLine("Vowel");
break;
case 'i': Console.WriteLine("Vowel");
break;
case 'o': Console.WriteLine("Vowel");
break;
case 'u': Console.WriteLine("Vowel");
break;
default: Console.WriteLine("Not a vowel");
break;
} } } }

- Fourth Example:
using System;
namespace Conditional
{
class SwitchCase
{
public static void Main(string[] args)
{
char ch;
Console.WriteLine("Enter an alphabet");
ch = Convert.ToChar(Console.ReadLine());
switch(Char.ToLower(ch))
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
Console.WriteLine("Vowel");
break;
default: Console.WriteLine("Not a vowel");
break;
} } } }
- Fifth Example:
using System;
namespace Conditional
{
class SwitchCase
{
public static void Main(string[] args)
{
char op;
double first, second, result
Console.Write("Enter first number: ");
first = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter second number: ");
second = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter operator (+, -, *, /): ");
op = (char)Console.Read();
switch(op)
{
case '+': result = first + second; Console.WriteLine("{0} + {1} = {2}", first, second, result);
break;
case '-': result = first - second; Console.WriteLine("{0} - {1} = {2}", first, second, result);
break;
case '*': result = first * second; Console.WriteLine("{0} * {1} = {2}", first, second, result);
break;
case '/': result = first / second; Console.WriteLine("{0} / {1} = {2}", first, second, result);
break;
default: Console.WriteLine("Invalid Operator");
break;

} } } }
- Sixth Example:
using System;
public class Program
{
public static void Main()
{
int day;
Console.WriteLine("Enter the week day number : ");
day = Convert.ToInt32(Console.ReadLine());

switch(day)
{
case 1: Console.WriteLine("Monday");
break;
case 2: Console.WriteLine("Tuesday");
break;
case 3: Console.WriteLine("Wednesday");
break;
case 4: Console.WriteLine("Thursday");
break;
case 5: Console.WriteLine("Friday");
break;
case 6: Console.WriteLine("Saturday");
break;
case 7: Console.WriteLine("Sunday");
break;
default: Console.WriteLine("Invalid input");
break;
} } }
- Seventh Example:
using System;
public class Exercise22
{
public static void Main()
{
int monno;
Console.Write("Input Month No : ");
monno = Convert.ToInt32(Console.ReadLine());

switch(monno)
{
case 1: Console.Write("January\n");
break;
case 2: Console.Write("February\n");
break;
case 3: Console.Write("March\n");
break;
case 4: Console.Write("April\n");
break;
case 5: Console.Write("May\n");
break;
case 6: Console.Write("June\n");
break;
case 7: Console.Write("July\n");
break;
case 8: Console.Write("August\n");
break;
case 9: Console.Write("September\n");
break;
case 10: Console.Write("October\n");
break;
case 11: Console.Write("November\n");
break;
case 12: Console.Write("December\n");
break;
default: Console.Write("invalid Month number. \nPlease try again ....\n");
break;
} } }

You might also like