Page 1 of 1
Contents of an OR statement
Posted: Tue Oct 14, 2025 5:48 pm
by Billduck1302
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.
Re: Contents of an OR statement
Posted: Tue Oct 14, 2025 7:07 pm
by chipfryer27
Hi
Leigh and Steve explain it well in this post
https://www.flowcode.co.uk/forums/viewt ... 337#p21337
Hope this helps
Re: Contents of an OR statement
Posted: Wed Oct 15, 2025 9:38 am
by Steve-Matrix
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)".