Decision Making Statements – Switch statement

October18, 2017
by admin

Syntax:


  switch(<expression>)
  {
    case <value 1>:
      // statement or statements
      break;
    case <value 2>:
      // statement or statements
      break;
      .............................
      .............................
    case <value n>:
      // statement or statements
      break;
    default:
      // default statement or statements
      break;
  }

As you see in the syntax, to use switch statement, we have to use the ’switch’ keyword. Switch statement has an expression which may be a variable or an expression which returns a single value of any primitive type.

Switch statement can have several sections enclosed within the curly braces. Each section has a keyword ’case’ with a possible value that may be returned by the expression. Every case or section has its own code statement or statements.

After each case or section, we have to write a ’break’ keyword before starting next case. You can write a number of case sections within a switch control statement.

Once you have all possible cases or sections, there is a default section with ’default’ keyword with no value at the end of the switch statement. That’s because the value returned by the switch expression is going to compare with the value of each section and if the match is found then the code statements of that section will be executed. If any of the cases of sections value doesn’t match with the expression value, then the statements of default section will be executed.

Note that after each case statement there is a ’break’ keyword. This is absolutely necessary. If you forget the break keyword, then after that case, control will not be jumping out of the switch statement and continue to check next cases and if no cases are there then default statements will execute which will result in incorrect output. So don’ forget to write a break after each case statement.

Let’s look at a simple switch statement example.

Ex:

Problem:

Write a program to convert a numeric weekday to character weekday. For ex: if the user enters 1 then weekday is Sunday. If a numeric weekday is 2, then output should be Monday .. so on.

Solution:


  int numWeekDay;
  string stringWeekDay;

  System.Console.WriteLine("Enter numeric weekday [1 to 7]: ");
  numWeekDay = Convert.ToInt32(System.Console.ReadLine());

  switch(numWeekDay)
  {
    case 1:
      stringWeekDay = "Sunday";
      break;
    case 2:
      stringWeekDay = "Monday";
      break;
    case 3:
      stringWeekDay = "Tuesday";
      break;
    case 4:
      stringWeekDay = "Wednesday";
      break;
    case 5:
      stringWeekDay = "Thursday";
      break;
    case 6:
      stringWeekDay = "Friday";
      break;
    case 7:
      stringWeekDay = "Saturday";
      break;
    default:
      stringWeekDay = "Invalid weekday";
      break;
  }

  System.Console.WriteLine(string.Format("Character week day = {0}", stringWeekDay));

  System.Console.WriteLine("Press any key to exit");
  System.Console.ReadKey();

Output 1:

Enter numeric weekday [1 to 7]: 2
Character weekday = Monday

Output 2:

Enter numeric weekday [1 to 7]: 9
Character week day = Invalid weekday

In the example, we have written code to convert numeric weekday to character weekday and show the output to the console window.

We have started the code by declaring two variables ’numWeekDay’and ’stringWeekDay ’ for storing user input as an integer and output as a string.

Next, we have captured the user’s input for numeric weekday using ReadLine() method and store the value in ’numWeekDay’ variable.

Next step is to check the input value and find the appropriate character weekday. For that, we have used switch statement since we need to check only one expression (in this case value of ’numWeekDay’ variable).

Then we have written case sections for each weekday and stores the corresponding character weekday in variable ’stringWeekDay’.

After all cases, we have written default section in case if the user would not provide a value between 1 to 7 and stores ’Invalid weekday’ message to inform the user that the input provided in invalid.

Once we get the correct character weekday then we have written the code to display the character weekday to the console output. Finally, we asked the user to Press any key to exit and close the app.

Notice that we have used the ‘break’ statement within the each case after its code statements. If we forget this, then we may get the unexpected output.

C# Fundamentals