Home > Backend Development > C++ > Usage of switch case in c++

Usage of switch case in c++

下次还敢
Release: 2024-05-06 19:27:18
Original
373 people have browsed it

In C, the switch-case statement is used to execute different blocks of code based on the value of an expression: 1. The expression can be an integer, character, or enumeration constant. 2. The case branch specifies the value of the expression to be matched. There can be multiple cases matching the same value. 3. Each case branch is followed by a block of code that needs to be executed, ending with a break statement. 4. The default branch is used to match values ​​that are not specified in the expression. If there is no matching case, this code block is executed.

Usage of switch case in c++

Usage of switch-case statement in C

Question: How to use switch-case statement in C Use switch-case statements?

Answer:

A switch-case statement is a conditional statement that is used to execute different blocks of code based on the value of an expression. The syntax is as follows:

<code class="cpp">switch (expression) {
  case value1:
    // 执行代码块 1
    break;
  case value2:
    // 执行代码块 2
    break;
  // ...
  default:
    // 可选的默认 case,如果没有匹配的 case,则执行此代码块
    break;
}</code>
Copy after login

Usage:

  1. Expression: The expression of the switch statement can be any integer, character or enumeration Take constants.
  2. case value: Each case branch specifies a value to match the expression. There can be multiple case branches matching the same value.
  3. Code block: After each case branch is the code block that needs to be executed.
  4. break statement: Each case branch ends with a break statement, which will cause the control flow to jump out of the switch statement. If there is no break statement, execution will continue with subsequent case branches.
  5. default branch: The default branch is optional and is used to match values ​​not specified in the expression. If there is no matching case, this code block is executed.

Example:

The following code uses a switch-case statement to perform different actions based on the characters entered by the user:

<code class="cpp">char ch;
cout << "输入一个字符:";
cin >> ch;

switch (ch) {
  case 'a': case 'A':
    cout << "您输入的是大写或小写的 A" << endl;
    break;
  case 'b': case 'B':
    cout << "您输入的是大写或小写的 B" << endl;
    break;
  default:
    cout << "您输入的不是 A 或 B" << endl;
}</code>
Copy after login

The above is the detailed content of Usage of switch case in c++. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
c++
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template