I'd like to know (just for knowing) if there is a way to execute the following instructions with simpler code: (C++)
if(a > b && a > c && a > d)
is it possible to replace it with something like this:
if(a > b, c, d)
I'd like to know (just for knowing) if there is a way to execute the following instructions with simpler code: (C++)
if(a > b && a > c && a > d)
is it possible to replace it with something like this:
if(a > b, c, d)
Use std::max(std::initializer_list<T>)
from the <algorithm>
header like follows:
#include <iostream>
#include <algorithm>
int main()
{
if(4 > std::max({2,3,6}))
std::cout << "greater\n";
else
std::cout << "not greater\n";
}
When you code if(a > b, c, d)
you are using the comma operator (and you don't want to).
I don't understand what you are exactly asking. In all cases, the computer has (sometimes) to do three compares. Why can't you spell them all?
You could perhaps use fancy preprocessor tricks, but in your particular case, you should not.
Of course, the test of an if
could be a long expression which takes several lines. It is acceptable and common to write something like
if (a > b
&& a > c
&& a > d
&& somelongandcomplexcondition(a,b,c)
&& a*a > 34)
Notice that Ruslan's answer is computing the max. You might not want to, in particular if b
and c
are long and complex sub-expressions with side effects.
Think for example of a test like if (c > 1 && a > 1 && a > b && a > b/c)
; you rely on the lazy "and then" evaluation of &&
to avoid dividing by zero.
If you want to learn more about C11, see some C reference and refer to its standard n1570.
If you want to learn more about C++11, see some C++ reference and refer to its standard n3337 (or some other, younger standard like C++17).
Both C and C++ define precisely what an if
statement can be.
Don't confuse C and C++, they are different programming languages, and both are specified in their standard specification. Some compilers, notably GCC, provide extensions to them. It is your responsibility to decide to use compiler-specific extensions or to stick to the language standard (and hope that your code would be compilable by many compilers respecting that standard).
In some particular cases, your tests (or your C or C++ code) are so long and so repetitive that you could consider some metaprogramming approach: you'll then write some script (or some metaprogram) to emit C (or C++) code in a file (and you could later compile that generated file, or #include
it, etc.). There are several examples of C or C++ code generators (e.g. bison, SWIG, etc...) that might inspire you. You could also use some generic preprocessor like GPP or m4, or have your awk or python script (or your other C++ program) generate some C or C++ file, etc... Of course, you will configure your build automation (e.g. your Makefile
) for such particular cases.
One way to make it simpler is to put the values into an array and sort them. Since you didn't give a concrete example, I can't say whether it would help for your case, but when you're dealing with a sorted array, you can do things like:
int i = 0;
while ((a < array[i]) && (i < kMaxElements))
{
i++;
}
At the end, i
will either be equal to kMaxElements
, or it will be the index of the item that is equal to or greater than a
.
Better yet, you can do a binary search the array to find a specific element. In C++ there are specific algorithms for this in the <algorithm>
tools. See specifically, binary_search()
, lower_bound()
, and upper_bound()
.