Else without a previous if - It's not a semicolon problem

I’ve searched both the forum and google, and I can’t seem to find out why my code is returning the “Else” without a previous “if” error. Every example I’ve found is someone terminating the condition with a semicolon. I’m definitely not doing that. I’m sure it’s a newby error of some sort, since I am quite new at this, but I can’t for the life of me find it. Can anyone help me here, please? Here’s the section of code:

if (ch7 < 1500)    // Only allows Actuators to move if ch7 is switched on
   {
      digitalWrite(R01, HIGH);
      digitalWrite(R02, HIGH);
   }
   else 
   {  
       if (ch5 < 1200 && HallSwitch01 == LOW)
     {
        digitalWrite(R01, LOW);            // Actuator is extending
        digitalWrite(R02, HIGH);
     }
     }
       else if (ch5 < 1200 && HallSwitch01 == HIGH) // Indicates actuator has reached the end of its extending travel
     {
        digitalWrite(R01, HIGH);            // Actuator is turned off and braking
        digitalWrite(R02, HIGH);
     }
       else
     {
        digitalWrite(R01, HIGH);            // Actuator is retracting
        digitalWrite(R02, LOW);
       // if(ch5 < 1200 && HallSwitch01 == LOW)
        // digitalWrite(R02, HIGH);
       // else
        // digitalWrite(R02, LOW);
     }

Here’s the section of code:

Why? The compiler told you EXACTLY which line the error was on.

   else 
   {  
       if (ch5 < 1200 && HallSwitch01 == LOW)
     {
        digitalWrite(R01, LOW);            // Actuator is extending
        digitalWrite(R02, HIGH);
     }
     }
       else if (ch5 < 1200 && HallSwitch01 == HIGH) // Indicates ac

If you had indented the code properly, you’d see that you have an else if after an else statement. That is not allowed.

Thanks for taking the time to respond. Can I not put an "If > Else If > Else" set as the "Else" condition within a higher level "If > Else" statement? That's what I'm trying to do here. Again, I'm new. You'll have to forgive me if I'm not indenting properly. I've never had a programming class, I'm stumbling my way through tutorials, and I'm asking questions on forums.

“else if” is valid in most variants of Basic. You would think that it would work in C because you can put a single statement after an “else” without putting braces {} around it, and an “if” is a single statement.

But it doesn’t work. You have to use braces, which means you have to indent it “properly”.

If you want a long chain of else-if’s without getting indented off the right edge of the screen, consider using a switch-case.

Thanks MorganS! So, If I'm reading correctly, it is possible to do what I'm attempting here (put an "If > Else If > Else" set as the "Else" condition within a higher level "If > Else" statement), but I'm indenting incorrectly. I have braces around what I think are the correct spots. I understand that I need to place them around the commands I want to make conditional, and I've looked through them about a bajillion times. Could you point me in the right direction as far as indenting correctly goes here? Do I need to put the open brace "{" on the same line as the If, Else If, and Else? Thanks again. I'm probably learning this out of order, but there it is.

There's two styles of braces:

void loop () {
  //code here
}
void loop()
{
  //code here
}

I prefer the first one because it saves space but the second one makes it a lot more obvious which braces match up.

The neat thing about the Arduino IDE is you can write code so that it complies without error, then hit the Tools->Auto Format (alt-T) command.

The first few times you do this it will appear to scramble your code in an unfamiliar way. Once you get used to following that style, then you can work without using that tool at all.

Figured out the problem: I had two closing braces “}” where I just needed one. Here’s the corrected code:

    if (ch7 < 1500)    // Only allows Actuators to move if ch7 is switched on
  {
    digitalWrite(R01, HIGH);
    digitalWrite(R02, HIGH);
  }
    else
  {
    if (ch5 < 1200 && HallSwitch01 == LOW)
    {
      digitalWrite(R01, LOW);            // Actuator is extending
      digitalWrite(R02, HIGH);
    }
    else if (ch5 < 1200 && HallSwitch01 == HIGH) // Indicates actuator has reached the end of its extending travel
    {
    digitalWrite(R01, HIGH);            // Actuator is turned off and braking
    digitalWrite(R02, HIGH);
    }
    else
    {
    digitalWrite(R01, HIGH);            // Actuator is retracting
    digitalWrite(R02, LOW);
    // if(ch5 < 1200 && HallSwitch01 == LOW)
    // digitalWrite(R02, HIGH);
    // else
    // digitalWrite(R02, LOW);
    }
  }
}

