如何“继续”在嵌套的do-while-loop中? [英] How to "continue" in nested do-while-loop?

查看:49
本文介绍了如何“继续”在嵌套的do-while-loop中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在嵌套的do-while-loop结构中我想继续外部

循环。使用goto,这在while循环中应该没问题。然而,对于

do-while我无法让它工作(没有一个奇怪的解决方案构造):


-



{

// ...

而(y){

if(z)goto next ; //继续外循环(但检查x!)

}

// ...

// A

}

// B

而(x);

-


问题是在哪里放置下一个这个构造中的标签?

将它放在A处给我一个复合语句末尾的标签,在B处放置

给出语法错误。


当然,我可以构建这样的东西:


-

做{

下一个:

// ...

而(y){

if(z){

如果(x)转到下一个; //继续外循环

其他转到后循环;

}

}

// ...

}

而(x);

afterloop:// ...

-


或者这个:


-

做{

// ...

while(y){

if(z)goto next; //继续外循环

}

// ...

下一个:

if(0){ }; //假允许标签

}

而(x);

-


但不知怎的,我的印象必须是直截了当的:-)


TIA,Jan

解决方案

Jan Schmidt写道:





嵌套的do-while-loop结构我想要继续外部

循环。使用goto,这在while循环中应该没问题。然而,对于

do-while我无法让它工作(没有一个奇怪的解决方案构造):


-



{

// ...

而(y){

if(z)goto next ; //继续外循环(但检查x!)

}

// ...

// A

}

// B

而(x);

-


问题是在哪里放置下一个这个构造中的标签?

将它放在A处给我一个复合语句末尾的标签,在B处放置

给出语法错误。



在A,有类似于下面描述的解决方法。


当然,我可以建造这样的东西:


-

做{

next:

//。 ..

while(y){

if(z){

if(x)goto next; //继续外循环

其他转到后循环;

}

}

// ...

}

而(x);

afterloop:// ...



那会有用,但它比下面的要好。


或者这个:


-

做{

// ...

而(y){

if(z)goto next; //继续外循环

}

// ...

下一个:

if(0){ }; //假允许标签

}

while(x);



您只需使用next:;即可。单个分号也是一个声明。


但不知怎的,我的印象一定是直截了当的:-)


你会这么认为,但这是C的限制之一,对不起。也许

你可以重写你的代码以避免它的需要,但如果你不能,

这是你能得到的最好的。




Jan Schmidt写道:



我想继续外部

循环。使用goto,这在while循环中应该没问题。然而,对于

do-while我无法让它工作(没有一个奇怪的变通方法构造):



(snip)


或者这个:


-

do {

// ...

while(y){

if(z)goto next; //继续外循环

}

// ...

下一个:

if(0){ }; //假允许标签

}

而(x);

-


但不知怎的,我的印象必须是直截了当的:-)


TIA,Jan



你对你最后的建议有正确的想法。为此目的提供了一个虚拟的

语句:使用单个分号,如下所示:

do {

/ * ... * /

while(y){

if(z)goto next;

}

/ * .. 。* /

下一个:;

}

而(x);


分号本身是一个没有效果的声明。

-

ais523


Jan Schmidt< ; sp ** @ jan-o-sch.netwrites:


嵌套的do-while-loop结构我想继续外部

循环。使用goto,这在while循环中应该没问题。然而,对于

do-while我无法让它工作(没有一个奇怪的解决方案构造):


-



{

// ...

而(y){

if(z)goto next ; //继续外循环(但检查x!)

}

// ...

// A

}

// B

而(x);

-


问题是在哪里放置下一个这个构造中的标签?

将它放在A处给我一个复合语句末尾的标签,在B处放置

给出语法错误。



C语法说标签后面必须跟一个

语句。这是'被标记的陈述,而不是

陈述之间的差距。通常的躲闪是在

标签后面加上一个分号,这样它就会标出空的陈述。


它不会去努力将它放在位置B,因为

