If statement best practice

what is best practice and "consume low resources "

in first aproch will the "millis() - ms_last_mp3Play > 10000" be executed or will not even be checked if ms_last_mp3Play =0


  if (ms_last_mp3Play != 0 && millis() - ms_last_mp3Play > 10000) { 
    digitalWrite(NPN_mp3,0); 
  }

or



  if (ms_last_mp3Play != 0 ) { 
    if(millis() - ms_last_mp3Play > 10000){
    digitalWrite(NPN_mp3,0) 
    }
  }

the language states that a boolean expression stops its evaluation when the answer is known, so if you have to evaluate (A && B) and A is false, then B is not evaluated

1 Like

The key phrase is short circuit evaluation.

in the above case, is the question about minimizing cycles by avoid the more complex evaluation?

i thought there's no guarantee which condition is evaluated first

so for example

if (null != p && *p == 123)

can cause an exception

whilst general expressions can be evaluated in any order, it's not the case for && and ||. These operators are said to be short-circuiting

https://en.cppreference.com/w/cpp/language/operator_logical

(overloaded operators behave like regular function calls and always evaluate both operands)

Now you know. If it weren't the case, it would make exploiting it impossible.

It may not fit into someone's idea of good code, but it is def a thing that can come in handy.

a7

was this something true in C or made more specific in C++?

it was working in the same way in C, There is a sequence point after the evaluation of the first element and if the answer is known, then the second part is not evaluated

you are telling me the multiple conditions are always evaluated from left to right in the order that they appear in the source code

if you don't consider overloaded operators and just focus on && and || then yes.

if you have a sequence of those then you need to look at the evaluation graph knowing that && has higher precedence than ||

this seems only relevant to a 3 condition statemen such as
if (a == 3 || b == 2 && c == 1)

but my question is about the order conditions are processed in statement such as
if (a == 3 && b ==2)
or
if (a == 3 || b == 2)
you're saying the conditions involving 'a' are always evaluated first because they are the first condition in the source code

yes a==3 will always be evaluated first for its truth value
with the &&, if it's false, b==2 won't be checked
with the ||, if it's true, b==2 won't be checked

is a perfect example of how short circuiting is used.

And if you don't like it from a style point of view, that is another matter.

a7

Many languages have it. Know that Nick didn't want it in Pascal, so you have to watch out when you think it might be universally seen as a good thing.

Oops! Evidently Pascal does have it, I wonder if that was added at some point, I would swear there was a time (or was it another legit mainstream language) that it did not.

a7

Order of evaluation

Order of evaluation of the operands of any C operator, including the order of evaluation of function arguments in a function-call expression, and the order of evaluation of the subexpressions within any expression is unspecified (except where noted below). The compiler will evaluate them in any order, and may choose another order when the same expression is evaluated again.

There is no concept of left-to-right or right-to-left evaluation in C

it is clear that there is no point in further evaluation if the conditions are ANDed and the first evaluated condition is false

Yes as I said

this does not apply to && and || for basic types (overloaded is a different story)

but what other type of multiple condition statements are there?

if ((a == 4) ?? (b==2))

?? '==", '!='

OK, I love the internet. No matter right or wrong, you can always find someone who you think knows better that agrees with you. :expressionless:

Pascal didn’t define whether AND and OR use short circuit evaluation or not, giving you the worst of both worlds.

a7

None indeed, the only other logical operator is NOT but has only one argument

The conditional operator (ternary) is just like selection statements, you provide a condition to be evaluated.

so when does it -- Order of Evaluation (or lack of) -- apply?

it seems Order of Evaluation and "short circuiting" are two independent concepts

regardless of Order of Evalulation, it make no sense to evaluate the 2nd condition (in any order) if the first one evaluated is false and the 2nd condition is ANDed