In this post, we will understand the difference between syntax and semantics.
It defines the rules and regulations that helps write any statement in any programming language.
It doesn’t have any relationship with the meaning of the statement.
It is associated with the grammar and structure of the programming language.
A line of code is syntactically valid and correct if it follows all the rules of syntax.
It does not have to do anything with the meaning of the statement.
Syntax errors are encountered after the program has been executed.
Some examples include: missing semicolons in C++, undeclared variables in Java
Such errors are easy to catch.
It refers to the meaning of the associated line of code in a programming language.
The semantic errors are handled during runtime of the program execution.
It tells about the meaning, and helps interpret what function the line of code/program is performing.
Semantic errors are encountered at runtime.
They are encountered at runtime.
Even when a statement has correct syntax, it wouldn’t do the function that was intended for it to do.
Such errors are difficult to catch.