'else' without a previous 'if'

if ((degreesC == 25) || (degreesC > 25));
{
digitalWrite(8, HIGH);
}
else ((degreesC < 25);
{
digitalWrite(8, LOW);
}
}

i tried to verify it and the arduino told me that:

Circuit_07_NEILL.ino: In function ‘void loop()’:
Circuit_07_NEILL:47: error: ‘else’ without a previous ‘if’

The else has an extra '(', and a ';' that does not belong there.

Regards, Ray L.

if you use the “else” the condition is not written, it is written only if you use the “elseif”.
Your code should look like this:

if (degreesC >= 25)
{
digitalWrite(8, HIGH);
}
else
{
digitalWrite(8, LOW);
}

if ((degreesC == 25) || (degreesC > 25));

This line is nonfunctional, because of the semicolon at the end. Don't forget the ">=" operator.

1 Like
if ((degreesC == 25) || (degreesC > 25));

What is that semicolon doing there ?

Why not write it like this anyway ?

if (degreesC >= 25)

if ((degreesC == 25) || (degreesC > 25));
{
digitalWrite(8, HIGH);
}
else ((degreesC < 25);
{
digitalWrite(8, LOW);
}
}

i tried to verify it and the arduino told me that:

Circuit_07_NEILL.ino: In function ‘void loop()’:
Circuit_07_NEILL:47: error: ‘else’ without a previous ‘if’

if ((degreesC == 25) || (degreesC > 25));

Lose the semicolon

else ((degreesC < 25);

Gain an “if”

thanks

Threads merged.

It can even be written more compact:

digitalWrite(8, degreesC>=25);

"It can even be written more compact:" is true, but only if:

  • You know something about digitalWrite(...) that isn't in the documentation, and
  • You know how this compiler represents true and false.

vaj4088: "It can even be written more compact:" is true, but only if:

  • You know something about digitalWrite(...) that isn't in the documentation, and
  • You know how this compiler represents true and false.

It is not difficult to find out

void setup()
{
  Serial.begin(115200);
  Serial.println(HIGH);
  Serial.println(true);
  Serial.println(1 > 2);
  Serial.println(2 > 1);
}

void loop()
{
}

I usually try to avoid 'nested' statements. Makes it more complex for those who might have to maintain the software in the future. And although it might not apply in this case, it is easier to learn a good habit than to unlearn a bad habit. My 2 cents :grin: