Neater Way Of Testing If A Variable Is Between Two Constants In If ...
Maybe your like
Just debugging my program, i've written this;
if (0 < columnNumber < 3) { sectorNumber = 0; }Which is obviously wrong as it doesn't work. It made sense in my head when i wrote it but the processor is having none of it.
My fix is this;
if (columnNumber > 0 && columnNumber < 3) { sectorNumber = 0; }My question is; is there a neater way to write it, thats closer to the first one?
Thanks in advance, Ben.
Peter_n June 3, 2015, 10:24pm 2But the second one is neat ! That is normal 'c' programming.
There is no "check if in between" condition in the 'c' language. Comparing two values is normal, and you have to do that twice when you want to check if it is in between two values. That's how it is, and it's not so bad at all, because it is very straightforward for a programmer and a compiler.
Benny_H88 June 3, 2015, 10:36pm 3Thanks, i was just sure i had seen something similar to my original statement before.
Benny_H88 June 3, 2015, 10:40pm 4Take the following statement
if ( x > 5){ //Do stuff }suppose x = 5.
is the stuff done or not?
Peter_n June 3, 2015, 10:49pm 5It is possible to compare integers, but also float numbers.
int x = 5; if ( x > 5 ) { // Do stuff }The stuff is not done. Only if 'x' is really greater than 5. So 'x' must be 6,7,8,9,10, and so on.
That is why the '>=' often is used (greater or equal than).
With float numbers it is like this:
float z = 5.001; if( z > 5.0) { // Do stuff }Here the 5.001 is greater than 5.0, so the stuff is executed.
robtillaart June 4, 2015, 4:46pm 6comparison can visually show it is between or outside
if (0 <= x && x <= 3) // between if (y < 0 || 10 < y) // outsideor you create a macro (like min, max etc) but be aware all culprits of macros apply...
#define between(x, a, b) (((a) <= (x)) && ((x) <= (b))) #define outside(x, a, b) (((x) < (a)) || ((b) < (x)))Note I interpreted between as between inclusive and outside as outside exclusive. This makes the two macros complementary.
Benny_H88 June 4, 2015, 7:52pm 7Thanks for the responses, very informative
Related topics
| Topic | Replies | Views | Activity |
|---|---|---|---|
| Comparison operator range Programming | 9 | 802 | October 23, 2022 |
| How to define a range in an 'If' statement Programming | 7 | 13450 | May 5, 2021 |
| Stato compreso tra Software | 2 | 2102 | May 7, 2021 |
| How to use if between two values? Programming | 7 | 53612 | May 5, 2021 |
| compare statement Programming | 9 | 799 | May 5, 2021 |
Tag » Arduino If Statement Between Two Values
-
How To Use If Between Two Values? - Arduino Forum
-
If Statement That Tests If A Value Is In Range? - Arduino Forum
-
If - Arduino Reference
-
How To Define A Range In An 'If' Statement - Arduino Forum
-
How To Make Two Conditions In An If Statement - Arduino Forum
-
Switch (case) Statement, Used With Sensor Input
-
How To Write IF/Else Using A Threshold Range Of Values
-
Arduino: If Else Statement, If All Numbers Are In Range, If At Least One ...
-
Arduino Comparison Operators - Linux Hint
-
Arduino Code: Conditional Statements - YouTube
-
[PDF] Arduino If Statement With Multiple Conditions
-
Arduino If-else Statement - Linux Hint
-
Arduino IDE: Conditional(if-else-if) Statements - STEMpedia
-
Tutorial 11: If Statement (and Else-if), Comparison Operators And ...