473,399 Members | 4,254 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,399 software developers and data experts.

continue


lets say i have the code:

while (some condition){ /* the "big" loop */

.... some code....

while (some condition){

...code...
if (!some condition){

///// How to continue the "big" loop?
}

..some code....
}

...some code.....
}

Is there any other solution besides resorting to using goto?
Or is there a way to rearrange the whole structure to eliminate the use of 2
loops?

this situation in Java is not a problem as Java supports labeled breaks and
continue,
but why C/C++ doesn't support them?

I basically find it amusing--we are discouraged to use goto as a practice
of good progrtamming, but C++ does not provide support for such things in
programming.....
Jul 22 '05 #1
6 5063

" Bern" <x@x.com> wrote in message news:41******@news.starhub.net.sg...

lets say i have the code:

while (some condition){ /* the "big" loop */

.... some code....

while (some condition){

...code...
if (!some condition){

///// How to continue the "big" loop?
}

..some code....
}

...some code.....
}

Is there any other solution besides resorting to using goto?
Or is there a way to rearrange the whole structure to eliminate the use of 2 loops?


Here's one way:

Implement the inner loop in a separate function, and call
that function from the "big" loop. When the inner loop
detects the "continue" situation, it simply returns, so that
the execution continues at that point in the "big" loop:

void InnerLoop()
{
while (some condition){
...code...
if (!some condition){
return; ///// to continue the "big" loop!
}
..some code....
}
}
while (some condition){ /* the "big" loop */
.... some code....
InnerLoop();
...some code.....
}
Of course, parameters may have to be passed and the meaning
of a return value defined (if appropriate) to/from the InnerLoop() .

Also, if the "if (!some condition)" is used to detect an 'exceptional'
situation, then try/catch/throw can also be used:

typedef int up;

while (some condition){ /* the "big" loop */
.... some code....
try {
while (some condition){
...code...
if (!some condition){
throw up(); // :-)
}
..some code....
}
}
catch( up & )
{
// deliberately empty
}
...some code.....
}
Note, some developers will object this - for a reason, really - if
the condition to throw is not 'exceptional' enough. Only you can
judge what is exceptional enough, though... :-)

Cheers!

- Risto -
Jul 22 '05 #2
Bern wrote:
lets say i have the code:

while (some condition){ /* the "big" loop */

.... some code....

while (some condition){

...code...
if (!some condition){

///// How to continue the "big" loop?
}

..some code....
}

...some code.....
}

Is there any other solution besides resorting to using goto?
What's the reason for not using goto for this?
Or is there a way to rearrange the whole structure to eliminate the use of
2 loops?

this situation in Java is not a problem as Java supports labeled breaks
and continue, but why C/C++ doesn't support them?
C++ has goto, which can do the same. It also jumps to a label. The
difference is just that it doesn't need to be a jump out of a loop.
I basically find it amusing--we are discouraged to use goto as a practice
of good progrtamming,
Says who? You're discouraged to _abuse_ goto, just like you are discouraged
to abuse any other language feature. I acually find it funny that so many
people seem to be told that goto is evil and simply believe that they must
never use it even if that usage would be perfectly valid just because
"though shalt never use goto" or something.
but C++ does not provide support for such things in programming.....


It provides goto.

Jul 22 '05 #3
Bern wrote:

lets say i have the code:

while (some condition){ /* the "big" loop */

.... some code....

while (some condition){

...code...
if (!some condition){

///// How to continue the "big" loop?
}

..some code....
}

...some code.....
}

Is there any other solution besides resorting to using goto?
Or is there a way to rearrange the whole structure to eliminate the use of
2 loops?

this situation in Java is not a problem as Java supports labeled breaks
and continue,
but why C/C++ doesn't support them?
I do not really see that goto is worse programming practice than attaching
labels to break and continue. Every once in a while, goto is just the right
thing to use. In fact, this seems to be a comparatively innocent case.

However, very often running into the need of goto or break labels indicates
that the algorithm might not be well planned. You did not provide enough
code for us the discuss this, so I will assume that you really need to exit
from an inner loop by two levels.

I basically find it amusing--we are discouraged to use goto as a practice
of good progrtamming, but C++ does not provide support for such things in
programming.....


Well, the *really* confusing uses of goto are those where you exit a
routine via goto for a jump to some error handler. This is about language
support for programming in the large. C++ has exceptions for those global
jumps, and even makes guarantees about cleanup involved in stack unwinding.
For local jumps, it appears that goto is just the more powerful way of
messing up your code.

I highly recommend:

