0

I have a question regarding how and in which order if() checks multiple conditions. I would like to call functions directly in the if statement. In an if statement like this:

if (condition1 || function1)
        return SOMETHING;

The first part of the logical expression will be checked, so if condition1 == 1. If this proved to be true, the whole statement is true, therefore the function function1 will not be executed, am I right?

What happens with something like this:

if (!(condition1 && function1))
        return FAIL;

Will the compiler turn this into:

if (!condition1 || !function1)
        return FAIL;

And not execute function1 if !condition1 turns out to be true? Or will it first check condition1 and execute function1 because of the AND operator and than negate the result? What if I switch the order?

  • I am not sure but I don't think the compiler should change the original conditional expression. But probably depends on the compiler. – kiner_shah Sep 29 '21 at 10:25
  • So normally it would lazy evaluate the && and after that negate the result? –  Sep 29 '21 at 10:28
  • Not sure why this question is marked as duplicate.. – kiner_shah Sep 29 '21 at 10:30
  • @Psoke always left to right and short circuit. The dupe explain everything – 0___________ Sep 29 '21 at 10:32
  • 1
    The 2 last `if`s are equal. For `condition1 == true` `function1` will be evaluated, for `condition1 == false` not. So, the compiler is free to change it, but I don't think it is an optimisation. – mch Sep 29 '21 at 10:32
  • I think the question was whether compiler transforms a conditional expression either into a simplified expression or for better optimized code. – kiner_shah Sep 29 '21 at 10:32
  • @Psoke there is no difference in last two. 1. If condition1 == false the whole is true. 2. condition1 == false the whole is true. – 0___________ Sep 29 '21 at 10:36
  • So if the compiler simplifies expressions, logically they stay the same (duh). And the definitions for evaluating the expressions (like the lazy evaluation) are made in a way, that it doesnt matter? Other than changing the condition order for example? –  Sep 29 '21 at 10:40
  • The evaluation of your conditions is clearly defined. Whatever a compiler does must exactly produce the same result as if nothing was modified at all. All observable side effect must be identical. Otherwise an optimization is not valid. – Gerhardh Sep 29 '21 at 10:49
  • @Psoke "Evaluation order" is a slippery concept. There's nothing special about `if` statements here; the same questions can come up anywhere an expression is evaluated. Normally, the evaluation order is unspecified — compilers can and do evaluate expressions in any order they want to, as long as they get the correct result. If I write `f() + g() * h()`; the compiler can arrange to call functions in any order. But the `&&` and `||` operators are special: (1) they *do* guarantee left-to-right evaluation and (2) they do skip the second part altogether if the first part determines the answer. – Steve Summit Sep 29 '21 at 11:06
  • See [question 3.6](http://c-faq.com/expr/shortcircuit.html) in the [C FAQ list](http://c-faq.com/). – Steve Summit Sep 29 '21 at 11:11

0 Answers0