Clark Cox wrote:
On 2007-03-05 15:31:29 -0800, Pete Becker <p...@versatilecoding.com> said:
Gary Wessle wrote:
Hi
often I need to read numbers and only keep the highest or lowest.
so I do something like
int uLimit = 0;
int lLimit = 999999999999; //hoping the compiler will not complain
uLimit = val_read > uLimit ? val_read : uLimit;
lLimit = val_read < lLimit ? val_read : lLimit;
how do I choose the original lLimit?
Use the first value of val_read for both limits.
I would tend to disagree, as that would require a special case for the
first iteration of the loop.
i.e. I would prefer A to B:
Err, they do two different things. And, of course, they're written to
make A look better.
/*A*/
int maxValue = std::numeric_limits<int>::min();
int minValue = std::numeric_limits<int>::max();
while( ... )
{
int input = getNextValue();
maxValue = std::max(maxValue, input);
minValue = std::min(minValue, input);
...
}
/*B*/
int input = getNextValue();
int maxValue = input;
int minValue = input;
do
{
input = getNextValue();
maxValue = std::max(maxValue, input);
minValue = std::min(minValue, input);
}
while(...);
Let me present two versions that are biased the other way:
/*A*/
maxValue = std::numeric_limits<int>::min();
minValue = std::numeric_limits<int>::max();
while (...)
{
int input = getNextValue();
maxValue = std::max(maxValue, input);
minValue = std::min(minValue, input);
...
}
/*B*/
maxValue = minValue = getNextValue();
while (...)
{
int input = getNextValue();
maxValue = std::max(maxValue, input);
minValue = std::min(minValue, input);
...
}
Obviously B is vastly superior to the wordy, verbose, lengthy,
overwrought, wordy, redundant, and repetitive A. <g>
Even with this rewrite, I would still prefer A. I'd prefer not to do
(which was my main point; poorly stated as it was :) ). If all the