Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > u misunderstand

Reply
Thread Tools

u misunderstand

 
 
Bern
Guest
Posts: n/a
 
      09-03-2004
i think i wasn't clear enough or you misunderstood.

continue the "big" loop means reset to the beginning of the "big" loop,
which means the condition of the big loop is evaluated , -- as if
the program has just entered the loop.

for example:

while (some condition){

if (condition) continue; //// continue means reset to the beginning
of the loop.

if (another ...){

some code;
}
}


if there is such thing as labeled continue,
we can do the following:
----------------------------------------------------
big_loop:

while (...){

some code;

while (...){

/*
continue big_loop means reset to the beginning
of big loop.
it means exactly "goto big_loop" here.
*/
if (....) continue big_loop;

}

}

however, C/C++ does not support labeled continue and breaks.

i know labeled continue and breaks can be replaced by "gotos",
but, such useful feature why doesn't C++ support?

i think labeled continue and breaks are similar to goto, thats why
the creators of C++ doesn't want to include this feature, since they
are already discouraging the use of goto.

then it comes to this question: Can we really do without gotos??





 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      09-03-2004
" Bern" <(E-Mail Removed)> wrote in message news:41384968$(E-Mail Removed)...
> if there is such thing as labeled continue,
> we can do the following:
> ----------------------------------------------------
> big_loop:
>
> while (...){
>
> some code;
>
> while (...){
>
> /*
> continue big_loop means reset to the beginning
> of big loop.
> it means exactly "goto big_loop" here.
> */
> if (....) continue big_loop;
>
> }
>
> }
>
> however, C/C++ does not support labeled continue and breaks.


In the instance above, you can replace "continue big_loop;"
with "break;" -- this will have the same effect.

If there is code after the inner loop that should not
be executed when continuing to the outer loop, however,
you may need to add a flag/test;
bool skip = false;
while (...){
some code;
while (...){
if (....) { skip = true; break; }
}
if(skip) skip=false;
else {
/* some code to be skipped by break/"continue" */
}
}

> i know labeled continue and breaks can be replaced by "gotos",
> but, such useful feature why doesn't C++ support?

Because it is not that much better / more informative than
a simple goto.
Actually, the problem above can better be solved by a syntax
supported in Python: an else clause at the end of a loop,
which is executed only when the loop exists because its
condition is false.
This leads to the following code:

while (...){
some code;
while (...){
if (....) break;
}
else {
/* some code to be skipped by break/"continue" */
}
}

I find this more elegant than a labeled continue...

> i think labeled continue and breaks are similar to goto, thats why
> the creators of C++ doesn't want to include this feature, since they
> are already discouraging the use of goto.

The designers of C and C++ are not the only ones to discourage gotos!
However, gotos do have few acceptable uses.
What happened is that developers of e.g. Java have decided to
only implement gotos in the situations that were seen as 'acceptable'.

> then it comes to this question: Can we really do without gotos??


Yes. A few languages don't have them, but remain usable.
The problem with gotos is that they get abused, and one
should always look for alternatives.


hth-Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com





 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      09-03-2004
Bern wrote:

> i think i wasn't clear enough or you misunderstood.
>
> continue the "big" loop means reset to the beginning of the "big" loop,
> which means the condition of the big loop is evaluated , -- as if
> the program has just entered the loop.
>
> for example:
>
> while (some condition){
>
> if (condition) continue; //// continue means reset to the

beginning
> of the loop.
>
> if (another ...){
>
> some code;
> }
> }
>
>
> if there is such thing as labeled continue,
> we can do the following:
> ----------------------------------------------------
> big_loop:
>
> while (...){
>
> some code;
>
> while (...){
>
> /*
> continue big_loop means reset to the beginning
> of big loop.
> it means exactly "goto big_loop" here.
> */
> if (....) continue big_loop;
>
> }
>
> }
>
> however, C/C++ does not support labeled continue and breaks.


That's because functions shouldn't contain so many statements. If the inner
loop were inside a method, it could use 'return' to reenter the outer loop.

> then it comes to this question: Can we really do without gotos??


Don't blame the symptom.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


 
Reply With Quote
 
Andrew Koenig
Guest
Posts: n/a
 
      09-03-2004
" Bern" <(E-Mail Removed)> wrote in message news:41384968$(E-Mail Removed)...

> i know labeled continue and breaks can be replaced by "gotos",
> but, such useful feature why doesn't C++ support?


Perhaps the feature is not as useful as you think it is.

The existence of such a feature makes it much more difficult to reason about
programs, because it implies that the effect of

statement1;
statement2;

might not be the same as the effect of statement1 followed by the effect of
statement2.

