在循环内部的case语句中打破 [英] break inside of case- statement inside of loop

查看:69
本文介绍了在循环内部的case语句中打破的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


我有一些类似于此的代码:


----------- ---------------------------------


char数组[10] =" abcdefghij";


for(int i = 0; i< 10; i ++)

{

switch (array [i])

{

case''a'':

/ * code * /

休息;

案例''b'':

/ *代码* /

休息;

案例''c'':

/ *代码* /

休息;


默认:

/ *代码* /

休息;

}


}


--------------------------------------------


是否有可能从一个案例内部中断for循环 -

语句,还是我必须使用变通方法?

Hi!

I have some code that looks similar to this:

--------------------------------------------

char array[10] = "abcdefghij";

for (int i = 0; i < 10; i++)
{
switch (array[i])
{
case ''a'':
/* code */
break;
case ''b'':
/* code */
break;
case ''c'':
/* code */
break;

default:
/* code */
break;
}

}

--------------------------------------------

Is it possible to break out of the for loop from inside of one case-
statement, or do I have to use a workaround?

推荐答案

在文章< 11 *************** @ nntpcache01.si.eunet.at>,

Alexander Korsunsky< A. ********* @ gmail.comwrote:
In article <11***************@nntpcache01.si.eunet.at>,
Alexander Korsunsky <A.*********@gmail.comwrote:

> for(int i = 0;我< 10; i ++)
{
开关(array [i])
{
[代码剪断全部]
休息;
}

是否可以从一个case-
语句中删除for循环,或者我是否必须使用变通方法?
>for (int i = 0; i < 10; i++)
{
switch (array[i])
{
[code snipped all up]
break;
}

Is it possible to break out of the for loop from inside of one case-
statement, or do I have to use a workaround?



break只能让你走出最小的封闭开关或循环。


你可以在for中加入另一个条件:


for(i = done = 0; i< 10&&!done; i ++){

switch(array [i]){

case''a'':

done = 1;

break; //或继续马上出去

case''b'':

休息;

}

}


或者你可以使用goto:


for(i = 0; i< 10; i ++){

switch (array [i]){

case''a'':

goto done;

case''b'':

休息;

}

}

完成:


-Beej - 凌晨4点......希望我的答案太糟糕了。

break only gets you out of the smallest enclosing switch or loop.

You could put another condition in the for:

for (i = done = 0; i < 10 && !done; i++) {
switch(array[i]) {
case ''a'':
done = 1;
break; // or "continue" to get right out
case ''b'':
break;
}
}

Or you could use goto:

for (i = 0; i < 10; i++) {
switch(array[i]) {
case ''a'':
goto done;
case ''b'':
break;
}
}
done:

-Beej--4am...hopefully I didn''t screw that answer up too bad.


2月25日上午11点16分,Alexander Korsunsky< ; A.Korsun ... @ gmail.com>

写道:
On Feb 25, 11:16 am, Alexander Korsunsky <A.Korsun...@gmail.com>
wrote:

>

char array [10] =" abcdefghij" ;;
>
char array[10] = "abcdefghij";



不幸的是,这并没有像
$ b那样生成

编译器警告$ b int array [2] = {1,2,3};确实如此。

It''s unfortunate that this doesn''t generate a
compiler warning in the same way that
int array[2] = { 1, 2, 3}; does.


>

是否有可能从一个案例内部突破for循环 -

声明,还是我必须使用解决方法?
>
Is it possible to break out of the for loop from inside of one case-
statement, or do I have to use a workaround?



至少有3种显而易见的方式:

1)增加指数以使你的状况在

for循环终止循环(一种解决方法。)

2)goto

3)修复循环条件以更准确地反映

无论如何这是你正在测试。 (可能是
正确的解决方案。)


没有中断3但是,在C语言中键入语法。


-

Bill Pursell

There are at least 3 obvious ways:
1) increment the index to cause your condition on the
for loop to terminate the loop (a workaround.)
2) goto
3) fix the loop condition to more accurately reflect
whatever it is you are testing for. (Probably
the correct solution.)

There is no "break 3" type syntax in C, however.

--
Bill Pursell


On Sun,25 2007年2月12:16:06 +0100,Alexander Korsunsky

< A. ********* @ gmail.comwrote:
On Sun, 25 Feb 2007 12:16:06 +0100, Alexander Korsunsky
<A.*********@gmail.comwrote:

>嗨!

我有一些类似于此的代码:

---------------- ----------------------------

char array [10] =" abcdefghij";

for(int i = 0; i< 10; i ++)
{
switch(array [i])
{
case''a'' :
/ *代码* /
休息;
案例''b'':
/ *代码* /
休息;
案例''c '':
/ *代码* /
休息;

默认:
/ *代码* /
休息;
}
>Hi!

I have some code that looks similar to this:

--------------------------------------------

char array[10] = "abcdefghij";

for (int i = 0; i < 10; i++)
{
switch (array[i])
{
case ''a'':
/* code */
break;
case ''b'':
/* code */
break;
case ''c'':
/* code */
break;

default:
/* code */
break;
}



//交换机执行后,控制权在这里传递。

//您可以重新测试并在此处中断以退出for循环。

//例如,如果情况''b'则打破。


if(array [ i] ==''b'')

休息;

// After the switch executes, control passes here.
// You could retest and break here to get out of the for loop.
// For example, breaking if case ''b''.

if(array[i] == ''b'')
break;


>
}
--------------------------------------------
<是否有可能从一个案例
声明中删除for循环,或者我是否必须使用变通方法?
>
}

--------------------------------------------

Is it possible to break out of the for loop from inside of one case-
statement, or do I have to use a workaround?



我不知道双重中断命令。 break语句停止

执行最小的封闭switch语句。如上所述,我会打破每个人的b $ b。您可以重新考虑switch语句并将

重新命名为if-else系列。


祝福,


Bob

I''m not aware of a double break command. A break statement stops
execution of the smallest enclosing switch statement. I would break
out of each as above. You might rethink the switch statement and
recast it as a if-else series.

Best wishes,

Bob


这篇关于在循环内部的case语句中打破的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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