c++语句switch语句
When you want to solve multiple option type problems, for example: Menu like program, where one value is associated with each option and you need to choose only one at a time, then, switch
statement is used.
当您要解决多个选项类型问题时,例如:菜单之类的程序,其中一个值与每个选项相关联,并且您一次只需要选择一个,然后使用switch
语句。
Switch statement is a control statement that allows us to choose only one choice among the many given choices. The expression in switch
evaluates to return an integral value, which is then compared to the values present in different cases. It executes that block of code which matches the case value. If there is no match, then default block is executed(if present). The general form of switch
statement is,
Switch语句是一种控制语句,它使我们只能在许多给定选项中选择一个。 switch
的表达式求值返回一个整数值,然后将其与不同情况下的值进行比较。 它执行与大小写值匹配的代码块。 如果不匹配,则执行默认块(如果存在)。 switch
语句的一般形式是:
switch(expression)
{
case value-1:
block-1;
break;
case value-2:
block-2;
break;
case value-3:
block-3;
break;
case value-4:
block-4;
break;
default:
default-block;
break;
}
使用switch
语句的规则 (Rules for using switch
statement)
The expression (after switch keyword) must yield an integer value i.e the expression should be an integer or a variable or an expression that evaluates to an integer.
表达式(在switch关键字之后)必须产生一个整数值,即该表达式应该是整数或变量或计算结果为整数的表达式。
The case label values must be unique.
案例标签值必须唯一。
The case label must end with a colon(:)
案例标签必须以冒号(:)结尾
The next line, after the case statement, can be any valid C statement.
case语句之后的下一行可以是任何有效的C语句。
要记住的要点 (Points to Remember)
We don't use those expressions to evaluate switch case, which may return floating point values or strings or characters.
我们不使用这些表达式来评估开关的大小写,这可能会返回浮点值或字符串或字符。
break
statements are used to exit the switch block. It isn't necessary to usebreak
after each block, but if you do not use it, then all the consecutive blocks of code will get executed after the matching block.break
语句用于退出切换块。 不必在每个块之后使用break
,但是如果不使用break
,则所有连续的代码块都将在匹配的块之后执行。int i = 1; switch(i) { case 1: printf("A"); // No break case 2: printf("B"); // No break case 3: printf("C"); break; }
A B C
美国广播公司
The output was supposed to be only A because only the first case matches, but as there is no
break
statement after that block, the next blocks are executed too, until it abreak
statement in encountered or the execution reaches the end of theswitch
block.因为只有第一个大小写匹配,所以输出只能是A ,但是由于该块之后没有
break
语句,因此也执行下一个块,直到遇到遇到break
语句或执行到达switch
块的结尾为止。default case is executed when none of the mentioned case matches the
switch
expression. The default case can be placed anywhere in theswitch
case. Even if we don't include the default case,switch
statement works.当上述任何一种情况都不符合
switch
表达式时,将执行default case。 默认案例可以放在switch
案例中的任何位置。 即使我们不包括默认情况,switch
语句也可以工作。Nesting of
switch
statements are allowed, which means you can haveswitch
statements inside anotherswitch
block. However, nestedswitch
statements should be avoided as it makes the program more complex and less readable.允许嵌套
switch
语句,这意味着您可以在另一个switch
块中包含switch
语句。 但是,应避免使用嵌套的switch
语句,因为它会使程序更复杂且可读性更差。
switch
语句示例 (Example of switch
statement)
#include<stdio.h>
void main( )
{
int a, b, c, choice;
while(choice != 3)
{
/* Printing the available options */
printf("\n 1. Press 1 for addition");
printf("\n 2. Press 2 for subtraction");
printf("\n Enter your choice");
/* Taking users input */
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter 2 numbers");
scanf("%d%d", &a, &b);
c = a + b;
printf("%d", c);
break;
case 2:
printf("Enter 2 numbers");
scanf("%d%d", &a, &b);
c = a - b;
printf("%d", c);
break;
default:
printf("you have passed a wrong key");
printf("\n press any key to continue");
}
}
}
switch
和if
之间的区别 (Difference between switch
and if
)
if
statements can evaluatefloat
conditions.switch
statements cannot evaluatefloat
conditions.if
语句可以评估float
条件。switch
语句无法评估float
条件。if
statement can evaluate relational operators.switch
statement cannot evaluate relational operators i.e they are not allowed inswitch
statement.if
语句可以评估关系运算符。switch
语句无法评估关系运算符,即switch
语句中不允许使用它们。
翻译自: https://www.studytonight.com/c/switch-statement-in-c.php
c++语句switch语句