嵌套 case 语句 [英] Nested case statements

查看:38
本文介绍了嵌套 case 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释将 case 语句嵌套到另一个语句中吗?我指的是 Duffs Device,其中所有其他 case语句位于与 case 0 相关联的 do-while 循环内.我无法理解它.在我看来,它应该像一个嵌套的 if.但是我肯定错过了一些东西.请解释.

Can some one please explain the nesting of case statements into another. I'm referring to the Duffs Device where all other case statements are inside the do-while loop associated with case 0. I cant get my head around it. It seems to me that it should act like a nested if. But then i'm definitely missing something. Please explain.

推荐答案

switch-case 结构中,switch 主体只是一个普通的或复合的语句,它可以包含任何其他有效的 c 语句.它还可能包含 casedefault 标签.
并且控制根据控制表达式的值跳转到适当的 case 标签,开关体中的语句就像任何其他作用域{ } 除非一个 >break 遇到.

In a switch-case construct, the switch body is just a normal or compound statement which can contain any other valid c statements. It may also contain case or default labels.
And the control jumps to appropriate case label depending on controlling expression value, the statements in the switch body are executed one after another just like any other scope { } unless a break is encountered.

例如,考虑以下简单测试程序:

For example, consider the following simple test program:

#include<stdio.h>
int main()
{
    int i = 6;//5,10;
    switch(6)
    {
        case 10:
              printf("\nIn case 10");
        case 11:
              printf("\nIn case 11");              
        case 12:
              printf("\nIn case 12");
              break;
        case 5:
              printf("\nIn case 5");
              break;
        case 6:
              printf("\nIn case 6");     
        default:
              printf("\nIn Default");

    }

    return 0;
}

考虑 switch 语句中控制表达式 i 的 3 个值:

Consider 3 values for the controlling expression i in the switch statement:

5   
6
10

结果输出如下:
场景 1: i = 6

In case 6
In Default

场景 2: i = 10

In case 10
In case 11
In case 12

场景 3: i = 5

In case 5

请注意,在上述每个场景中,一旦遇到匹配的 case 标签,语句就会顺序执行,直到遇到 break,从而得出以下结论是答案中的第一个陈述.

Notice that, in each of the above scenarios, once a matching case label is encountered the statements are executed sequentially until a break is encountered, Thereby leading to conclusion which is the first statement in the answer.

这篇关于嵌套 case 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