嵌套案例语句 [英] Nested case statements

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

问题描述

谁能解释一下 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 标签,switch 主体中的语句会像任何其他范围 { } 一样依次执行,除非 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("
In case 10");
        case 11:
              printf("
In case 11");              
        case 12:
              printf("
In case 12");
              break;
        case 5:
              printf("
In case 5");
              break;
        case 6:
              printf("
In case 6");     
        default:
              printf("
In Default");

    }

    return 0;
}

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

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

5   
6
10

结果输出如下:
场景一: 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.

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

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