开关,外壳,以及它有时如何让你跌倒 [英] switch, case, and how it sometimes lets you fallthrough
本文介绍了开关,外壳,以及它有时如何让你跌倒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很困惑如何在开关中限制穿透。例如
以下作品:
string switch_test =" a" ;;
开关(switch_test)
{
case" a":
case" b":
case" c":
doSomething(a);
休息;
}
但以下不起作用:
string switch_test =" a" ;;
switch(switch_test)
{
case" a":
if(< some test>)
break;
case" b":
case" c":
doSomething(a);
}
为什么会这样?
I''m confused as to how fallthrough is limited in switch. For example
the following works:
string switch_test = "a";
switch (switch_test)
{
case "a":
case "b":
case "c":
doSomething(a);
break;
}
but the following does not work:
string switch_test = "a";
switch (switch_test)
{
case "a":
if (<some test>)
break;
case "b":
case "c":
doSomething(a);
}
why is that?
推荐答案
案例必须为空,或通过''jump''语句退出(中断,继续,
返回等)。
如果你只是在dosomething(a)行之后包含一个跳转(通常是'break'')
,那么第二个例子也会有用,并确保跳转到达在
第一种情况下(目前,只有在if测试为真的情况下才会达到)。
-
David Anton
www.tangiblesoftwaresolutions.com
即时C#:VB .NET到C#转换器
即时VB:C#到VB.NET转换器
即时C ++:C#到C ++转换器
即时J#:VB。 NET转J#转换器
" Benny Raymond"写道:
A case must either be empty, or exit via a ''jump'' statement (break, continue,
return, etc.).
The second example will work if you just include a jump (usually ''break'')
after the dosomething(a) line and also ensure that a jump is reached in the
first case (currently, it''s only reached if the if test is true).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter
"Benny Raymond" wrote:
我很困惑如何在开关中限制穿透。例如
以下工作:
string switch_test =" a" ;;
switch(switch_test)
{
case" a":
case" b":
case" c":
doSomething(a);
break;
}
但以下不起作用:
string switch_test =" a" ;;
switch(switch_test)
{
case" a":
if(< some test> )
休息;
案例" b":
案例" c":
doSomething(a);
}
为什么是吗?
I''m confused as to how fallthrough is limited in switch. For example
the following works:
string switch_test = "a";
switch (switch_test)
{
case "a":
case "b":
case "c":
doSomething(a);
break;
}
but the following does not work:
string switch_test = "a";
switch (switch_test)
{
case "a":
if (<some test>)
break;
case "b":
case "c":
doSomething(a);
}
why is that?
这不是解决这个问题,但我想指出你的陈述
也应该有一个''默认''开关,以尽量减少未处理的错误。
像这样:
string switch_test =" a";
开关(switch_test)
{
case" a":
{
if(this)
休息;
其他
do_somthing_nice(switch_test);
休息;
}
case" b":
case" c:"
{
do_something_naughty(switch_test);
休息;
}
默认值:
{
whine_about(switch_test);
休息;
}
}
" Benny Raymond"写道:
This isn''t a fix for this, but I wanted to point out that your statement
should also have a ''default'' switch to minimize unhandled errors.
like this:
string switch_test = "a";
switch(switch_test)
{
case "a":
{
if (this)
break;
else
do_somthing_nice(switch_test);
break;
}
case "b":
case "c:"
{
do_something_naughty(switch_test);
break;
}
default:
{
whine_about(switch_test);
break;
}
}
"Benny Raymond" wrote:
我很困惑如何在开关中限制穿透。例如
以下工作:
string switch_test =" a" ;;
switch(switch_test)
{
case" a":
case" b":
case" c":
doSomething(a);
break;
}
但以下不起作用:
string switch_test =" a" ;;
switch(switch_test)
{
case" a":
if(< some test> )
休息;
案例" b":
案例" c":
doSomething(a);
}
为什么是吗?
I''m confused as to how fallthrough is limited in switch. For example
the following works:
string switch_test = "a";
switch (switch_test)
{
case "a":
case "b":
case "c":
doSomething(a);
break;
}
but the following does not work:
string switch_test = "a";
switch (switch_test)
{
case "a":
if (<some test>)
break;
case "b":
case "c":
doSomething(a);
}
why is that?
我知道......我只是想知道如何只打破a如果
什么 - 否则会掉线。
Kevin Kitching写道:
I know... I just was mainly wondering about how to only break on "a" if
something - otherwise fallthough.
Kevin Kitching wrote:
这不是解决这个问题,但我想要指出你的陈述
也应该有一个''默认''开关,以尽量减少未处理的错误。
这样:
string switch_test =" a" ;;
开关(switch_test)
{
案例a:
{
if(this)
break;
其他
do_somthing_nice(switch_test);
休息;
}
案例" b":
案例" c:"
{
do_something_naughty(switch_test);
休息;
}
默认:
{
whine_about(switch_test);
休息;
}
}
Benny Raymond写道:
This isn''t a fix for this, but I wanted to point out that your statement
should also have a ''default'' switch to minimize unhandled errors.
like this:
string switch_test = "a";
switch(switch_test)
{
case "a":
{
if (this)
break;
else
do_somthing_nice(switch_test);
break;
}
case "b":
case "c:"
{
do_something_naughty(switch_test);
break;
}
default:
{
whine_about(switch_test);
break;
}
}
"Benny Raymond" wrote:
我很困惑如何在开关中限制穿透。例如
以下工作:
string switch_test =" a" ;;
switch(switch_test)
{
case" a":
case" b":
case" c":
doSomething(a);
break;
}
但以下不起作用:
string switch_test =" a" ;;
switch(switch_test)
{
case" a":
if(< some test> )
休息;
案例" b":
案例" c":
doSomething(a);
}
为什么是吗?
I''m confused as to how fallthrough is limited in switch. For example
the following works:
string switch_test = "a";
switch (switch_test)
{
case "a":
case "b":
case "c":
doSomething(a);
break;
}
but the following does not work:
string switch_test = "a";
switch (switch_test)
{
case "a":
if (<some test>)
break;
case "b":
case "c":
doSomething(a);
}
why is that?
这篇关于开关,外壳,以及它有时如何让你跌倒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文