# Need some help with IF statement Error

``````{
if (inches > 50)  // more than 50 inches
{
for(int fadeValue1 = 1500 ; fadeValue1 <= 1600; fadeValue1 +=100)
//for(int fadeoutValue = 1600 ; fadeoutValue >= 1500; fadeoutValue -=100);
motor2.writeMicroseconds(1500);
delay(1000);
}
else if ((30 < inches) && (inches <= 50))  // between
{
for(int fadeValue2 = 1600 ; fadeValue2 <= 1800; fadeValue2 +=100)
//for(int fadeoutValue2 = 1600 ; fadeoutValue >= 1500; fadeoutValue -=100)
{
delay(1000);
{
}
else if ((24 < inches) && (inches <= 30)) // between
{
for(int fadeValue3 = 1600 ; fadeValue3 <= 1700; fadeValue3 +=100)
//for(int fadeoutValue2 = 1600 ; fadeoutValue >= 1500; fadeoutValue -=100)
{
delay(1000);
}
else if ((12 < inches) && (inches <= 24) )  // between
{
for(int fadeValue4 = 1600 ; fadeValue4 <= 1650; fadeValue4 +=100)
//for(int fadeoutValue2 = 1600 ; fadeoutValue >= 1500; fadeoutValue -=100)
{
delay(1000);
}
}
else  // closer than 12 inches
{
motor1.writeMicroseconds(1500);
motor2.writeMicroseconds(1500); // stop motors
delay(1000);
}
}
``````

On the second else if statement, I am getting an error saying " ‘else’ without a previous ‘if’ ". I cant see why i am getting this error. Can anybody else see where this error is coming from?

``````          {
delay(1000);
{
``````

Curly bracket wrong way around.

``````   else if ((30 < inches) && (inches <= 50))  // between
``````

How come the constant and the variable switch sides? This statement is hard to read because the English equivalent has the number and the variable in the wrong order to start with.

It is not wrong (necessarily). It is just harder to read than is necessary.

I think the idea is that it is of the style:

``````30 < inches <= 50
``````

but broken into multiple clauses.

I think the idea is that it is of the style:

Agreed. If one were to vocalize that statement, though, one would say "if inches greater than 30 and less than or equal to 50...".

Translating that English statement into code would NOT put the 30 before inches. And not doing so makes the code easier to comprehend at a glance, in my opinion.

Translating that English statement into code would NOT put the 30 before inches. And not doing so makes the code easier to comprehend at a glance, in my opinion.

I completely agree. The whole thing is just horrible…

in SQL language there’s a
`inches BETWEEN 30 AND 50`
which is horrible syntax, agreed. Not being a native English speaker, I understand it tries to simulate natural language.

For me
`(30 < inches && inches <= 50)`
is rather clearer than
`(inches > 30 && inches <= 50)`
where I have to really check hard if the angles are in the correct direction.
Especially when there are a couple of range comparisons formatted nicely.

[quote author=Nick Gammon link=topic=107906.msg810229#msg810229 date=1338364023]

``````          {
delay(1000);
{
``````

Curly bracket wrong way around. [/quote]

Thank you!

michael_x:
For me
`(30 < inches && inches <= 50)`
is rather clearer than
`(inches > 30 && inches <= 50)`
where I have to really check hard if the angles are in the correct direction.

It’s funny how people are different. For me it is the opposite. For example, if you said “this motor needs more than 5 V but less than 10V to run” I can understand that easily (ie. voltage > 5 && voltage < 10).

But if you say “5V is less than the amount needed for the motor, but the motor needs less than 10V” I would say “what?”. It’s like saying “the cat is green, but blue is the colour of the dog”. You are swapping concepts in mid sentence.

You could say “5V is too little and 10V is too much” but that would be “5 < voltage && 10 > voltage”. But then the comparison signs are different again.

[quote author=Nick Gammon link=topic=107906.msg811004#msg811004 date=1338412388]It’s funny how people are different. For me it is the opposite. For example, if you said “this motor needs more than 5 V but less than 10V to run” I can understand that easily (ie. voltage > 5 && voltage < 10).

But if you say “5V is less than the amount needed for the motor, but the motor needs less than 10V” I would say “what?”. It’s like saying “the cat is green, but blue is the colour of the dog”. You are swapping concepts in mid sentence.

You could say “5V is too little and 10V is too much” but that would be “5 < voltage && 10 > voltage”. But then the comparison signs are different again.
[/quote]
I think the # < x %% x < # isn’t about English, but more closely resembles mathematical notation common in algebra classes:

``````50 < x < 100
``````

expands to

``````50 < x && x < 100
``````