切换语句 [英] Switch Statements

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

问题描述

对于switch语句,是否可以更改switch语句内部的switch值,以便它可以跳转到不同的情况? 例如:

For switch statements, is it possible to change the value of the switch inside the switch statement so that it can jump around to different cases? Ex:

int w = 0;
switch(w)
{
   case 1:
     doSomething();
     w = 3;
   case 2:
     doSomething();
     break;
   case 3:
     doSomething();
     break;
}

基本上,我要问的是,如果我不为一个案例放置一个break语句,而在同一案例中更改switch的值,那么代码会同时执行这两种情况吗?

Basically what I'm asking is, if I do not place a break statement for a case and I change the value of the switch in the same case, will the code execute both cases?

推荐答案

是的,您可以在switch内更改值,但是直到您更改值的情况中断后,它才会对新值执行大小写操作.

Yes you can change the value inside switch but it will not execute case for new value until you break in the case where you changed the value.

在您的情况下,无论如何它都不会消失,因为没有0的情况.但是,如果更改为w = 1,那么它将在情况1和情况2都通过,因为您没有中断;但在情况3中不适用.

In your case it will not go in any case as there is no case for 0. But if you change to w = 1 then it will go for case 1 and then for case 2 as you do not have break; but it will not go for case 3.

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

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