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)cybers pace.org | don't, I need to know. Flames welcome. 4 2704
In article <bo**********@c hessie.cirr.com >,
Christopher Benson-Manica <at***@nospam.c yberspace.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(f oo);
foo++;
if(baz(foo))
do_stuff_with(f oo); /*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.c yberspace.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;f oo<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;fo o++) {
int barval=bar()+fo o;
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.c yberspace.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[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof 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. Inside (or just after) the same query that searches for
available seats, I need to SIMULTANEOUSLY mark those seats as "on
hold".
I've only read...
|
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).
TABLE matchResults
cont_id team_id contest_result
1 1 3
1 2 5
|
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 record in the second table and call
the update method of the data adapter the command builders update command
text is for the first table. Can the...
|
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
the user can change the date, which will force a requery in the
subform to bring up records from the date selected.
My question is this... The...
| |
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 compare these two objects so that it will return true if both
object's members have the same value...
it is good if u can give me a single function...
|
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 became
realllllllllllly slow. Content in LoginView Role Groups was not displaying
even after a user in a role had logged in.
It was taking about...
|
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 w1 and w2 make up the
first string and, w3 and w4 make up the second string. I do not want
to allocate memory, then put the words together to create...
|
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 dimensional arrays from
std::vectors ??? I want to use normal Array Syntax.
|
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...
|
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...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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...
|
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...
|
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...
|
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 we have to send another system
| |
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...
| |