Thanks for your help folks.

"else if" is valid in most variants of Basic. You would think ... But it doesn't work.

Huh? This is perfectly valid:

if (a == 12) {
   // blah
} else if (a == 13) {
  // humbug
} else if (a == 100) {
  // stuff
} else {
  // otherwise
}

As westfw says, its fine, the OP's didn't work as else was before the else if, which does not form a logical statement.

press CTRL-T to auto indent your code.

int a=7 ;
int b ;

if ( a==1 ) b=1 ;
else if (a == 2 )  b=743 ;
else b=-4 ;

I think that is legal and you don't need braces.

The problem with the OP's original code is that he had a superfluous } there.

michinyon: int a=7 ; int b ; if ( a==1 ) b=1 ; else if (a == 2 ) b=743 ; else b=-4 ;

I think that is legal and you don't need braces.

The problem with the OP's original code is that he had a superfluous } there.

The braces all match in the original example. As PaulS mentioned, the code was structured like this:

int a=7 ; int b ; if ( a==1 ) b=1 ; else b=-4 ; else if (a == 2 ) b=743 ;

I think a few people are missing what I was trying to do. Here's a non-code, approximation of what I was trying to logically accomplish:

If (condition) Execute this code

Else Execute this next conditional:

If (condition) Execute this code

Else If (condition) Execute this code

Else Execute this code

I am not experienced enough with this stuff to say whether or not this is legal, but it certainly works. I have done it before, and now it is working in my code above. I understand the braces are necessary to have multiple executions happen under a conditional. The problem that I was running into, is that I had an extra brace where I didn't need one. A typo. it did not seem to have anything to do with indenting, in the end. Thanks again, for everyone's help and time.

iskanderdumacedon: I think a few people are missing what I was trying to do. Here's a non-code, approximation of what I was trying to logically accomplish:

If (condition) Execute this code

Else Execute this next conditional:

If (condition) Execute this code

Else If (condition) Execute this code

Else Execute this code

I am not experienced enough with this stuff to say whether or not this is legal, but it certainly works. I have done it before, and now it is working in my code above. I understand the braces are necessary to have multiple executions happen under a conditional. The problem that I was running into, is that I had an extra brace where I didn't need one. A typo. it did not seem to have anything to do with indenting, in the end. Thanks again, for everyone's help and time.

Then is must be parenthesized like this, which is NOT what you had in your original post:

If (condition)
{
    // Execute this code
} Else
{
    //Execute this next conditional:
   If (condition)
    {
        //Execute this code
    } Else If (condition)
    {
        //Execute this code
    } Else 
    {
        //Execute this code
    }
}

ALWAYS use curly braces when doing if/else, even though they are not always required. Use Ctrl_t to reformat the code correctly. ALWAYS indent properly, and mistakes will be obvious. When you DO get an error, LOOK at the error message - it gives you the exact line number, and the exact character position where the error is, and the error message is NEVER wrong. IF you get an error, there IS a mistake in your code, and it IS exactly what and where the error message says it is.

Regards, Ray L.

I am not experienced enough with this stuff to say whether or not this is legal, but it certainly works.

If it works, it must be legal.

I think we DID see what the problem was. The problem is always how to convey that to you in a way that: 1) Solves the immediate problem 2) Involves you in the solution 3) Makes you learn how to solve problems, without needing to rely on the forum.

and the error message is NEVER wrong.

It is often cryptic, and sometimes the real problem is something several lines earlier, so sometimes the messages ARE wrong.

Fix the FIRST message, which is nearly always right (except for the line and column numbers). Then, the rest of the messages may change/go away.

PaulS: It is often cryptic, and sometimes the real problem is something several lines earlier, so sometimes the messages ARE wrong.

Fix the FIRST message, which is nearly always right (except for the line and column numbers). Then, the rest of the messages may change/go away.

best advice for people new to coding. 50 problems in compile normally means one problem on the first thing the compiler found