Switch Statement

Using Switch Case

If a large number of status checks are to be made, more understandable codes can be written using this structure. However, only equality check can be done with switch statement.

Unlike the If Else structure, comparisons such as greater than or less than (<, <=, >, >=, !=) cannot be made.

Its usage is as follows. After the switch statement, the variable or object to be controlled is written in normal parentheses.

Case means situation. What the controlled variable will be equal to is  written after the case. After the command lines to be made in case of equality, that state is ended with the break command and the new state is started.

In case none of the specified conditions are fulfilled, the actions to be taken are written in the default part. (Like the else part in the if structure)

switch(your grade)
{
case 1:
label1.Text = "You got a bad grade.";
break;
case 2:
label1.Text = "You have received a passing grade.";
break;
case 3:
label1.Text = "You got a middle grade.";
break;
case 4:
label1.Text = "Good grade.";
break;
case 5:
label1.Text = "Well noted.";
break;
default:
label1.Text ="Incorrect Entry!";
break;
}

***Every situation starts with a case and ends with a break. Do not forget to put : at the end of the case line.

using switch case in c#, switch case statement, how to use switch case, switch case tutorials

EXERCISES

C# Switch Case Tutorial

In a ComboBox, the expressions Ankara, Istanbul, Izmir, Çanakkale, Kocaeli, Tekirdağ should be included, respectively. Let the license plate code of the city chosen by the user be written in label1:

switch(comboBox1.SelectedIndex)

{

case 0:

label1.Text="06";

break;

case 1:

label1.Text="34";

break;

case 2:

label1.Text="35";

break;

case 3:

label1.Text="17";

break;

case 4:

label1.Text="41";

break;

case 5:

label1.Text="59";

break;

default:

label1.Text="Seçim Yapmadınız!";

break;

}

 



COMMENTS




Read 575 times.

Online Users: 207



switch-case-statement