SYNTAX
The syntax of a programming language refers to the order to which different elements are combined to from valid expressions. These elements may be words, operators, or phrases. The syntax of a programming language doesn't have any relationship with the meaning.
An example of a syntax rule for programming is the assignment statement:
print(expression)
This is a valid syntax for using the print statement in python. If we try this without the closed bracket --> print(expression, it'll return a SyntaxError since we didn't follow the correct syntax.
SEMANTICS
Semantics emphasizes the meaning of a program, so it'll be understandable and easy to predict the outcome of execution.Semantics provides significant information needed to understand a program
For example
while <Boolean expression> :
<statement>
This is the general form of a WHILE statement in python. For the semantics, when the value of the boolean expression is met, the embedded statement would run.
<statement>
while <Boolean expression>:
The code above has no valid meaning because, we placed the statement before the starting the While loop. In this case the syntax is correct, but the semantics is wrong.
Another example is when we try to divide an integer by a string.
For instance
x = 3
y = "Richie"
print(x/y)
The syntax for the code above is correct, but the semantics is wrong, because it doesn't send a valid meaning. An integer can not be divided by a string.
Top comments (0)