Home  >  Article  >  Backend Development  >  How to use "switch case" statement in C language?

How to use "switch case" statement in C language?

烟雨青岚
烟雨青岚Original
2020-07-02 14:23:2934761browse

The usage of the "switch case" statement in the C language is to determine whether the expression after the case matches the expression after the switch. The syntax is "switch (variable expression) {case constant 1: statement; break;}".

How to use

The usage of the "switch case" statement in the C language is to determine whether the expression after the case matches the expression after the switch. Once the case If it matches, the following program code will be executed sequentially, regardless of whether the following cases match, until break is encountered.

Recommended video tutorial: "C Language Tutorial"

The syntax of the switch statement is as follows (switch, case, break and default are keywords):

switch ( 变量表达式 )
{
case 常量1 :语句;break;
case 常量2 :语句;break;
case 常量3 :语句;break;
...
case 常量n:语句;break;
default :语句;break;
}

When the amount expressed by the constant expression matches the constant in one of the case statements, the statements following the case statement are executed, and in sequence Continue to execute the statements in all subsequent case statements, unless a break; statement is encountered and the switch statement is jumped out. If the value of the constant expression does not match the constants of all case statements, the statements in the default statement are executed and the switch statement is jumped out.

How to use switch case statement in C language?

How to use switch case statement in C language?

Recommended tutorial: "C Language"

The above is the detailed content of How to use "switch case" statement in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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