做...虽然可能只包含一个语句(通常是

,在这种情况下是复合语句)。

-

只是另一个C黑客。


Hi,

in a nested do-while-loop structure I would like to "continue" the outer
loop. With goto this should be no problem in while-loops. However, for
do-while I cannot get it to work (without a strange workaround construct):

--
do
{
// ...
while (y) {
if (z) goto next; //continue outer loop (but check x!)
}
// ...
//A
}
//B
while (x);
--

The question is where to place the "next" label in this construct?
Placing it at A gives me a "label at end of compound statement", placing
it at B gives a "syntax error".

Of course, I could construct things like this:

--
do {
next:
// ...
while (y) {
if (z) {
if (x) goto next; //continue outer loop
else goto afterloop;
}
}
// ...
}
while (x);
afterloop: // ...
--

Or this:

--
do {
// ...
while (y) {
if (z) goto next; //continue outer loop
}
// ...
next:
if (0) {}; //dummy to allow label
}
while (x);
--

But somehow I''ve got the impression there must be a straight way :-)

TIA, Jan

解决方案

Jan Schmidt wrote:

Hi,

in a nested do-while-loop structure I would like to "continue" the outer
loop. With goto this should be no problem in while-loops. However, for
do-while I cannot get it to work (without a strange workaround construct):

--
do
{
// ...
while (y) {
if (z) goto next; //continue outer loop (but check x!)
}
// ...
//A
}
//B
while (x);
--

The question is where to place the "next" label in this construct?
Placing it at A gives me a "label at end of compound statement", placing
it at B gives a "syntax error".

At A, with a workaround similar to what you describe below.

Of course, I could construct things like this:

--
do {
next:
// ...
while (y) {
if (z) {
if (x) goto next; //continue outer loop
else goto afterloop;
}
}
// ...
}
while (x);
afterloop: // ...

That would work, but it''s uglier than the below.

Or this:

--
do {
// ...
while (y) {
if (z) goto next; //continue outer loop
}
// ...
next:
if (0) {}; //dummy to allow label
}
while (x);

You can just use "next: ;". A single semicolon is a statement as well.

But somehow I''ve got the impression there must be a straight way :-)

You''d think so, but this is one of the limitations of C, sorry. Maybe
you can rewrite your code to avoid a need for it, but if you can''t,
this is the best you can get.



Jan Schmidt wrote:

Hi,

in a nested do-while-loop structure I would like to "continue" the outer
loop. With goto this should be no problem in while-loops. However, for
do-while I cannot get it to work (without a strange workaround construct):

(snip)

Or this:

--
do {
// ...
while (y) {
if (z) goto next; //continue outer loop
}
// ...
next:
if (0) {}; //dummy to allow label
}
while (x);
--

But somehow I''ve got the impression there must be a straight way :-)

TIA, Jan

You have the right idea with your last suggestion. There is a dummy
statement provided for this purpose: use a single semicolon, like this:
do {
/* ... */
while(y) {
if(z) goto next;
}
/* ... */
next: ;
}
while(x);

The semicolon by itself is a statement with no effect.
--
ais523


Jan Schmidt <sp**@jan-o-sch.netwrites:

in a nested do-while-loop structure I would like to "continue" the outer
loop. With goto this should be no problem in while-loops. However, for
do-while I cannot get it to work (without a strange workaround construct):

--
do
{
// ...
while (y) {
if (z) goto next; //continue outer loop (but check x!)
}
// ...
//A
}
//B
while (x);
--

The question is where to place the "next" label in this construct?
Placing it at A gives me a "label at end of compound statement", placing
it at B gives a "syntax error".

The C grammar says that a label has to be followed by a
statement. It''s the statement that''s labeled, not a gap between
statements. The usual dodge is to just put a semicolon after the
label, so that it''s labeling the empty statement.

It''s not going to work to put it in location B, because
do...while may only contain a single statement (which ordinarily
and in this case is a compound statement).
--
Just another C hacker.


这篇关于如何“继续”在嵌套的do-while-loop中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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