# If statement best practice

**URL:** <https://forum.arduino.cc/t/if-statement-best-practice/1217879>\
**Category:** Programming\
**Created:** [January 30, 2024, 3:31pm UTC](https://forum.arduino.cc/t/if-statement-best-practice/1217879 "2024-01-30T15:31:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![arpa123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/arpa123/32/1098836_2.png) [@arpa123](https://forum.arduino.cc/u/arpa123)\
**Post date:** [January 30, 2024, 3:31pm UTC](https://forum.arduino.cc/t/if-statement-best-practice/1217879/1 "2024-01-30T15:31:39Z")

</div>

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**

```arduino

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

```

or

```arduino

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

```

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [January 30, 2024, 3:34pm UTC](https://forum.arduino.cc/t/if-statement-best-practice/1217879/2 "2024-01-30T15:34:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Coding\_Badly](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Coding\_Badly](https://forum.arduino.cc/u/Coding_Badly)\
**Post date:** [January 30, 2024, 4:09pm UTC](https://forum.arduino.cc/t/if-statement-best-practice/1217879/3 "2024-01-30T16:09:47Z")

</div>

The key phrase is [short circuit evaluation](https://www.google.com/search?client=firefox-b-1-d&q=short+circuit+evaluation).

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [January 30, 2024, 5:01pm UTC](https://forum.arduino.cc/t/if-statement-best-practice/1217879/4 "2024-01-30T17:01:17Z")

</div>

> [@J-M-L](#):
>
> 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

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

> [@J-M-L](#):
>
> the language states that a boolean expression stops its evaluation when the answer is known,

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

so for example

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

```

can cause an exception

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [January 30, 2024, 5:13pm UTC](https://forum.arduino.cc/t/if-statement-best-practice/1217879/5 "2024-01-30T17:13:47Z")

</div>

> [@gcjr](#):
>
> i thought there's no guarantee which condition is evaluated first
> 
> so for example
> 
> ```arduino
> if (null != p && *p == 123)
> 
> ```
> 
> can cause an exception

whilst general expressions [can be evaluated in any order](https://en.cppreference.com/w/cpp/language/eval_order), it's not the case for && and ||. These operators are said to be [short-circuiting](https://en.wikipedia.org/wiki/Short-circuit_evaluation)

[https://en.cppreference.com/w/cpp/language/operator\_logical](https://en.cppreference.com/w/cpp/language/operator_logical)

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

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [January 30, 2024, 5:13pm UTC](https://forum.arduino.cc/t/if-statement-best-practice/1217879/6 "2024-01-30T17:13:54Z")

</div>

> [@gcjr](#):
>
> i thought there's no guarantee which condition is evaluated first

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

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [January 30, 2024, 5:23pm UTC](https://forum.arduino.cc/t/if-statement-best-practice/1217879/7 "2024-01-30T17:23:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [January 30, 2024, 5:25pm UTC](https://forum.arduino.cc/t/if-statement-best-practice/1217879/8 "2024-01-30T17:25:08Z")

</div>

it was working in the same way in C, There is a [sequence point](https://en.cppreference.com/w/c/language/eval_order) after the evaluation of the first element and if the answer is known, then the second part is not evaluated

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [January 30, 2024, 5:27pm UTC](https://forum.arduino.cc/t/if-statement-best-practice/1217879/9 "2024-01-30T17:27:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [January 30, 2024, 5:33pm UTC](https://forum.arduino.cc/t/if-statement-best-practice/1217879/10 "2024-01-30T17:33:22Z")

</div>

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 `||`

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [January 30, 2024, 5:39pm UTC](https://forum.arduino.cc/t/if-statement-best-practice/1217879/11 "2024-01-30T17:39:54Z")

</div>

> [@J-M-L](#):
>
> 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

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [January 30, 2024, 5:41pm UTC](https://forum.arduino.cc/t/if-statement-best-practice/1217879/12 "2024-01-30T17:41:19Z")

</div>

> [@gcjr](#):
>
> 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

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [January 30, 2024, 5:41pm UTC](https://forum.arduino.cc/t/if-statement-best-practice/1217879/13 "2024-01-30T17:41:52Z")

</div>

> [@gcjr](#):
>
> so for example
> 
> ```arduino
> if (p != null && *p == 123)
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [January 30, 2024, 5:43pm UTC](https://forum.arduino.cc/t/if-statement-best-practice/1217879/14 "2024-01-30T17:43:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [January 30, 2024, 5:44pm UTC](https://forum.arduino.cc/t/if-statement-best-practice/1217879/15 "2024-01-30T17:44:58Z")

</div>

> [@J-M-L](#):
>
> There is a [sequence point](https://en.cppreference.com/w/c/language/eval_order) after the evaluation of the first elemen

# 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

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [January 30, 2024, 5:47pm UTC](https://forum.arduino.cc/t/if-statement-best-practice/1217879/16 "2024-01-30T17:47:25Z")

</div>

Yes as I said

> [@J-M-L](#):
>
> whilst general expressions [can be evaluated in any order](https://en.cppreference.com/w/cpp/language/eval_order),

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

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [January 30, 2024, 5:49pm UTC](https://forum.arduino.cc/t/if-statement-best-practice/1217879/17 "2024-01-30T17:49:38Z")

</div>

> [@J-M-L](#):
>
> this does not apply to && and ||

but what other type of multiple condition statements are there?

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

?? '==", '!='

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [January 30, 2024, 5:52pm UTC](https://forum.arduino.cc/t/if-statement-best-practice/1217879/18 "2024-01-30T17:52:03Z")

</div>

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

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

a7

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [January 30, 2024, 5:55pm UTC](https://forum.arduino.cc/t/if-statement-best-practice/1217879/19 "2024-01-30T17:55:20Z")

</div>

> [@gcjr](#):
>
> what other type of multiple condition statements are there

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.

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [January 30, 2024, 6:00pm UTC](https://forum.arduino.cc/t/if-statement-best-practice/1217879/20 "2024-01-30T18:00:58Z")

</div>

> [@J-M-L](#):
>
> > [@J-M-L](#):
> >
> > whilst general expressions [can be evaluated in any order](https://en.cppreference.com/w/cpp/language/eval_order),
> 
> this does not apply to && and ||

> [@J-M-L](#):
>
> None indeed,

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

[Next page](https://forum.arduino.cc/t/if-statement-best-practice/1217879.md?page=2)
