C language/C++ [Detailed explanation of switch statement (usage, rules, flow chart, example)]

C++| Ingenious work from 0 to 1 introductory programming [video + courseware + notes + source code]

table of Contents

1. Switch statement [C language]

1.2, the syntax of the switch statement in C language

1.3, switch statements follow the rules

1.3, switch flow chart

1.4, switch instance

2. Switch statement [C++]

2.1, role

2.2, grammar

2.3. Example

2.4. Precautions and summary


1. Switch statement [C language]

Novice tutorial C language switch statement: https://www.runoob.com/cprogramming/c-switch.html

switch  statement allows testing when a variable is equal to multiple values. Each value is called a case, and the variable being tested will  be checked for each  switch case .

1.2, the syntax of the switch statement in C language 

switch (expression) {
    case constant-expression :
       statement(s);
       break; /* 可选的 */
    case constant-expression :
       statement(s);
       break; /* 可选的 */
    /* 您可以有任意数量的 case 语句 */
    default : /* 可选的 */
       statement(s);
}

1.3, switch statements follow the rules

The switch statement must follow the following rules:

  •  The expression in the switch statement   is a constant expression and must be an integer or enumerated type .
  • There can be any number of case statements in a switch. Each case is followed by a value to be compared and a colon.
  • The constant-expression of the case   must have the same data type as the variable in the switch, and it must be a constant or a literal.
  • When the tested variable is equal to the constant in the case, the statement following the case will be executed until the break  statement is encountered  .
  • When a break  statement is encountered  , the switch is terminated and the control flow will jump to the next line after the switch statement.
  • Not every case needs to include  break . If the case statement does not contain  break , the control flow will  continue with  subsequent cases until break is encountered.
  • switch  statement can have an optional  default  case, which appears at the end of the switch. The default case can be used to perform a task when none of the above cases are true. The break  statement in the default case is  not necessary.

1.3, switch flow chart

Switch statement in C

1.4, switch instance

#include <stdio.h>

int main()
{
    char grade = 'B'; /* 局部变量定义 */
    switch (grade)
    {
        case 'A':
            printf("很棒!\n");
            break;
        case 'B':
        case 'C':
            printf("做得好!\n");
            break;
        case 'D':
            printf("您通过了!\n");
            break;
        case 'F':
            printf("最好再试一下!\n");
            break;
        default:
            printf("无效的成绩!\n");
    }
    printf("您的成绩是 %c!\n", grade);
    return 0;
}

2. Switch statement [C++]

2.1, role

Execute multiple conditional branch statements.

2.2, grammar

switch (表达式) //【switch语句中表达式类型只能是整型或者字符型】
{
    case 结果1: 执行语句;break;
    case 结果2: 执行语句;break;
    ...
    default: 执行语句;break;
}

2.3. Example

#include <iostream>
using namespace std;

int main() //switch语句
{
	//请给电影进行评分
	// 10 ~ 9   经典
	// 8 ~ 7   非常好
	// 6 ~ 5   一般
	// 5分以下 烂片

	//1、提示用户给电影评分
	cout << "请给电影打分:" << endl;

	//2、用户开始进行打分
	int score = 0;
	cin >> score;
	cout << "您打的分数为:" << score << endl;

	//3、根据用户输入的分数来提示用户最后的结果
	switch (score)
	{
	case 10:
		// cout << "您认为是经典电影!" << endl;
		// break; //退出当前分支
	case 9:
		cout << "您认为是经典电影!" << endl;
		break; //退出当前分支
	case 8:
		// cout << "您认为电影非常好!" << endl;
		// break;
	case 7:
		cout << "您认为电影非常好!" << endl;
		break;
	case 6:
	case 5:
		cout << "您认为电影一般!" << endl;
		break;
	default:
		cout << "您认为是烂片!" << endl;
		break;
	}

	//if和switch区别?
	//switch缺点:判断时候只能是整型或者字符型,不可以是一个区间!
	//switch优点:结构清晰,执行效率高!

	system("pause");

	return 0;
}

2.4. Precautions and summary

  • Note 1: The expression type in the switch statement can only be integer or character type .
  • Note 2: If there is no break in the case, the program will be executed all the way down.
  • Summary: Compared with the if statement, for multi-condition judgment, the structure of switch is clear and the execution efficiency is high. The disadvantage is that the switch cannot judge the interval.

Guess you like

Origin blog.csdn.net/weixin_44949135/article/details/115174682