473,950 Members | 21,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5093

" Bern" <x@x.com> wrote in message news:41******@n ews.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(o uter_loop);
if(whatever2) break_loop(oute r_loop);
}
outer_loop_cont inue:
}
outer_loop_brea k:

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******@n ews.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
2706
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 "keyList": case "curKey": print "Matched exception<br />";
5
2549
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
2059
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 never in the loop again??????????????????????? DInst *dstReady = dinst->getNextPending();
14
3683
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
2579
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 page is left blank. I tried to use a code snippet with the "return confirm" function but without success. The code should check whether the textbox is empty or not, and if empty, it should ask the user to continue and then run the next code. How...
15
9186
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 GoTo GoTo nxt End If
12
1385
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 miss it.
25
18419
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
2026
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
2692
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 particularly useful. Thanks as usual.
0
10171
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11600
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, 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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11191
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9904
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8268
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7443
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6233
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.