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

  • Thread starter Thread starter helofrind
  • Start date Start date
  • Tags Tags
    Arduino
Click For Summary

Discussion Overview

The discussion revolves around a syntax error encountered in an Arduino program, specifically related to the use of an 'else' statement without a preceding 'if'. Participants are exploring the cause of this error and providing guidance on correcting it.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant describes their issue with a syntax error in their Arduino code, specifically mentioning the error message regarding 'else' without a previous 'if'.
  • Another participant identifies that the error is due to a misplaced semicolon after the 'if' statement, which terminates the conditional prematurely.
  • A later reply confirms the need to remove the semicolon to resolve the issue.
  • The original poster expresses relief and acknowledges the simplicity of the mistake, indicating a learning experience.

Areas of Agreement / Disagreement

Participants generally agree on the cause of the syntax error and the solution to remove the semicolon. The discussion appears to be resolved with a clear understanding of the issue.

Contextual Notes

The discussion does not explore deeper implications or alternative coding practices related to the error, focusing solely on the specific syntax issue at hand.

helofrind
Messages
23
Reaction score
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
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...
 
if (digitalRead (light) == HIGH);

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

Similar threads

Replies
7
Views
3K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
3
Views
10K