C++ Switch Statements

manzura_kerimova

Kerimova_Manzura

Posted on April 6, 2023

C++ Switch Statements

C++ Switch Statements
Use the switch statement to select one of many code blocks to be executed.

switch(_expression_) {
  case x:
    _// code block_
    break;
  case y:
  _  // code block_
    break;
  default:
    _// code block_
}
Enter fullscreen mode Exit fullscreen mode

This is how it works:

  • The switch expression is evaluated once

  • The value of the expression is compared with the values of each case

  • If there is a match, the associated block of code is executed

  • The break and default keywords are optional, and will be described later in this chapter

The example below uses the weekday number to calculate the weekday name:

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)_
Enter fullscreen mode Exit fullscreen mode

The break Keyword

When C++ reaches a break keyword, it breaks out of the switch block.

This will stop the execution of more code and case testing inside the block.

When a match is found, and the job is done, it's time for a break. There is no need for more testing.

A break can save a lot of execution time because it "ignores" the execution of all the rest of the code in the switch block.

The default Keyword
The default keyword specifies some code to run if there is no case match:

int day = 4;
switch (day) {
  case 6:
    cout << "Today is Saturday";
    break;
  case 7:
    cout << "Today is Sunday";
    break;
  default:
    cout << "Looking forward to the Weekend";
}
_// Outputs "Looking forward to the Weekend"_
Enter fullscreen mode Exit fullscreen mode

The switch statement transfers control to one of the labeled-statements in its body, depending on the value of condition .

Condition must be of integer type or be a class type that has a one-to-one conversion to integral type. Integer promotion is performed as described in Standard Conversions.

The body of a switch statement consists of a series of case labels and an opt label ional default . The labeled-statementis one of these labels and the following statements. Labeled statements are not syntactic requirements, but a switch statement without them is meaningless. No two constant-expression values in case statements can have the same value. The default label can only appear once. The default statement is often placed at the end, but it can appear anywhere in the text of a switch statement. A case or default label can only appear inside a switch statement.

Eachconstant-expressioncase label is converted to a constant value of the same type as condition. This is then compared to condition for equality. The control is passed to the first caseconstant-expression after the value corresponding to condition. The resulting behavior is shown in the following table.

The converted value corresponds to the value of the raised control expression.
Control is transferred to the operator following this label.
None of the constants match the constants in the case labels;
the default label is present.
The control is passed to the default label.
None of the constants match the constants in the case labels;
the label is missing default .
Control is transferred to the statement after the switch statement.

If a matching expression is found, execution may continue later than case or default with labels. The break statement is used to stop execution and transfer control to the statement after the switch statement. break Without a statement, every statement from the matched case label to the end of the switch is executed, including default.

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

💖 💪 🙅 🚩
manzura_kerimova
Kerimova_Manzura

Posted on April 6, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related