c++语句switch语句_C中的switch语句

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)

  1. 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关键字之后)必须产生一个数值,即该表达式应该是整数或变量或计算结果为整数的表达式。

  2. The case label values must be unique.

    案例标签值必须唯一。

  3. The case label must end with a colon(:)

    案例标签必须以冒号(:)结尾

  4. The next line, after the case statement, can be any valid C statement.

    case语句之后的下一行可以是任何有效的C语句。

要记住的要点 (Points to Remember)

  1. We don't use those expressions to evaluate switch case, which may return floating point values or strings or characters.

    我们不使用这些表达式来评估开关的大小写,这可能会返回浮点值或字符串或字符。

  2. break statements are used to exit the switch block. It isn't necessary to use break 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 a break statement in encountered or the execution reaches the end of the switch block.

    因为只有第一个大小写匹配,所以输出只能是A ,但是由于该块之后没有break语句,因此也执行下一个块,直到遇到遇到break语句或执行到达switch块的结尾为止。

  3. default case is executed when none of the mentioned case matches the switch expression. The default case can be placed anywhere in the switch case. Even if we don't include the default case, switch statement works.

    当上述任何一种情况都不符合switch表达式时,将执行default case。 默认案例可以放在switch案例中的任何位置。 即使我们不包括默认情况, switch语句也可以工作。

  4. Nesting of switch statements are allowed, which means you can have switch statements inside another switch block. However, nested switch 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");
        }
    } 
}

switchif之间的区别 (Difference between switch and if)

  • if statements can evaluate float conditions. switch statements cannot evaluate float conditions.

    if语句可以评估float条件。 switch语句无法评估float条件。

  • if statement can evaluate relational operators. switch statement cannot evaluate relational operators i.e they are not allowed in switch statement.

    if语句可以评估关系运算符。 switch语句无法评估关系运算符,即switch语句中不允许使用它们。

翻译自: https://www.studytonight.com/c/switch-statement-in-c.php

c++语句switch语句

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值