What is a Syntax Error in a Computer Program?
One of the essential concepts any computer programmer must understand is the syntax error. Universal to all programming languages, a syntax error is a common error message and gives you important clues on how to correct your code.
-
Syntax
-
Computer programming uses a set of reserved words, or commands, as well as a set of symbols (like curly braces, semicolons, and mathematical operators such as plus and minus) to write programs that can be executed in a variety of ways. Essential to writing a functional computer program is syntax, which is the correct ordering of words and symbols to create the desired output or functionality.
Syntax Error Basics
-
When you incorrectly order words and symbols and try to compile or execute a program, the compiler, which takes your code and translates it into commands for the computer's processor, will return a syntax error. This syntax order usually contains a line number, which will take you directly to the incorrect code, as well as an error message, which can be very general ("Syntax error at line 271") or specific ("Syntax error: If without End If, line 271).
Examples of Error Messages
-
Syntax errors may take other forms, as well, depending on the specific error. Not all syntax errors will say "Syntax error". Some examples of other types of error messages, taken from Visual Basic, include: "Invalid use of '.', '!', or '()'", "Wrong number of arguments used with function", and "Object doesn't support this property or method".
Interpreting Syntax Errors
-
Each of these error messages would appear with a line number, and looking at that specific line of code along with the error message will give you the information you need to correct your code.
Troubleshooting Syntax Errors
-
When you are faced with a syntax error you can't figure out, a good first stop is the documentation for the programming language you are using. Look up the specific words or commands used in the line triggering the error, and compare your code to the documentation. You should be able to find a description of the correct syntax, which will help you pinpoint the syntax error.
-