Semicolon Function

I would like to ask if perhaps the site could elaborate on the function of the semicolon in Arduino code writing? In the Arduino servo library's Sweep code, there is the line:
" for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees"

When I searched up the function of the semicolon in the Reference page, the site discussed how semicolons are only used to end a statement, not to indicate a sort of conjunction such as "and" and/or "or." Will someone please help elaborate on that? I just wanted to make sure I'm reading how the for loop's conditions work with Arduino.

Thanks a bunch!

Where is this conjunction you speak of? Edit - oh, I see. You need to look at the reference page for the for statement.

There are multiple parts to a statement in C:

operators, operands
expressions
statements

operators -- symbols that have meaning to the compiler: + means add, - means subtract, etc.
operands -- the arguments used by the operators. Most operators require two operands and are
called binary operators:

x + y

x is an operand, y is an operand, and + is the binary operator. There are unary operators (e.g., !)
and ternary operators (= ? : )

expression -- a group of operators and operands that combine to form an expression.

z = x + y

is called an assignment expression. Because of operator precedence, the addition of x and y
is processed as a subexpression yielding an intermediate result, then the assignment operator
causes that intermediate result to be assigned into z.

statement -- a group of one or more expressions that combine to form a complete syntactical unit
or sequence point. These groupings are terminated with a semicolon:

z = x + y;

is called an assignment statement and represents all of the expressions the programmer
wants to form a complete statement.

A for loop is simply three expressions:

for (expression1; expression2; expression3)

expression1 sets the environment for the loop, and may have a comma-separated list of sub-
expressions.
expression2 is some form of expression that evaluates to true or false.
expression3 is one or more expressions that usually relate to the content of the statement body.

An example of a for loop with multiple subexpressions might be:

for (pos = 0, x = 10, y = 5, z = 0; pos <= 180; pos++, z += 2) {

Note the comma-separated list of subexpressions in expression1 and expression3. The semicolons simply act as sequence points for the compiler and tell it that it has enough information about the expression (and subexpressions) to process the expression.

1 Like

what econojack said, but the other way around:

A C program is a list of functions (and declarations and other stuff).

A function is a declaration followed by a statement list

A statement list is a series of statements in curly braces

A statement may be one of several things:

  • a statment list is a statement

  • a while(), for(), if() FOLLOWED BY A STATEMENT is a statment

  • an expression FOLLOWED BY A SEMICOLON is a statment

  • a semicolon on its own is a statement

There's a lot more to it, of course. The key is that C does not use line breaks or indenting to define the structure of your program, instead it uses structure and grammar. The semicolon is part of that grammar. It's not a thing that does something in itself - it's a particle that allows the compiler to make sense of the bits that do do stuff.

1 Like

You can think of the "for" statement as having three "sub-statements" (expressions as per econjack) separated by semicolors (because semicolons are statement separators, right?) This probably annoys the more modern compiler experts is being a syntactic abomination, but C predates all of that, it's probably easy to implement, and it sort-of makes sense...