如何在嵌套的Switch语句中退出For-Next? [英] How to exit a For-Next in a nested Switch statement?

查看:114
本文介绍了如何在嵌套的Switch语句中退出For-Next?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




回归基础!


我的理解是,提前退出For-Next循环的唯一方法是

带有''break''关键字。如果你在里面,你应该如何做?

一个Switch语句? break关键字只会出现在交换机中,而不是
for-next循环。

for(int i = 0; i< = myArray.Length; i ++)

{

if(myArray [i] == true)

switch(i)

{

案例0:

...做某事

退出for循环;


案例1:

...做些什么

退出for循环;


案例2:

。 ..做某事

退出for循环;

}

}

干杯,

PeterZ

解决方案

我认为没有内置的语言支持。再一次,你有一个解决方法 - 使用Label和goto语句。 :-)


" PeterZ"写道:



回到基础!

我的理解是退出For-Next循环的唯一方法过早地使用''break''关键字。如果你在里面转换声明,你应该如何做到这一点? break关键字只会来自开关,而不是for-next循环。

for(int i = 0; i< = myArray.Length; i ++)
{
if(myArray [i] == true)
切换(i)
{
案例0:
...做某事
退出for loop;

案例1:
...做某事
退出for循环;

案例2:
...做一些
退出for循环;
}
}
干杯,
PeterZ



>我的理解是,提前退出For-Next循环的唯一方法是使用''break''关键字

。如果你在里面转换声明,你应该如何做到这一点? break关键字只会来自开关,而不是for-next循环。

for(int i = 0; i< = myArray.Length; i ++)
{
if(myArray [i] == true)
切换(i)
{
案例0:
...做某事
退出for loop;

案例1:
...做某事
退出for循环;

案例2:
...做一些
退出for循环;
}
}




彼得,

在你的情况下如上所述,我相信以下(警告 - 未经测试的代码!)将工作(请注意if条款周围添加的大括号):


for (int i = 0; i< = myArray.Length; i ++)

{

if(myArray [i] == true)

{

开关(i)

{

案例0:

...做些什么

休息; //退出开关


案例1:

...做些什么

休息; //退出开关

}

休息; //退出for循环

}

}


HTH,

-rick-


PeterZ写道:

我的理解是,提前退出For-Next循环的唯一方法是带有''break''关键字的br />。如果你在里面转换声明,你应该如何做到这一点? break关键字只会来自开关,
不是for-next循环。




嗯,这是一个很好的例子为什么打破并不比

转到更好。你为什么不设置旗帜:


bool endLoop = false;

for(int i = 0; i< = myArray.Length &&!endLoop; i ++){

...

switch(i){

case 0:

//无论什么

endLoop = true;

休息;

}

}


这种方式你有一个明确定义的循环,你可以在循环之后找到

为什么你完全离开它,你不会跳过程序流程

喜欢休息。


我不是说从不使用它 - 但我认为它应该只用在
$中b $ b最简单的情况,它绝对清楚会发生什么。

Oliver Sturm

-

omnibus ex nihilo ducendis sufficit unum

插入空格以防止谷歌电子邮件销毁:

MSN oliver @ sturmnet.org Jabber sturm @ amessage.de

ICQ 27142619 http://www.sturmnet.org/blog


Hi,

Back to basics!

My understanding is that the only way to exit a For-Next loop prematurely is
with the ''break'' keyword. How are you supposed to do that if you''re inside
a Switch statement? The break keyword will only come out of the switch, not
the for-next loop.
for (int i=0; i<=myArray.Length; i++)
{
if (myArray[i] == true)
switch (i)
{
case 0 :
... do somehting
exit the for loop;

case 1 :
... do somehting
exit the for loop;

case 2 :
... do somehting
exit the for loop;
}
}
Cheers,
PeterZ

解决方案

I think there is no built-in language support for doing that. Again, you have
a work-around - use a Label and goto statement. :-)

"PeterZ" wrote:

Hi,

Back to basics!

My understanding is that the only way to exit a For-Next loop prematurely is
with the ''break'' keyword. How are you supposed to do that if you''re inside
a Switch statement? The break keyword will only come out of the switch, not
the for-next loop.
for (int i=0; i<=myArray.Length; i++)
{
if (myArray[i] == true)
switch (i)
{
case 0 :
... do somehting
exit the for loop;

case 1 :
... do somehting
exit the for loop;

case 2 :
... do somehting
exit the for loop;
}
}
Cheers,
PeterZ



> My understanding is that the only way to exit a For-Next loop prematurely is

with the ''break'' keyword. How are you supposed to do that if you''re inside
a Switch statement? The break keyword will only come out of the switch, not
the for-next loop.
for (int i=0; i<=myArray.Length; i++)
{
if (myArray[i] == true)
switch (i)
{
case 0 :
... do somehting
exit the for loop;

case 1 :
... do somehting
exit the for loop;

case 2 :
... do somehting
exit the for loop;
}
}



Peter,
In your case as presented I believe the following (caveat - untested code!) will
work (note the added braces around the "if" clause):

for (int i=0; i<=myArray.Length; i++)
{
if (myArray[i] == true)
{
switch (i)
{
case 0 :
... do somehting
break; // exit the switch

case 1 :
... do somehting
break; // exit the switch
}
break; // exit the for loop
}
}

HTH,
-rick-


PeterZ wrote:

My understanding is that the only way to exit a For-Next loop prematurely
is
with the ''break'' keyword. How are you supposed to do that if you''re inside
a Switch statement? The break keyword will only come out of the switch,
not
the for-next loop.



Well, this is really one good example why break isn''t much better that
goto. Why don''t you just set a flag:

bool endLoop = false;
for (int i = 0; i <= myArray.Length && !endLoop; i++) {
...
switch(i) {
case 0:
// whatever
endLoop = true;
break;
}
}

This way you have a clearly defined loop, you can find out after the loop
why exactly you left it, and you don''t go jumping around the program flow
like break does.

I''m not saying never use it - but I do think it should be used only in the
simplest situations where it''s absolutely clear what will happen.
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog


这篇关于如何在嵌套的Switch语句中退出For-Next?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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