Arduino Syntax problem ('else' without a previous 'if')

  • Thread starter helofrind
  • Start date
  • Tags
    Arduino
In summary, The user is new to programming and was trying a simple code to control an LED, but encountered a syntax error. They asked for an explanation and someone pointed out that they had terminated the 'if' statement with a semi-colon, causing the error. The user thanks them and acknowledges their mistake.
  • #1
helofrind
23
1
not sure why I am getting a syntax, I am new to programming so trying something simple. Can somone please explain why I am getting this error?

Code:
int LED = 11;                                // on/off battery 2 control
int LEDP = 5;
void setup()
{
 pinMode (LED, OUTPUT);
 pinMode (LEDP, INPUT);
}

void loop()
{
 int light = digitalRead (LEDP);
 if (digitalRead (light) == HIGH);
 {
   digitalWrite (light, HIGH);
 }
 else
 {
   digitalWrite (light, LOW);
 }
}
Arduino: 1.6.12 (Windows 7), Board: "Arduino/Genuino Uno"

C:\Users\User\Documents\LED_controller\LED_controllerIO\LED_controllerIO.ino: In function 'void loop()':

LED_controllerIO:19: error: 'else' without a previous 'if'

else

^

exit status 1
'else' without a previous 'if'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
 
Last edited by a moderator:
Engineering news on Phys.org
  • #2
helofrind said:
not sure why I am getting a syntax, I am new to programming so trying something simple. Can somone please explain why I am getting this error?

Code:
int LED = 11;                                // on/off battery 2 control
int LEDP = 5;
void setup()
{
 pinMode (LED, OUTPUT);
 pinMode (LEDP, INPUT);
}

void loop()
{
 int light = digitalRead (LEDP);
 if (digitalRead (light) == HIGH);
 {
   digitalWrite (light, HIGH);
 }
 else
 {
   digitalWrite (light, LOW);
 }
}
Arduino: 1.6.12 (Windows 7), Board: "Arduino/Genuino Uno"

C:\Users\User\Documents\LED_controller\LED_controllerIO\LED_controllerIO.ino: In function 'void loop()':

LED_controllerIO:19: error: 'else' without a previous 'if'

else

^

exit status 1
'else' without a previous 'if'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
You have terminated the 'if' statement with a semi-colon...
 
  • #3
if (digitalRead (light) == HIGH);

Remove the red semicolon.
 
  • #4
HAHA, thanks, had a feeling it was going to be something stupid. Lesson learned
 

1. What is an "else" statement in Arduino syntax?

An "else" statement in Arduino syntax is used to specify an alternative action to be performed if the previous "if" statement evaluates to false. It is typically used in conjunction with an "if" statement to create a conditional structure in the program.

2. Why do I get an "else without a previous if" error in my Arduino code?

This error occurs when an "else" statement is used without a preceding "if" statement. This means that the program does not have a condition to check before executing the alternative action specified in the "else" statement. To fix this error, make sure that every "else" statement is preceded by an "if" statement.

3. Can I have multiple "else" statements in a single "if" statement in Arduino?

Yes, you can have multiple "else" statements in a single "if" statement in Arduino. This allows you to specify multiple alternative actions to be performed if the condition in the "if" statement evaluates to false. However, each "else" statement must be preceded by its own "if" statement.

4. How do I troubleshoot an "else without a previous if" error in my Arduino code?

To troubleshoot this error, you can check your code for missing or mismatched "if" statements. Make sure that each "else" statement has a corresponding "if" statement before it. You can also use the serial monitor to print out the value of the condition being checked in the "if" statement to see if it is evaluating to true or false.

5. Can I use an "else" statement without an "if" statement in Arduino?

No, you cannot use an "else" statement without an "if" statement in Arduino. The "else" statement is designed to provide an alternative action if the condition in the "if" statement is false. If there is no "if" statement, there is no condition to check and the "else" statement has no purpose. You can use a single "if" statement without an "else" statement to perform an action only if the condition is true.

Similar threads

Replies
7
Views
1K
  • Electrical Engineering
Replies
5
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
Replies
22
Views
2K
  • Computing and Technology
Replies
1
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Replies
3
Views
10K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top