I have a situation something like this:
int foo;
for( foo=bar() ; foo <= bar()+1 ; foo++ ) {
if( !baz(foo) ) break;
/* do stuff with foo */
}
The idea is that the loop happens at most twice - once for foo=bar(), and once
for foo=bar()+1. If baz(foo) fails, I know I can stop. However, I don't
think this is the "elegant" way to get this behavior - there is a break (which
might be bad style, for some), and bar() may be called three times (which
doesn't seem optimal). I thought of
int foo=bar();
do {
if( !baz(foo) ) break;
/* doo stuff with foo */
} while( foo++ == bar() );
, but it's no better (and is somewhat more obfuscated, to boot). Any
thoughts?
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome. 4 2696
In article <bo**********@chessie.cirr.com>,
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote: I have a situation something like this:
int foo;
for( foo=bar() ; foo <= bar()+1 ; foo++ ) { if( !baz(foo) ) break; /* do stuff with foo */ }
The idea is that the loop happens at most twice - once for foo=bar(), and once for foo=bar()+1. If baz(foo) fails, I know I can stop. However, I don't think this is the "elegant" way to get this behavior - there is a break (which might be bad style, for some), and bar() may be called three times (which doesn't seem optimal). I thought of
int foo=bar();
do { if( !baz(foo) ) break; /* doo stuff with foo */ } while( foo++ == bar() );
, but it's no better (and is somewhat more obfuscated, to boot). Any thoughts?
How long is the chunk that "/*do stuff with foo*/" expands to?
If it's not too long, something like this might be a little bit cleaner
(for one thing, it makes it clear that it's a "do something again unless
baz() fails" and not a "loop until some condition is met"), though it's
not without its problems:
--------
foo=bar();
/*do stuff with foo*/
foo++;
if(baz(foo))
{
/*Note that this stuff is the same stuff we did above. If baz
returns nonzero we have to do it again.
*/
/*do stuff with foo*/
}
--------
Depending on how closely what you're doing with foo is tied to the rest
of the code there, it might be worth moving it to another function:
--------
foo=bar();
do_stuff_with(foo);
foo++;
if(baz(foo))
do_stuff_with(foo); /*again*/
--------
dave
--
Dave Vandervies dj******@csclub.uwaterloo.caYou can quit emacs?
Yes, and after 12 weeks of rehab, you might even stay off the stuff.
--James Riden and Greg Andrews in the scary devil monastery
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in
news:bo**********@chessie.cirr.com: I have a situation something like this:
int foo;
for( foo=bar() ; foo <= bar()+1 ; foo++ ) { if( !baz(foo) ) break; /* do stuff with foo */ }
The idea is that the loop happens at most twice - once for foo=bar(), and once for foo=bar()+1. If baz(foo) fails, I know I can stop. However, I don't think this is the "elegant" way to get this behavior - there is a break (which might be bad style, for some), and bar() may be called three times (which doesn't seem optimal). I thought of
int foo=bar();
do { if( !baz(foo) ) break; /* doo stuff with foo */ } while( foo++ == bar() );
, but it's no better (and is somewhat more obfuscated, to boot). Any thoughts?
If the value returned by bar() is loop invarient, assign it to a
variable. If bar() is an expensive (but invarient) computation this will
be a nice gain, and performing a single unnecessary check is at no cost.
You might do this:
int foo, startpos;
startpos=bar();
for (foo=startpos;foo<startpos+2 && !baz(foo);foo++) {
/* do stuff */
}
if you don't like the break. Personally, I don't mind "if (cond) break;"
especially at the beginning of loops.
If bar() isn't loop invarient and expensive, then you can still do it
nicely. something like:
int foo;
for (foo=0;foo<2;foo++) {
int barval=bar()+foo;
if (!baz(barval)) break;
/* do stuff */
}
If bar() isn't expensive and loop invarient, it doesn't really matter if
it's invoked a third.
Ian Woods
On 11/10/2003 10:13 AM, Christopher Benson-Manica wrote: I have a situation something like this:
int foo;
for( foo=bar() ; foo <= bar()+1 ; foo++ ) { if( !baz(foo) ) break; /* do stuff with foo */ }
The idea is that the loop happens at most twice - once for foo=bar(), and once for foo=bar()+1. If baz(foo) fails, I know I can stop. However, I don't think this is the "elegant" way to get this behavior - there is a break (which might be bad style, for some), and bar() may be called three times (which doesn't seem optimal). I thought of
int foo=bar();
do { if( !baz(foo) ) break; /* doo stuff with foo */ } while( foo++ == bar() );
, but it's no better (and is somewhat more obfuscated, to boot). Any thoughts?
If the value returned from bar() never changes, then of course you'd assign that
to a variable outside of the loop:
int foo;
int fooMin = bar();
int fooMax = fooMin + 1;
for( foo=fooMin ; foo <= fooMax ; foo++ ) {
if( !baz(foo) ) break;
/* do stuff with foo */
}
Instead of breaking out of the loop if baz() fails, the obvious change would be
to only do the work if baz() succeeds, e.g.:
int foo;
int fooMin = bar();
int fooMax = fooMin + 1;
for( foo=fooMin ; foo <= fooMax ; foo++ ) {
if( baz(foo) ) {
/* do stuff with foo */
}
}
or even:
int foo;
int fooMin = bar();
int fooMax = fooMin + 1;
for( foo=fooMin ; (foo <= fooMax) && baz(foo) ; foo++ ) {
/* do stuff with foo */
}
If the condition becomes complex, write a macro:
#define ISVALID(foo) ((foo) <= fooMax ? baz((foo)) : 0)
int foo;
int fooMin = bar();
int fooMax = fooMin + 1;
for( foo=fooMin ; ISVALID(foo) ; foo++ ) {
/* do stuff with foo */
}
Now, if bar() doesn't always return the same value, we can easily modify the
above to:
#define ISVALID(foo) ((foo) <= bar() + 1 ? baz((foo)) : 0)
int foo;
for( foo=bar() ; ISVALID(foo) ; foo++ ) {
/* do stuff with foo */
}
Regards,
Ed.
Christopher Benson-Manica <at***@nospam.cyberspace.org> writes: int foo;
for( foo=bar() ; foo <= bar()+1 ; foo++ ) { if( !baz(foo) ) break; /* do stuff with foo */ }
The idea is that the loop happens at most twice - once for foo=bar(), and once for foo=bar()+1. If baz(foo) fails, I know I can stop. However, I don't think this is the "elegant" way to get this behavior - there is a break (which might be bad style, for some), and bar() may be called three times (which doesn't seem optimal).
If I understand the situation correctly, try this:
for (foo = bar(); baz(foo) && foo <= bar() + 1; foo++) {
/* do stuff with foo */
}
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: SimonC |
last post by:
I'm looking to do something similar to a feature found on
Ticketmaster.com, where you select your seats at a venue, and then you
have two minutes in which to take or leave them.
QUESTION 1a....
|
by: John Grenier |
last post by:
Hi,
I have to determine the "standing" (WIN - TIE - LOSS) from
confrontations between two teams on a contest. The table matchResults
has fields cont_id, team_id and contest_result (int).
...
|
by: Willem |
last post by:
Hi,
I have a newbie question: is it possible to make a search form in asp that
searches in two different databases (access)?
Willem
|
by: Hank1234 |
last post by:
Can I use one Data Adapter and one Command Builder to update amny tables?
Currently in my data adapter I query two tables and fill them into two tables
in a data set. When I make a change to a...
|
by: Matt K. |
last post by:
Hi there,
I have a form in an Access project that contains a subform which
displays the results of a query of the style "select * from
where = #a certain date#". In the main part of the form...
| |
by: Prabhudhas Peter |
last post by:
I have two object instances of a same class...
and i assigned values in both object instances (or the values can be taken
from databse and assigned to the members of the objects)...
Now i want to...
|
by: clintonG |
last post by:
I applied aspnet_regsql to SQL2K which was working fine throughout Beta 2
development. After installing Visual Studio and SQL Express RTM my
application has blown up. Logging in to the application...
|
by: Steven |
last post by:
Hello,
I have a question about strcmp(). I have four words, who need to be
compared if it were two strings. I tried adding the comparison values
like '(strcmp(w1, w2) + strcmp(w3, w4))', where...
|
by: Peter Olcott |
last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a
I think that the operator() member function does not work correctly, does
anyone else know how to make a template for making two...
|
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...
|
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,...
| |
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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: 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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
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...
| |