I have an if statement "If Cell_1_Voltage_Float || Cell_2_Voltage_Float <= 3.0".
The intent was to terminate a process, if either cell, in a Nissan Leaf battery, fell below 3 volts, in a discharge capacity test.
Well, it did not work. Instead the following "If Cell_1_Voltage_Float <= 3.0 || Cell_2_Voltage_Float <= 3.0" did work. Thank you.
I am wondering how the processor interpreted the first statement.
Contents of an OR statement
-
- Posts: 23
- http://meble-kuchenne.info.pl
- Joined: Fri Dec 06, 2024 4:41 pm
- Has thanked: 1 time
- Been thanked: 1 time
-
- Valued Contributor
- Posts: 1745
- Joined: Thu Dec 03, 2020 10:57 am
- Has thanked: 386 times
- Been thanked: 597 times
Re: Contents of an OR statement
Hi
Leigh and Steve explain it well in this post https://www.flowcode.co.uk/forums/viewt ... 337#p21337
Hope this helps
Leigh and Steve explain it well in this post https://www.flowcode.co.uk/forums/viewt ... 337#p21337
Hope this helps
-
- Matrix Staff
- Posts: 1595
- Joined: Sat Dec 05, 2020 10:32 am
- Has thanked: 222 times
- Been thanked: 373 times
Re: Contents of an OR statement
It often helps to use brackets to describe your intent in these situations. Experienced programmers will often write code that doesn't use brackets and assumes anyone reading the code will understand the natural order of operators within C. But with the addition of a few brackets, it means even novice programmers should be able to understand what's intended.
I think your initial expression is trying to say "if either cell1 or cell2 is less than or equal to 3". We understand this in English, but the chip/computer will interpret this in stages. Just "if cell1" will be evaluated first, which will be true unless cell1 is zero. If it is true, then is will treat the whole expression as true and will not bother with the secondary "if cell2 <= 3".
If you put brackets around this like "if (cell1 || cell2) <= 3.0)" then this will also not work because "cell1 || cell2" will resolve first to either "true" or "false" (i.e. 1 or 0), and this will always be less than 3 and so the whole expression will always be true.
As you have found, the "correct" way to describe your intent in the code is to say "if (cell1 is less than 3) or (cell2 is less than 3)".
I think your initial expression is trying to say "if either cell1 or cell2 is less than or equal to 3". We understand this in English, but the chip/computer will interpret this in stages. Just "if cell1" will be evaluated first, which will be true unless cell1 is zero. If it is true, then is will treat the whole expression as true and will not bother with the secondary "if cell2 <= 3".
If you put brackets around this like "if (cell1 || cell2) <= 3.0)" then this will also not work because "cell1 || cell2" will resolve first to either "true" or "false" (i.e. 1 or 0), and this will always be less than 3 and so the whole expression will always be true.
As you have found, the "correct" way to describe your intent in the code is to say "if (cell1 is less than 3) or (cell2 is less than 3)".