473,288 Members | 2,350 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

How to "continue" in nested do-while-loop?

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
Dec 15 '06 #1
14 19377
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.

Dec 15 '06 #2

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

Dec 15 '06 #3
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.
Dec 15 '06 #4
Hi Jan,
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):
Perhaps I do not understand fully your problem, but why don't you use
simply 'break' statement?

do {
// ....
while (y) {
if (z) break; // go out of that while(y) loop
}
// ...
}
while (x);

HTH,
Loic

Dec 15 '06 #5
loic-...@gmx.net wrote:
Hi Jan,
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):

Perhaps I do not understand fully your problem, but why don't you use
simply 'break' statement?

do {
// ....
while (y) {
if (z) break; // go out of that while(y) loop
}
// ... [ XXX ]
}
while (x);
Any statements at the location I marked with [ XXX ] would be executed
with your suggestion, but the idea of "continue" is to go straight to
rechecking the loop condition and to skip everything else.

Dec 15 '06 #6
Hi Harald,
do {
// ....
while (y) {
if (z) break; // go out of that while(y) loop
}
// ... [ XXX ]
}
while (x);

Any statements at the location I marked with [ XXX ] would be executed
with your suggestion, but the idea of "continue" is to go straight to
rechecking the loop condition and to skip everything else.
Thanks for your explanations... Now, I understand what the problem is
;-)

Cheers,
Loic.

Dec 15 '06 #7
Hi again,
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):
Ok, now that I do understand your problem, another alternative to your
2nd work-around could be:

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

Cheers,
Loic.

Dec 15 '06 #8
Jan Schmidt said:
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
do
{
foo();
bar = 0;
while(!bar && baz)
{
quux();

/* At this point, either you want to carry on
processing this loop, or you don't. Set bar accordingly. */

if(!bar)
{
quuy();
}
}
if(!bar)
{
quuz();
}
} while(ribbit);

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 15 '06 #9

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:f7*********************@bt.com...
Jan Schmidt said:
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

do
{
foo();
bar = 0;
while(!bar && baz)
{
quux();

/* At this point, either you want to carry on
processing this loop, or you don't. Set bar accordingly. */

if(!bar)
{
quuy();
}
}
if(!bar)
{
quuz();
}
} while(ribbit);

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Amazing that on clc it took this long for someone to post
the effective answer - Why not write the code correctly
instead of hack it to work?
Dec 17 '06 #10
Barry said:

<snip>
Amazing that on clc it took this long for someone to post
the effective answer -
Well, I actually posted it within two hours of the question being asked. It
seems that it's taken two days for my answer to reach your server, though -
so what is *truly* amazing is how slow some news swervers can be. :-)
Why not write the code correctly instead of hack it to work?
Quite so.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 17 '06 #11
Amazing that on clc it took this long for someone to post
the effective answer - Why not write the code correctly
instead of hack it to work?
checking same condition twice is correct? in my opinion this is hacking
just to prevent goto. I still wonder why some programmers are afraid of
it. Just like C#ers are afraid of pointers, because they can be
misused.

what's wrong with having label at end of compound statement? This is
clear example where it can be useful.

Dec 17 '06 #12
Amazing that on clc it took this long for someone to post
the effective answer - Why not write the code correctly
instead of hack it to work?
checking same condition twice is correct? in my opinion this is hacking
just to prevent goto. I still wonder why some programmers are afraid of
it. Just like C#ers are afraid of pointers, because they can be
misused.

what's wrong with having label at end of compound statement? This is
clear example where it can be useful.

Dec 17 '06 #13
"Barry" <ba****@nullhighstream.netwrote:
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
Jan Schmidt said:
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
do
{
foo();
bar = 0;
while(!bar && baz)
{
quux();

/* At this point, either you want to carry on
processing this loop, or you don't. Set bar accordingly. */

if(!bar)
{
quuy();
}
}
if(!bar)
{
quuz();
}
} while(ribbit);

Amazing that on clc it took this long for someone to post
the effective answer - Why not write the code correctly
instead of hack it to work?
Because peppering your code with dozens of superfluous variables and
extra ifs is _not_ the correct way to write this code. Perhaps there is
a way to rewrite the OP's code so that it doesn't use goto and maintains
its legibility - but this isn't it.

Richard
Dec 18 '06 #14
Richard Bos said:

<snip>
>>
Amazing that on clc it took this long for someone to post
the effective answer - Why not write the code correctly
instead of hack it to work?

Because peppering your code with dozens of superfluous variables and
extra ifs is _not_ the correct way to write this code.
I didn't use any variables not used by the OP. (I did, however, give them
different names.) It's true that I used one extra condition in the if(),
but in the process I lost a goto and made the code structure clearer.

By all means criticise my code if you wish - that's how we learn, right? -
but please try to keep your criticisms accurate.
Perhaps there is
a way to rewrite the OP's code so that it doesn't use goto and maintains
its legibility - but this isn't it.
It's one way. If you have a better alternative, let's see it. Until then, I
stand by my solution.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 18 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Paul Johnston | last post by:
I'm using VB.Net. I've tried using Console.Read() but it requires a line-terminator before it finishes (doesn't that mean it's functionally equivalent to ReadLine() -- why have it then?). I've...
14
by: Daniel Bass | last post by:
is there an equivalent key word for C++'s "continue" in VB (.net) in this context? CString szLine; szLine = myReader.ReadLine(); while ( !szLine.IsEmpty() ) { if ( szLine(0) == '-' ) {
2
by: Mihajlo Cvetanović | last post by:
Avoid "Program Database for Edit & Continue" in VC 2003! This option made me think I'm in Twilight Zone! The code behaves as not intended, the breakpoints in the debugger get skipped, the only...
8
by: Timur Ametov | last post by:
Hallo everybody. I'm using Visual Studio 7 and hier is my question. How can i see in Outputwindow after my Programm ended the message "Press any key to continue". Is it possible? Now this window...
9
by: Susan Rice | last post by:
I'm running a simple win32 console application and I want to impliment a "Press any key to continue", so I print that prompt, and then what's the easiest way to impliment reading any key? Do I use...
4
by: Curious | last post by:
I have two buttons on a form, "Pause" and "Continue". When "Pause" button is clicked, it should: 1) Stop the background worker; and, 2) Enable the "Continue" button. I have the code below...
13
by: xz | last post by:
What if I want the following: vector<intv; // v is loaded by push_back() switch( v.size() ) { case 2: //do something
36
by: mdh | last post by:
May I ask the group this somewhat non-focused question....having now seen "continue" used in some of the solutions I have worked on. ( Ex 7-4 solution by Tondo and Gimpel comes to mind) Is there a...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.