以下代码的输出有什么不同 [英] what is the difference in output for the below codes

查看:88
本文介绍了以下代码的输出有什么不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2个代码有什么区别?

int a = 2;

开关(a)

{

案例1:printf(1);

案例2:printf(2);

...

}

int a = 2;

开关(a)

{

案例1:printf (1);

休息;

案例2:printf(2);

休息;

...

}

what is the difference b/w the 2 codes?
int a=2;
switch(a)
{
case 1:printf("1");
case 2:printf("2");
...
}
int a=2;
switch(a)
{
case 1:printf("1");
break;
case 2:printf("2");
break;
...
}

推荐答案

在switch语句中,将执行所选案例和任何后续案例的代码,直到遇到break语句。换句话说,第一个代码将执行案例1和案例2的代码,而第二个代码块将执行案例1的代码然后退出,或者案例2然后退出。



这两种情况的样本:



In a switch statement the code for the selected case and any subsequent cases will be executed until the "break" statement is encountered. In other words, the first code will execute the code for case 1 as well as case 2, whereas the second code block will execute code for case 1 and then exit, or for case 2 and then exit.

Sample for both those cases:

int a = 1;

switch (a)
{
  case 1:
    // Some code for case 1
  case 2:
    // Some code for case 1 and 2
    break;
  case 3:
    // Code only for case 3
    break;
  default:
    // For all other cases
    break;
}


这篇关于以下代码的输出有什么不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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