Difference between “Syntax Errors“ and “Logical Errors“ in programming

Two words that for someone could be confusing. Let’s explain them!

Andrei Albu
SCIENTIFIC ZOOM
4 min readOct 12, 2021

--

The problem considered and a solution

Let’s suppose we have an array of int types and we want to print them in sequence to the screen in the following way: “output: 1 2 3 4 5“.

Solution:

Now let’s understand what I could have made wrong.

Syntax errors

Look at the following code:

The two files posted above are equal? Look at them.

They are not equal. In the second one I wrote “i < arrayEx.lengt;“. What is “lengt“? I don’t know, you probably don’t know, the compiler surely doesn’t know. Here is the point! If you try to compile the second file the process will not succeed. The compiler will protest with a «error: cannot find symbol» message. This is a syntax error!

What is a syntax error?

An syntax error occurs when during compilation the compiler finds something in your code it doesn’t know what it means, or it not sure how to translate what you wrote. The compiler will always advise you about syntax errors, even though sometimes it could be wrong about the line (or lines) where the error occurred.

Look at the following example:

Where is the error?

I wrote “System.out.printlnnumbers);“. What is “printlnnumbers);“?? It is quite easy for us to understand that I forgot to write the “(“ bracket. The compiler didn’t get it and it tells me:

  • I didn’t write a statement -> quite right, what I wrote it is not a valid statement.
  • “;“ expected -> False, I wrote the semicolon at the end!

So pay attention. The compiler advises you but sometimes it could miss the real problem.

Logical errors

Look at the following code:

What changed? Try to figure it out.

In the for loop this time I started from 1, I wrote “int i = 1;“. Is it an error? It depends. On what? On what we wanted to do. I mean, it is not a syntax error (try to convince you), so the compiler wouldn’t protest. The program would be finally executable but the output wouldn’t be the one we expected. Why? We initialized the variable “i“ from value “1“ so we will print the values of the array from the second element (arrayEx[1] is the second element!). This is is a logical error!

What are logical errors?

They are errors which occurs when the program executes but doesn’t do what we expected. They are the most difficult to identify because the compiler doesn’t do nothing for you (the program has already been compiled).

Some logical errors are so serious that exceptions are thrown (you can throw exceptions in your program too).

Example:

In this code in the for loop, the condition changed. I wrote “i<=arrayEx.length“. I added the “=“ operator! So? It means also the value of “i“ equal to “arrayEx.length“ will be used as index for the array, but that index doesn’t exist in that array. An “ArrayIndexOutOfBoundsException“ will be thrown.

Conclusion

I hope I helped you to understand better the difference between “syntax errors“ and “logical errors“.

If you have an argument you want me to discuss about tell me in the comments.

Do you know the difference between “argument“ and “parameter“ in programming? I wrote an article about it.

Thank you for having read my article. I hope I added value to your knowledge.

Consider following me and subscribing below with your email to know when I publish the new daily articles.

Did you enjoy reading it?

Support me. Use the link below to become a Medium member.

Support my writing by becoming a Medium member today and get full access to all the stories on Medium. Click on “Medium member” above for the link. No extra costs for you!

--

--

Andrei Albu
SCIENTIFIC ZOOM

Writer and Reader — I am 24. Writing about software, computers, programming languages, business, marketing, psychology and many other topics. Writing daily.