+ 2
Logical error Vs Syntax error
What is the difference between Logical error and Syntax error ?
7 Respuestas
+ 5
HonFu has explained really well; I will explain in some other way
1) syntax error-) any compiler when read the statement then it looks for the words or symbols which are available in their symbol table internally. That symbol and words are defined according to that language Grammar that can be an context free grammar, natural grammar, Regular grammar etc
In this grammar starting symbol, procedural rules, end symbol all are present.
Now come back to syntax error, syntax analyzer is present in compiler which analysis the syntax by making an syntax tree by parsing each expression.
If that expression is not present in the grammar then it will raise an error so that error is considers as syntax error
For eg you missed semi colon at the end in C language and according to C language grammar an termination is done by (;) which is missing while trying to parse and made syntax tree by compiler then it will raise an error of missing semicolon
This way the syntax error is raise.
+ 5
Syntax Error:
Program doesn't even run. You get a message Syntax Error or sometimes another message saying you messed something up.
Logical Error:
Program runs perfectly fine but behaves in a way you didn't want to. Maybe your text is written backwards, or negative numbers appear although they shouldn't. Or anything else.
I once wrote a function that was supposed to fill up lines of text with whitespace to create a blocky look. Instead it *ate up* the lines until nothing was left, because I calculated some indexing wrong.
In short, logical error means:
You wrote nonsense that doesn't break any rules. ;-)
+ 5
Logical error means you have written the correct syntax but at time of analysing that syntax use which is done by semantics analyzer to analyze the meaning of written expression. If no analyse of the use of that logic is detected then an logical error.
here is an link Purva J you can get some information by reading if found any doubt ping me
http://www.dickbaldwin.com/Cosc1315/Pf00120.htm#a.A_logic_error
Have some 🍎 🍎
+ 3
Syntax error:
There is an error in your code...mostly like grammatical errors.....they raise an error when compiled or interpreted....
E.g.....1.you may miss a closing curly braces that cause a problem on block scope.... 2.you may miss a semicolon so that the statement is not finished...3.you may have written wrong spelling for some keywords....
Logic error:
You write code to solve a problem , not just to simply learn and challenge on sololearn....you may have to write a code for finding the factorial of a number, find whether a number is prime or not, or code for some complex tasks....In such case your code may not throw errors on compiling , but when you run it , it may give us some unexpected result
Note : it is not a syntax error now...the problem is with your algorithm, which produces unexpected results....
+ 2
DishaAhuja thanks for detail information.
+ 2
HonFu thank you so much..
0
Example:
Make a program that sums total employees by gender
int femaleEmpoyee = 17;
int maleEmployee = 22;
int totalEmployees = FemaleEmpoyee * maleEmployee;
Console.WriteLine(totalEmployees);
Compiler will run, code works great and has no syntax errors or warnings, however, there's a logical error since your company doesn't have 374 employees.
This is a logical error.
int FemaleEmpoyee = "22";
int maleEmployee = 17
int totalEmployees = femaleEmpoyee + maleEmployee;
Console.WriteLine(totalEmployees);
Even if the logic is correct, compiler won't run, there's several syntax errors, mismatched variable names, missing semicolon, wrong data types.
These errors are easier to fix since compilers will complain and often show you what/where is wrong, logic errors need more time to figure out and are basically how your regular dev job looks like - debugging and finding new ways to improve program logic