Friday, February 11, 2011

What are semantic errors?

In programming, writing a valid programming structure with invalid logic. The compiler will generate instructions that the computer will execute, because it understands the syntax of the programming statements, but the output will not be correct.
Semantic Errors are logical.


Semantic errors usually arise from either a fault in your initial algorithm (your plan of action), or in implementation of your algorithm (typos etc).

For example: "Add two numbers and print the result"
Algorithm:
Take in number 1
Take in number 2
Add number 1 to number 1
Print result

You could implement this perfectly, to the letter. It would compile. It would be semanticaly wrong.

Example 2: "Add two numbers and print the result"
Algorithm:
Take in number 1
Take in number 2
Add number 1 to number 2
Print result

This algortihm is correct. In your implementation, however, you may make a mistake:
i.e.
int answer = num1 * num2
print(answer)

Again, this would compile without issue. It is, however, sematically wrong again.

These errors are the hardest to find. You, as a programmer, must have the ability to diagnose these kinds of errors. Having full and intimate knowledge of the code you are working on is a must!

Semantic-Errors
Hope this clears things up a little.

Search This Blog