;

Description

Used to end a statement.

Example Code

int a = 13;

※ NOTES AND WARNINGS:

Forgetting to end a line in a semicolon

Forgetting to end a line in a semicolon will result in a compiler error. The error text may be obvious, and refer to a missing semicolon, or it may not. If an impenetrable or seemingly illogical compiler error comes up, one of the first things to check is a missing semicolon, in the immediate vicinity, preceding the line at which the compiler complained.

#define and #include

The semicolon MUST NOT places after #define and #include

if/else statement and for/while loop

If a semicolon is placed right after an if statement, else statement, for loop, or while loop, the semicolon is equivalent to a empty {} (curly braces). It means code inside these statements/loops become outside of the statements/loops. Let see two examples of without and with semicolon.

  • Without semicolon
void setup() { Serial.begin(9600); Serial.println("====== TEST START ======"); bool condition = false; if (condition) Serial.println("ArduinoGetStarted.com"); Serial.println("====== TEST START ======"); } void loop() {}
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
====== TEST START ====== ====== TEST START ======
Ln 11, Col 1
Arduino Uno on COM15
2
  • With semicolon
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
====== TEST START ====== ArduinoGetStarted.com ====== TEST START ======
Ln 11, Col 1
Arduino Uno on COM15
2

As we can see, with semicolon the code inside if statement is executed although the condition of the statement is false.

In more detail, the code with semicolon:

if (condition); Serial.println("ArduinoGetStarted.com");

is equivalent to:

if (condition) { } Serial.println("ArduinoGetStarted.com");

See Also

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables .

※ OUR MESSAGES