传递多个值切换用逗号分隔值 [英] passing multiple values to switch separating the values by commas

查看:114
本文介绍了传递多个值切换用逗号分隔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以传递多个值开关case语句用逗号分隔不同的值?什么会前任pression评估为?如果没有错误,其中多个值将编译器评估与交换机?牢记逗号运算符的低precedence?例如说我写的code如下:

  INT M = 10;
开关(间,间* 2)
{
     案例10:输出(\\ n案件10);
     案例20:输出(\\ n案件20);
     案例30:输出(\\ n案件30);
     默认:输出(\\ n任何情况下);
 }


解决方案

逗号运算符的所有前pressions并返回最右边的值。 (它不会使您使用它在开关任何区别。)

您的例子是完全一样的开关(M * 2),但如果任何preceding前pressions的有副作用,这些侧效果会被执行语句开关之前被应用。

Can I pass multiple values to the switch case statement separating different values by commas ? what would the expression evaluate to? If no error occurs, which of the multiple values would the compiler evaluate the switch with ? Keeping in mind the low precedence of the comma operator? say for instance i write the code as follows:

int m=10;
switch(m, m*2)
{
     case 10: printf("\n case 10");
     case 20: printf("\n case 20");
     case 30: printf("\n case 30");
     default: printf("\n no case");
 }

解决方案

The comma operator evaluates all the expressions and returns the value of the rightmost one. (It doesn't make any difference that you're using it in a switch.)

Your example is exactly the same as switch (m*2), but if any of the preceding expressions had side effects, those side effect would be applied before the switch statement was executed.

这篇关于传递多个值切换用逗号分隔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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