4

Lets assume a simple If Statement with two conditions A and B:

If ( condA && condB) 

Is the Sequenz for all compilers the Same?

  1. condition A
  2. condition B

And is the execution of condition B therefore optional, in case condition A is already false?

Ernte1893
  • 183
  • 2
  • 11

1 Answers1

4

Yes. Not evaluating condition B if A is false is called short circuit logic, and this behavior is guaranteed by the language specification.

Ernte1893
  • 183
  • 2
  • 11
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 1
    have to add "only for built-in operators". If operators are redefined then short-circuiting is not happening and order of evaluation might be defined or not, based on what standard we talk about. Latest standard defines the order of function argument evaluation, earlier ones do not. – Swift - Friday Pie Mar 14 '20 at 00:09