CSwitch.md - brainchildservices/curriculum GitHub Wiki
SLIDE-1
C# Switch Statements
In C#, Switch statement is a multiway branch statement. It provides an efficient way to transfer the execution to different parts of a code based on the value of the expression. The switch expression is of integer type such as int, char, byte, or short, or of an enumeration type, or of string type. The expression is checked for different cases and the one match is executed.
SLIDE-2
Why do we use Switch Statements instead of if-else statements?
-
We use a switch statement instead of if-else statements because if-else statement only works for a small number of logical evaluations of a value. If you use if-else statement for a larger number of possible conditions then, it takes more time to write and also become difficult to read.
-
break statement is used to exit from the switch statement.
-
goto statement can be used to go to a different case of the switch statement.
SLIDE-2(downwards)
SLIDE-3
Example 1: Expression with integer
https://dotnetfiddle.net/LbJpOA
using System;
public class Program
{
public static void Main()
{
int expression = 5;
switch (expression)
{
case 1: //checks if value of expression variable is 1
Console.WriteLine("1");
break; //Break and go to end of line 25
case 2: //checks if value of expression variable is 2
Console.WriteLine("2");
break;
case 3: //checks if value of expression variable is 3
Console.WriteLine("3");
break;
case 4:
Console.WriteLine("4");
break;
default:
Console.WriteLine("Not Found");
break;
}
}
}
SLIDE-4
Example 2: Expression with string
https://dotnetfiddle.net/on5aZp
using System;
public class Program
{
public static void Main()
{
string letter = "E";
switch (letter)
{
case "A": //checks if value of expression variable is A
Console.WriteLine("A");
break; //Break and go to end of line 25
case "B": //checks if value of expression variable is B
Console.WriteLine("B");
break;
case "C": //checks if value of expression variable is C
Console.WriteLine("C");
break;
case "D":
Console.WriteLine("D");
break;
default:
Console.WriteLine("Not Found");
break;
}
}
}
SLIDE-5
Example 3: Switch statement with grouped cases https://dotnetfiddle.net/8yLXIw
using System;
public class Program
{
public static void Main()
{
string letter = "C";
switch (letter)
{
case "A": //checks if value of expression variable is A
case "B": //checks if value of expression variable is B
case "C": //checks if value of expression variable is C
Console.WriteLine("A/B/C");
break;
case "D":
Console.WriteLine("D");
break;
default:
Console.WriteLine("Not Found");
break;
}
}
}
SLIDE-6
Exercise To Do:
Q> Redo Q8 to Q12 from section(Except Q11) C# Conditions and If Statements using switch statement
SLIDE-7
Q1: Write a program to take a weekday number from the user and accordingly print the day of the week.
Test Data :
Enter a weekday number: 5
Expected Output :
Its Thursday.
Q2: Write a program to take a month number from the user and accordingly print the month of the year.
Test Data :
Enter a month number: 3
Expected Output :
Its March.
SLIDE-8
Q3: Write a program to create calculator using switch Statement
Test Data:
Enter first integer: 10
Enter the operand (+, -, *, /): *
Enter first integer: 20
Expected Output :
10 * 20=200
Q4: Write a program to enter the month number and print the number of days in a month. (Use Switch statement with grouped cases)
Test Data:
Enter a number: 1
Expected Output :
31 days