Semicolon

Hi, I have a bit of code but I do not understand something

Here is my a part of my code:

if (temperature >= baselineTemp + 22) {
** digitalWrite(2, HIGH);**
** digitalWrite(4, HIGH);**
** digitalWrite(motorPin, HIGH);**
** }**

else (temperature <= baselineTemp + 21); {
** digitalWrite(2, LOW);**
** digitalWrite(4, HIGH);**
** digitalWrite(motorPin, LOW);**
** }**

As you can see there is a semicolon in italics, I do not understand why I need to have a semicolon there and not at the other part of the code.

Thanks for any help or suggestions,

Planetoid127

Please use code tags ("</>" button) to post code.

So that it looks like this:

 else (temperature <= baselineTemp + 21); {

which, incidentally, is not a valid else clause and also should not have a semicolon.

The else statement does not take an argument. If you want to test another condition then use the 'else if' form. Try this code.

if (temperature >= baselineTemp + 22) 
{
    digitalWrite(2, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(motorPin, HIGH);
}
else
{
    digitalWrite(2, LOW);
    digitalWrite(4, HIGH);
    digitalWrite(motorPin, LOW);
}

Also, you have an extra semicolon at the end of the else:

                  // No semicolon here |
                  //                   V
else (temperature <= baselineTemp + 21); {

Edit:

If that’s what you intended, it should be written:

else {
   if (temperature <= baselineTemp + 21) {
      // code controlled by else-if...
   }
}

A statement can be an expression ending in a semicolon, a statement list in braces, or the various control keyword structures (for, while, if, switch, …).

A semcolon on it’s own is a ‘do nothing’ no-op statement.

When the compiler reads the code you have written, it will interpret it like this:

if (temperature >= baselineTemp + 22) {
  digitalWrite(2, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(motorPin, HIGH);
}
else (temperature <= baselineTemp + 21)
  ; // do nothing

{
  digitalWrite(2, LOW);
  digitalWrite(4, HIGH);
  digitalWrite(motorPin, LOW);
}

In other words, the second block will always be executed no matter what the value of temperature.

This is probably not what you meant.