when both the function1 and function2 return bool values, but also modify universal variables?
I know it would be possible to do something like:
1 2 3 4 5 6 7
bool condition_met = false;
if (function1() )
condition_met = true;
if (function2() )
condition_met = trueif (condition_met)
yada_yada_yada();
if you want the global variables in question to be modified each time. A nested if statement won't do it.
but how does c++ evaluate if (function1() || function2())? As soon as it evaluates function1(), if it returns true, that will be enough for the whole expression to be true, so will it even evaluate the function2() at all?
I know this would be easy to test but I'm not at a computer at the moment with a compiler.
As soon as it evaluates function1(), if it returns true, that will be enough for the whole expression to be true
Yes. If function1 returns true, then function2 would not be called. (unless the || operator is overloaded, but that's another story)
Personally, I avoid compounding statements like this for this very reason. The exact behavior isn't all that clear and it can be a nasty source of bugs.
I would do something like your 'condition_met' approach. That is the most clear, and best approach IMO.
But for fun, here are some other ways to compact the statement and ensure that both functions are called:
1 2 3 4 5 6 7 8 9 10 11 12
if(function1() + function2())
{
// since true == 1, this will result in a nonzero value if either function returned true,
// and will result in zero if both returned false.
}
if(function1() | function2())
{
// use of the binary OR operator | (instead of the logical OR operator ||)
// ensures that both functions will be called
}
Disch I like your examples. My question is though those 2 examples you provided, are they similar to inclusive or exclusive or?
if(function1() + function2())
would this expression only be true if 1, but not both function1 and function2 are true? I forget the exact meanings of what is true and what is false, if true means any nonzero positive interger then I guess it would be inclusive or. but if true means == 1 and nothing more, i guess it would be exclusive or.