- Learn C++ Test Site - https://test.learncpp.com -

3.1 — Syntax and semantic errors

Types of errors

Programming can be difficult, and there are a lot of ways to make mistakes. Errors generally fall into one of two categories: syntax errors, and semantic errors (logic errors).

Syntax errors

A syntax error occurs when you write a statement that is not valid according to the grammar of the C++ language. This includes errors such as missing semicolons, using undeclared variables or functions, mismatched parentheses or braces, etc… For example, the following program contains quite a few syntax errors:

Fortunately, with compiled languages like C++, the preprocessor or compiler will catch syntax errors at compile-time, and will pinpoint the type of problem and the general location of the problem (usually within a line).

Thus, finding and resolving syntax errors tends to be fairly straightforward -- fix the first syntax error encountered, and then compile again until there are no more errors left.

Semantic errors

Once your program is compiling correctly, getting it to actually produce the result(s) you want can be tricky. A semantic error (also called a logic error) occurs when a statement is syntactically valid, but does not do what the programmer intended.

Some types of semantic errors will cause your program to halt, such as in the case of a divide by zero:

Sometimes these will just produce the wrong value:

or

Unfortunately, the compiler will not be able to catch these types of problems, because the compiler only enforces grammar, not intent. Semantic errors only manifest when the program is running, and are thus sometimes called runtime errors.

For advanced readers

Sometimes the term “logic error” is used to refer specifically to an error that is entirely the programmer’s, such as the misuse of operator- instead of operator+ or the misspelling of “world” in the above examples. Similarly, the term “runtime error” is sometimes used to refer to errors that occurred due to something outside the programmer’s control, such as the user entering invalid input, or losing internet connectivity. Many of these issues outside of the programmer’s control can be detected and handled (if the programmer if sufficiently mindful that they could occur).

In the above example, the errors are fairly easy to spot. But in most non-trivial programs, many semantic errors will not be easy to find by eyeballing the code. This is where debugging techniques can come in handy.

In this set of lessons, we’ll explore some basic debugging techniques, including using an integrated debugger.

Syntax and semantic errors

Programming can be difficult, and there are a lot of ways to make mistakes. Errors generally fall into one of two categories: syntax errors, and semantic errors (logic errors).

A syntax error occurs when you write a statement that is not valid according to the grammar of the C++ language. This includes errors such as missing semicolons, using undeclared variables, mismatched parentheses or braces, etc… For example, the following program contains quite a few syntax errors:

Fortunately, the compiler will generally catch syntax errors and generate warnings or errors, so you easily identify and fix the problem. Then it’s just a matter of compiling again until you get rid of all the errors.

Once your program is compiling correctly, getting it to actually produce the result(s) you want can be tricky. A semantic error occurs when a statement is syntactically valid, but does not do what the programmer intended.

Sometimes these will cause your program to crash, such as in the case of a divide by zero:

Sometimes these will just produce the wrong value:

or

Unfortunately, the compiler will not be able to catch most of these types of problems, because the compiler’s job is to enforce grammar, not intent.

In the above example, the errors are fairly easy to spot. But in most non-trivial programs, semantic errors are not easy to find by eyeballing the code. This is where debugging techniques can come in handy.

In this set of lessons, we’ll explore some basic debugging techniques, including using an integrated debugger.

%Missing lookup for lesson id 3498%