Do && and || cause branching?

Dear all,

I read in the c++ tutorial that when the expression b1 && b2 is evaluated, with b1 and b2 two boolean expressions, b2 is not evaluated if it is not necessary

http://www.cplusplus.com/doc/tutorial/operators/

for example, in the following code:

1
2
3
4
5
6
bool very_complex_function(int x) {...}

bool makeand(int a, int b)
{
return (a>0) && very_complex_function(b);
}


if a <=0, the function very_complex_function(b) is not evaluated at all as it is not needed.

Does it mean that there is a branching whenever makeand is called, i.e. as if it was written as follows?

1
2
3
4
5
6
7
bool makeand(int a, int b)
{
  if (a<=0)
   return 0;
  else
    return very_complex_function(b);
}



Thank you all,
Panecasareccio.

Yes, since you're instructing the program to take different execution paths based on a condition, you will typically see some sort of a conditional branch CPU instruction in the resulting binary.


Topic archived. No new replies allowed.