If statements that call 2 functions in the evaluation expression

I was just wondering how does c++ implement the following code:

1
2
3
4
5
if ( function1() || function2() )
    {
         cout << "testing... " << " " << endl;
    }


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 = true
if (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.

Thanks anyone to answer my question.
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
}
I did something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int global;

bool f(int id, bool ret)
{
    cout << 'f' << id <<" was called!" << endl;
    global++;
    return ret;
}

int main()
{
    if (f(1,true)||f(2,true)) cout << "yo!" << endl;
    if (f(1,false)||f(2,true)||f(3,true)) cout << "yo!" << endl;
    return 0;
}

And the output was:

f1 was called!
yo!
f1 was called!
f2 was called!
yo!

EDIT: I didn't see at first the requirement that f must modify a global variable. I fixed it and the results were the same.
Last edited on
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.

anyhow how about this for exclusive or:
1
2
if ( (function1() + function2() ) == 1)


is that right?
true means any nonzero positive interger

Any (positive or negative) nonzero value is true.

However if you go the other way around (converting from bool to int) false is converted to 0 and true is converted to 1.

Thus, if f1() and f2() return bool,

inclusive or can be emulated like this:
f1()+f2(), which is the same as (f1()+f2())>0

exclusive or like this:
(f1()+f2())==1
(yeap, you got this one right!)

and like this:
(f1()+f2())==2

and not like this:
1-f1() or !f1()

EDIT: If you want to do this with more than 2 conditions (i.e. more than 2 functions) you can do it like this:

inclusive or:
f1()+f2()+f3()+...+fn(), which is the same as (f1()+f2()+f3()+...+fn())>0

and:
(f1()+f2()+f3()+...+fn())==n
Last edited on
Topic archived. No new replies allowed.