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 14 19397
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.
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
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.
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.
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.
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.
"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:f7******** *************@b t.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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 tried
Console.In.ReadBlock(), too, with no success.
So, along these lines, how would I implement, simply, a "Press Any Key to
Continue" routine or,...
|
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) == '-' )
{
|
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 positive
thing about it is that the error is repeatable.
Consider the following code snippet (it may not produce the bug, but I
can't really post...
|
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 will closed automatisch.
Thank you
|
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
'getchar', or 'gets', or some get function? or 'cin', or what's
an easy way?
What I want is the program to pause before exiting so the user
can...
| |
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 to explicitly enable the "Continue" button.
However, it's still not enabled. Anyone can tell me why? Thanks.
|
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
|
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 good principle/s for the good usage of "continue"...or...do
any of the regular/irregular contributors have a "favorite" way in
which it is...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |