Skip to content

Latest commit

 

History

History
67 lines (55 loc) · 1.53 KB

switch.md

File metadata and controls

67 lines (55 loc) · 1.53 KB

Switch

The switch statement allows us to execute one code block among many alternatives.

You can do the same thing with the if...else..if ladder. However, the syntax of the switch statement is much easier to read and write.

Syntax

switch (expression)
​{
    case constant1:
      // statements
      break;

    case constant2:
      // statements
      break;
    .
    .
    .
    default:
      // default statements
}

The expression is evaluated once and compared with the values of each case label.

  • If there is a match, the corresponding statements after the matching label are executed. For example, if the value of the expression is equal to constant2, statements after case constant2: are executed until break is encountered.

  • If there is no match, the default statements are executed.

📝 If we do not use break, all statements after the matching label are executed.
☝️ By the way, the default clause inside the switch statement is optional.

Example

int day = 4;
switch (day) {
  case 1:
    cout << "Monday";
    break;
  case 2:
    cout << "Tuesday";
    break;
  case 3:
    cout << "Wednesday";
    break;
  case 4:
    cout << "Thursday";
    break;
  case 5:
    cout << "Friday";
    break;
  case 6:
    cout << "Saturday";
    break;
  case 7:
    cout << "Sunday";
    break;
}
// Outputs "Thursday" (day 4)