Perhaps you should try writing an actual piece of code that would use your
ideal feature. Then we could examine the code, point out what we think is
good and not so good about it, and suggest alternatives. As it is, we don't
have much to use as a basis for discussion.


 
Reply With Quote
 
Bern
Guest
Posts: n/a
 
      09-04-2004
> while (...){
> if (....) { skip = true; break; }
> }
> if(skip) skip=false;
> else {
> /* some code to be skipped by break/"continue" */
> }


you waste time executing the" if (skip) skip=false" statement.
it would be better if the program jumps directly to the outer loop

"Ivan Vecerina" <(E-Mail Removed) > wrote in
message news:ch9j3v$tnj$(E-Mail Removed)...
> " Bern" <(E-Mail Removed)> wrote in message news:41384968$(E-Mail Removed)...
> > if there is such thing as labeled continue,
> > we can do the following:
> > ----------------------------------------------------
> > big_loop:
> >
> > while (...){
> >
> > some code;
> >
> > while (...){
> >
> > /*
> > continue big_loop means reset to the beginning
> > of big loop.
> > it means exactly "goto big_loop" here.
> > */
> > if (....) continue big_loop;
> >
> > }
> >
> > }
> >
> > however, C/C++ does not support labeled continue and breaks.

>
> In the instance above, you can replace "continue big_loop;"
> with "break;" -- this will have the same effect.
>
> If there is code after the inner loop that should not
> be executed when continuing to the outer loop, however,
> you may need to add a flag/test;
> bool skip = false;
> while (...){
> some code;
> while (...){
> if (....) { skip = true; break; }
> }
> if(skip) skip=false;
> else {
> /* some code to be skipped by break/"continue" */
> }
> }
>
> > i know labeled continue and breaks can be replaced by "gotos",
> > but, such useful feature why doesn't C++ support?

> Because it is not that much better / more informative than
> a simple goto.
> Actually, the problem above can better be solved by a syntax
> supported in Python: an else clause at the end of a loop,
> which is executed only when the loop exists because its
> condition is false.
> This leads to the following code:
>
> while (...){
> some code;
> while (...){
> if (....) break;
> }
> else {
> /* some code to be skipped by break/"continue" */
> }
> }
>
> I find this more elegant than a labeled continue...
>
> > i think labeled continue and breaks are similar to goto, thats why
> > the creators of C++ doesn't want to include this feature, since they
> > are already discouraging the use of goto.

> The designers of C and C++ are not the only ones to discourage gotos!
> However, gotos do have few acceptable uses.
> What happened is that developers of e.g. Java have decided to
> only implement gotos in the situations that were seen as 'acceptable'.
>
> > then it comes to this question: Can we really do without gotos??

>
> Yes. A few languages don't have them, but remain usable.
> The problem with gotos is that they get abused, and one
> should always look for alternatives.
>
>
> hth-Ivan
> --
> http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
> Brainbench MVP for C++ <> http://www.brainbench.com
>
>
>
>
>



 
Reply With Quote
 
Ivan Vecerina
Guest
Posts: n/a
 
      09-05-2004
> "Ivan Vecerina" <(E-Mail Removed) > wrote
in
> message news:ch9j3v$tnj$(E-Mail Removed)...

" Bern" <(E-Mail Removed)> wrote in message news:413959b4$(E-Mail Removed)...
> > while (...){
> > if (....) { skip = true; break; }
> > }
> > if(skip) skip=false;
> > else {
> > /* some code to be skipped by break/"continue" */
> > }

> you waste time executing the" if (skip) skip=false" statement.
> it would be better if the program jumps directly to the outer loop


Cases where this "waste of time" is measurably significant will be few.
But the real question is, what is better for clarity/maintainability:
adding such a dispatch flag, or actually using a goto?

In such a situation, I do prefer using a goto than either of the
commonly seen workarounds ( adding a flag or a throw-catch) :

> > However, gotos do have few acceptable uses.
> > What happened is that developers of e.g. Java have decided to
> > only implement gotos in the situations that were seen as 'acceptable'.

....
> > The problem with gotos is that they get abused, and one
> > should always look for alternatives.


This said, nested loops bring possibly unnecessary complexity.
Given a concrete example, we could explore refactorings/redesigns
that can advantageously replace a goto.

NB:
The worst loop nesting I ever saw in a review was 7-level deep !
There is no way I would have let this into a check-in...


Cheers,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Did I misunderstand the concept of threads? Lars Uffmann C++ 3 02-07-2008 01:57 PM
URLEncoder -- When Is It Really Needed? (I Seem to Misunderstand It.) Hal Vaughan Java 3 04-10-2006 05:57 AM
Misunderstand WTK2.0 teddykim Java 1 08-06-2004 12:33 AM



Advertisments