Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Semantic error and logical error

Tags:

php

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?

like image 655
wildav Avatar asked Mar 13 '15 16:03

wildav


People also ask

What is the difference between a logic and syntax 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.

What is an example of a logical error?

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.

What is the difference between syntax error and semantic error give example of each?

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.

What is the difference between syntactic and semantic error?

Syntax errors occurs when the rules of the programming language are violated. Semantic errors occur when the statement are not meaningful.


Video Answer


1 Answers

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.

like image 168
Roy Avatar answered Sep 25 '22 19:09

Roy