Go Down

Topic: Problem with if-else ladder (Read 1 time) previous topic - next topic

pantonvich

sorry about transposing: val > 312

but if it's checked before (as in the example given) then it's useless to check again


robtillaart


unless the type of val is volatile ....  (sorry to make it difficult)

if the else part was missing the first condition is mandatory
Code: [Select]

if(val<=312) tem=map(val,0,312,700,400);
if(312 > val && val<=455) tem=map(val,312,455,400,300);
if(455 > val && val<=612) tem=map(val,......
etc

but the code of sixeyes is optimized => more efficient as the number of tests is minimized
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Aditya Patadia

I just removed all "else" statements ad everything is working cool now. I think if(x<val<=y) type of condition checking is supported in arduino. I also tried other methods and they worked as well.. Thanks guys....

sixeyes

Quote
I think if(x<val<=y) type of condition checking is supported in arduino


If you really want this check you can write it as:

Code: [Select]

if (x < val && val <= y)


The code you wrote was a mathematical expression but in C/C++ (as used on the arduino) you have to use binary expressions (i.e. only two values to an operator. So to implement what you want needs three bits (x < val), (val <= y) and &&. The && is C's logical and operator combining the two comparisons.

BTW the code I posted in reply #3 is much more efficient that your original (see below) as you are performing many more comparisons that is necessary. In the first else if you have already performed the 312 < val in the preceding if (as val <= 312 is the opposite of 312 < val) so this part of the expression will always be true if it is executed (a waste of clock cycles and code memory). It is the same for each else if. This final else if if should be replaced by an else as the analog read will produce a value between 0 - 1023 (so the test in this expression will always be true).

As a side issue: if val was not limited to the range 0 - 1023, for values outside this range tem would be uninitialised. When I initialise a variable in a if/else if ladder, I always have an else or initialise the variable before the first if.

Code: [Select]

  if (val <= 312)
    tem = map(val, 0, 312, 700, 400);
  else if(312 < val && val <= 455)
    tem = map(val, 312, 455, 400, 300);
  else if (455 < val && val <= 612)
    tem = map(val, 455, 612, 300, 200);
  else if (612 < val && val <= 745)
    tem = map(val, 612, 745, 200, 100);
  else if(745 < val && val <= 1023)
    tem = map(val, 745, 1023, 100, 0);


Hope that helps.

Iain

robtillaart


Quote
if(x<val<=y)


As sixeyes said this is a correct mathematical expression but is doesn't do what you want.

try this sketch
Code: [Select]
void setup()
{
  Serial.begin(115200);
  int val = 100;
 
  if (10 < val <= 50) Serial.println("BUG");
  else Serial.println("OK");
}
void loop(){}


It prints BUG because the precedence/order of comparison operators is from left to right and the outcome of a compression is either 0(false) or 1(true).
1) 10 < 100 <=50 is evaluated:
2) 10<100 ?? => true so (10<100) is replaced by 1
3) 1 <= 50 ?? => true  ==> print bug

Another variation:
Code: [Select]
void setup()
{
  Serial.begin(115200);
  int val = 100;

  if (10 > val > -1 ) Serial.println("BUG");
  else Serial.println("OK");
}

void loop(){}


Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up