from some articles I read on general programming concept. I was made to know that "syntaxs are the formal rules that governs the construct of valid statement in a language" while "semantics are set of rules that give meaning to a statement of a language". from the defination of semantics, I feel it is similar to logic, if not, then please I want to know the difference between logical error and semantic error?
Syntax Error vs Logical ErrorA syntax error is an error in the syntax of a sequence of characters or tokens that is intended to be written in a particular programming language. A logical error is an error in a program that causes it to operate incorrectly but not to terminate abnormally.
A logical error in a program is an error were the instructions given in the program do not accomplish the intended goal. "Get me a cup of coffee." is a logical error when the person intended to ask for a cup of tea. In computer programs, this error can occur in many different forms.
Tabular Difference between Syntax and Semantic Error: It occurs when a statement that is not valid according to the grammar of the programming language. Some examples are: missing semicolons in C++, using undeclared variables in Java, etc. It referred to as semantic error. It is generally encountered at run time.
Syntax errors occurs when the rules of the programming language are violated. Semantic errors occur when the statement are not meaningful.
There seems to be lot of confusion around the definition of these terms, but here's my understanding:
Syntax relate to spelling and grammar.
Logic relate to program flow.
Semantics relate to meaning and context.
If the code fails to execute due to typos, invalid names, a missing parenthesis or some other grammatical flaw, you have a syntax error.
If the syntax is correct but a piece of code is (inadvertently) never executed, operations are not done in the correct order, the operation itself is wrong or code is operating on the wrong data, you have a logical error. Using a wrong conditional operator is a common example, so is inadvertently creating an infinite loop or mixing up (valid) names of variables or functions.
If both your program logic and syntax is correct so the code runs as intended, but the result is still wrong: you likely have a semantic error. Confusing a metric input value for an imperial value will get you there. Nothing wrong with the program, except that miles and kilometres don't add up, so your area calculation throws out the wrong number. Having a race condition is another common example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With