Pages: [1]   Go Down
Author Topic: semi colon after if statement  (Read 1027 times)
0 Members and 1 Guest are viewing this topic.
United Kingdom
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what is the effect of putting a semi colon on an if statement? I'm sure this used to flag as an error (before version 1.0) but it doesn't any more and code seems to function differently with it/without it. so I was wondering what is meant to happen when you do this? (I hope that makes sense)
Code:
void loop()
{
 if(a >= b);
  {
    digitalWrite(13, LOW);
  }
}
   
the code is not relevant just an example
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 634
Posts: 50244
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'm sure this used to flag as an error (before version 1.0) but it doesn't any more and code seems to function differently with it/without it.
No, it didn't, and yes, it does.

The ; after the if() statement becomes the empty body of the if statement. Only the empty body will be executed, meaning that nothing happens.
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And this is executed unconditionally:
Code:
  {
    digitalWrite(13, LOW);
  }
Logged

United Kingdom
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ahh i see now, thanks that explains nicely  smiley-lol
Logged

Pages: [1]   Go Up
Jump to: