-
Key Features and
Create a new project in Visual Studio 2022 - Naming Standards & Comments
- Data Types
- Variables and Constant Variable
- Conditional Statement
- Switch Conditional Statement
- Arithmetic, Comparison, Assignment and Logical Operators
- For, While and Do While Loops
- Importance of Break and Continue Keywords
- Strings and Important String Methods
- Single Dimensional Array
- Multi Dimensional Array
- Methods and Method Overloading
- Math Class and Functions
- Casting and Type Conversions
- Class and Objects
- Constructor, Parameter less and Parameterized Constructor
- Single, Multi level and Hierarchical Inheritance
- Compile Time Polymorphism, Method and Operator Overloading
- Run-time Polymorphism and Importance of base keyword
- Abstract Class and Method
- Interface and Multiple Inheritance through Interfaces
Agenda
- C# Switch Conditional Statement
Switch Conditional Statement in C#
- A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.
- User can use a switch instead of if-else statement because if-else statement only works for a small number of logical evaluations of a value. In case user wants to use if-else statement for a larger number of possible conditions then, it takes more time to write and also become difficult to read.
- The switch statement works with char, short, int, long, string data types.
- The case values must be unique. In case of duplicate value, it will throw compile-time error.
- Not every case needs to contain a break. In case of no break statement appears, then it will raise a compile time error.
- A switch statement can have an optional default case, which must appear at the end of the switch and specifies some code to run if there is no case match. Multiple default statements are not allowed.
Syntax:
case value:
// code block
break;
case value:
// code block
break;.
.
.
default:
// code block
break;
}
Switch Statement Example with char Data Type
char grade = ‘A’;
switch (grade)
{
case ‘A’:
Console.WriteLine(“Excellent”);
break;
case ‘B’:
Console.WriteLine(“Very Good”);
break;
case ‘C’:
Console.WriteLine(“Good”);
break;
case ‘D’:
Console.WriteLine(“Average”);
break;
default:
Console.WriteLine(“Invalid Grade”);
break;
}
int studentclass = 2;
switch (studentclass)
{
case 1:
Console.WriteLine("Student belong to Primary standard I");
break;
case 2:
Console.WriteLine("Student belong to Primary standard II");
break;
case 3:
Console.WriteLine("Student belong to Primary standard III");
break;
case 4:
Console.WriteLine("Student belong to Primary standard IV");
break;
default:
Console.WriteLine("Student does not belong to Primary Standard");
break;
}
char grade = 'A';
switch (grade)
{
case 'A':
Console.WriteLine("Excellent");
break;
case 'B':
Console.WriteLine("Very Good");
break;
case 'C':
Console.WriteLine("Good");
break;
case 'D':
Console.WriteLine("Average");
break;
default:
Console.WriteLine("Invalid Grade");
break;
}