D.E. Knuth: Structured Programming with goto Statements
(Computing Surveys, Vol 6, No 4, Dec 1974, p 261--301)
Best

Kai-Uwe Bux
Jul 22 '05 #4
Bern wrote:
lets say i have the code:

while (some condition){ /* the "big" loop */

.... some code....

while (some condition){

...code...
if (!some condition){

///// How to continue the "big" loop?
}

..some code....
}

...some code.....
}

Is there any other solution besides resorting to using goto?
Or is there a way to rearrange the whole structure to eliminate the use of 2
loops?


You missed Dijkstra's point :). Your example is the classic most
legitimate use of 'goto' statement there is. Therefore don't
"resort to using goto" here, just "use goto here".

HTH,
- J.
Jul 22 '05 #5
Bern wrote:
Is there any other solution besides resorting to using goto?


In languages like Java which have labelled breaks and continues, these
are better. From the C Rationale:

"3.6.6.2 The continue statement

The Committee rejected proposed enhancements to continue and break which
would allow specification of an iteration statement other than the
immediately enclosing one, on grounds of insufficient prior art."

It's a good idea that now has "prior art"; it makes the purpose more
explicit. C just didn't do it. Use goto - it's not a big deal. If you
want to make it more explicit, try out these macros:

#define continue_loop(x) goto x##_continue
#define break_loop(x) goto x##_break

Then you can do:

while(...) {
while(...) {
if(whatever) continue_loop(outer_loop);
if(whatever2) break_loop(outer_loop);
}
outer_loop_continue:
}
outer_loop_break:

Another common solution is to form a chain of breaks/continues. For example:

while(...) {
int i;
for(i=0; i<n; i++) {
if (evil is afoot) break;
...
}
if (i < n) {
/* We get here if and only if we did a break above */
break; /* or continue */
}
...
}

All in all, a goto seems clearer and less error-prone than this
approach, especially for longer chains.
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Jul 22 '05 #6
" Bern" <x@x.com> wrote in message news:41******@news.starhub.net.sg...

lets say i have the code:

while (some condition){ /* the "big" loop */
.... some code....
while (some condition){
...code...
if (!some condition){
///// How to continue the "big" loop?
}
..some code....
}
...some code.....
}

Is there any other solution besides resorting to using goto?
The "only good use" of a goto is a forward reference to jump out of
multiple embedded loops. It's "bad" to use a goto to do looping or to
jump into other functions.

Another way to do what you want is by using flags. This gets tedious if
there are many different ones, and adds an extra check at each loop
point. Here's how that's done (this better not be homework!):

-------------------------------------------
continue_flag = TRUE;
while (some condition){ /* the "big" loop */
.... some code....
while (TRUE == continue_flag && some condition){
...code...
if (!some condition){
continue_flag = FALSE; /* set continue inner loop flag */
break; /* avoids running the "some inner code" */
}
..some inner code....
}
...some outer code.....
}
-------------------------------------------
Or is there a way to rearrange the whole structure to eliminate the use of 2 loops?


Only you can decide that.
Jul 22 '05 #7

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

Similar topics

2
by: Michael Satterwhite | last post by:
I *MUST* be overlooking something obvious. Consider the following code: foreach($_POST as $key=>$value) { print "$key=>$value<br />"; if(! empty($value)) { switch($key) { case "Submit": case...
5
by: Ann | last post by:
I have trouble sometimes figuring out where break and continue go to. Is there some easy way to figure it out, or a tool? TIA Ann
3
by: Jianli Shen | last post by:
const DInst *stopAtDst = 0; while (dinst->hasPending()) { if (stopAtDst == dinst->getFirstPending()) break; ///////break? break? break? is this break outof the whole while loop and...
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: buran | last post by:
Dear ASP.NET Programmers, I have a question about a script I'm trying to code and invoke when a button (btnSave) is pressed on the page. This script should only run when a textbox (txtAD) on the...
15
by: PagCal | last post by:
Is this language missing the functionality of a C/C++ 'continue' statement? For example: While NOT isEof() If condition ' a C or C++ continue would work here ' but we are forced to use a...
12
by: William | last post by:
VB6 had the ability to pause your execution while debugging, messing with the code, and continue executing right from where you left off. Is DotNet ever going to have this capability? I really...
25
by: v4vijayakumar | last post by:
'continue' within switch actually associated with the outer 'while' loop. Is this behavior protable? int ch = '\n'; while (true) { switch(ch) { case '\n': cout << "test"; continue; } }
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
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,...
0
jinu1996
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
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...

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.