473,586 Members | 2,681 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Continue" usage

mdh
May I ask the group this somewhat non-focused question....hav ing 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.
Sep 22 '08 #1
36 2630
mdh said:
May I ask the group this somewhat non-focused question....hav ing 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.
Personally, I prefer to reserve it as an indicator that an otherwise empty
loop is intended to be empty:

for(t = s; *s != '\0'; (*s != c) ? *t++ = *s++ : *s++)
{
continue;
}
*t = '\0';

Another reasonable view is that it should be used for quickly dispensing
with exceptional situations encountered in a loop where you don't want to
do any processing on this iteration (but don't want to stop the loop):

while(line_read ing_function(&l ine, &len, fp) != EOF)
{
skipwhite(&line );
if(line[0] == '\0')
{
continue; /* empty line */
}
if(line[0] == '#')
{
continue; /* comment encountered */
}

/* now we can get on with processing "real" lines */
Sep 22 '08 #2
mdh
On Sep 21, 8:39*pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
mdh said:
May I ask the group
.....having now
seen "continue" used
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. *

Personally, I prefer to reserve it as an indicator that an otherwise empty
loop is intended to be empty:

for(t = s; *s != '\0'; (*s != c) ? *t++ = *s++ : *s++)
{
* continue;}

*t = '\0';

Another reasonable view is that it should be used for quickly dispensing
with exceptional situations encountered in a loop where you don't want to
do any processing on this iteration (but don't want to stop the loop):

while(line_read ing_function(&l ine, &len, fp) != EOF)
{
* skipwhite(&line );
* if(line[0] == '\0')
* {
* * continue; /* empty line */
* }
* if(line[0] == '#')
* {
* * continue; /* comment encountered */
* }

* /* now we can get on with processing "real" lines */
* .
*
thanks Richard for those "hooks" to hang that idea on.
Sep 22 '08 #3
mdh wrote:
>
May I ask the group this somewhat non-focused question....hav ing
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.
I primarily use it to indicate an empty statement. It is not
limited to that however. For example:

while ((EOF != (ch = getc(f))) && ('\n' != ch)) continue;

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Sep 22 '08 #4
CBFalconer said:
mdh wrote:
>>
May I ask the group this somewhat non-focused question....hav ing
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.

I primarily use it to indicate an empty statement. It is not
limited to that however. For example:

while ((EOF != (ch = getc(f))) && ('\n' != ch)) continue;
Your example is fine, but your description is not, because you can't always
use continue to indicate an empty statement - *only* in loop contexts and
*only* where your intent is for the loop to continue from the
loop-continuation portion of the smallest enclosing iteration statement.
For example:

int main(void)
{
continue;
return 0;
}

is a constraint violation requiring a diagnostic message. Furthermore, the
following example illustrates *legal* C where the use of continue to
indicate a null statement gives the wrong code for the job:

#include <stdio.h>

int main(void)
{
int ch;
while((ch = getchar()) != EOF)
{
if(isalpha(ch))
{
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
{
continue; /* bad idea */
}
else
{
/* consonant-specific processing - non-aeiou */
}
/* letter-specific processing - for all a-z */
}
}
return 0;
}

In this code, vowels will *not* receive letter-specific processing.

So no, using it to indicate an empty statement is not a good idea. Using it
to indicate an empty loop body is obviously fine, however.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 22 '08 #5
mdh wrote:
May I ask the group this somewhat non-focused question....hav ing 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.
I have seen `continue` used sensibly [1] so rarely
that I treat any use of it
as a code smell
suggesting
that there is an opportunity
to fix the design
in a way that eliminates any need
to consider continue for that code.

To indicate an empty loop body
I find `{}` perfectly adequate
on those odd occasions
when it is needed.

Mind the gap.

[1] From my POV, natch.

--
'It changed the future .. and it changed us.' /Babylon 5/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Sep 22 '08 #6
In article <f4************ *************** ***@bt.com>,
Richard Heathfield <rj*@see.sig.in validwrote:
>Your example is fine, but your description is not, because you can't always
use continue to indicate an empty statement - *only* in loop contexts and
*only* where your intent is for the loop to continue from the
loop-continuation portion of the smallest enclosing iteration statement.
Unlike Fortran, where CONTINUE was a no-op, and was often used at the
end of loops because it could reliably be used to hang the line number
on.

-- Richard
--
Please remember to mention me / in tapes you leave behind.
Sep 22 '08 #7
Chris Dollin <ch**********@h p.comwrites:
mdh wrote:
>May I ask the group this somewhat non-focused question....hav ing 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.

I have seen `continue` used sensibly [1] so rarely
that I treat any use of it
as a code smell
suggesting
that there is an opportunity
to fix the design
in a way that eliminates any need
to consider continue for that code.

To indicate an empty loop body
I find `{}` perfectly adequate
on those odd occasions
when it is needed.

Mind the gap.

[1] From my POV, natch.
Actually it makes perfect sense when you have your gate condition at the
top of the section - far easier to read, understand and maintain in many
cases (not all of course).

while(n--){

if(complexCondi tionNotMatched( n))
continue; /* not interested. Check the next if there is one. */

/*do processing of things we are interested in*/

}

Now we could invert the test but then we need to scroll down to the end
of the function to be see what else happens after the block. Here we see
that nothing happens at all - we must move to the next element.

Seems clear, maintainable and decent design to me. I fail to see how it
could be construed as "bad" in any shape or form.

Sep 22 '08 #8
mdh wrote:
May I ask the group this somewhat non-focused question....hav ing 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.
Hi, my name is Eric, and I'm a `continue' abuser. I use
it as a disguised `goto', simply to avoid excessive nesting:

for (...) {
... prepare for a test ...
if (not_applicable )
continue;
... prepare another test ...
if (not_applicable _2)
continue;
... payload ...
}

Inverting the sense of the tests would eliminate my abusive
`continue' statements:

for (...) {
... prepare for a test ...
if (! not_applicable) {
... prepare another test ...
if (! not_applicable_ 2) {
... payload ...
}
}
}

.... but if there are three or four such tests, as often
happens when you're converting and validating input from
an uncontrolled source, the "payload" -- the real purpose
of the function --
tends to get squee-
zed into short
little lines all
crowded at the right-
hand side and larded
with ugly line bre-
aks.

So although I know in my heart of hearts that I overuse
`continue' and really shouldn't, the way I really shouldn't
drink and smoke and gamble on cockfights, I, er, continue in
my disgusting habit. There, I've admitted my guilty secret.
They say it's the first step to recovery, but only time will
tell.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Sep 22 '08 #9
Richard<rg****@ gmail.comwrites :
Chris Dollin <ch**********@h p.comwrites:
>mdh wrote:
>>May I ask the group this somewhat non-focused question....hav ing now
seen "continue" used in some of the solutions I have worked on.
<snip>
>I have seen `continue` used sensibly [1] so rarely
that I treat any use of it
as a code smell
suggesting
that there is an opportunity
to fix the design
<snip>
>[1] From my POV, natch.

Actually it makes perfect sense when you have your gate condition at the
top of the section - far easier to read, understand and maintain in many
cases (not all of course).

while(n--){

if(complexCondi tionNotMatched( n))
continue; /* not interested. Check the next if there is one. */

/*do processing of things we are interested in*/

}

Now we could invert the test but then we need to scroll down to the end
of the function
You mean "loop" presumably here.
to be see what else happens after the block. Here we see
that nothing happens at all - we must move to the next element.
There is, of course, no need to invert the test though many people
really seem to object to an empty if branch -- an empty else is fine
but an empty if seems to be OTT for most people.

It is unfortunate that your example is already negative because, to my
mind, there is slightly more clarity in:

while (n--) {
if (complexConditi onMatched(n)) {
}
}

but that is just an accident of choosing a negative condition to start
with.
Seems clear, maintainable and decent design to me. I fail to see how it
could be construed as "bad" in any shape or form.
Clear and decent yes, but not 100% maintainable because you loose a
place to put code executed at the end of every loop. I honestly don't
know if this matters (I have no metric for maintainability ) but people
site less complex matters are being an issue for maintainability so I
will suggest this one.

I almost never use continue but I don't think this is because I want
to write super maintainable code, just that to me the "if in a loop"
form seems to say what I mean more clearly in all the cases that have
come up.

--
Ben.
Sep 22 '08 #10

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

Similar topics

3
580
by: Harman Sahni | last post by:
As per this URL http://msdn.microsoft.com/library/en-us/vjref98/html/14_14.asp?frame=true conitnue works on for, while, do... I know it works for foreach as well as I'm using it somewhere. My question is how do I "continue" (jump an iteration) of an outermost loop from an inner loop. So I have something like this:
14
3631
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) == '-' ) {
13
1993
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
0
7912
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...
0
8202
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. ...
1
7959
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...
0
6614
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...
1
5710
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...
0
5390
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...
0
3837
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...
1
2345
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
0
1180
bsmnconsultancy
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...

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.