;

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() {}
COM6
Send
====== TEST START ====== ====== TEST START ======
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • With semicolon
COM6
Send
====== TEST START ====== ArduinoGetStarted.com ====== TEST START ======
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

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
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

※ OUR MESSAGES