Syntax Errors

Syntax is how we refer to the way in which individual elements in a programming language (tokens) are structured. Syntax errors are usually the result of typos or code not being put in the right place. On this page I’m going to walk you through some common syntax errors and what they mean.

Missing Semicolons

If you’re used to languages like Python (which uses newlines) or javascript (which treats semicolons as optional), or are just new to programming this one can often trip you up. In most c-derrived languages statements (instructions) are seperated by semicolons ;.

Syntax is how we refer to the way in which individual elements in a programming language (tokens) are structured. Syntax errors are usually the result of typos or code not being put in the right place. On this page I’m going to walk you through some common syntax errors and what they mean.

Missing Semicolons

If you’re used to languages like Python (which uses newlines) or javascript (which treats semicolons as optional), or are just new to programming this one can often trip you up. In most c-derrived languages statements (instructions) are seperated by semicolons ;.

Syntax error resulting from missing semicolons

In other words, code often looks like this:

statement;
statement;
statement;

Sometimes, people will miss out one of the semicolons (;) between these statements, which results in strange errors (notice the missing semicolon on the second line):

statement;
statement
statement;

In the IDE, it often looks like this:

Syntax error resulting from missing semicolons

Missing curley brackets

Curley brackets are used to form blocks of code (scopes, but we’ll talk about that another time). When writing code, you may accidently forget to include a closing curley bracket } at the end of a method or statement. This usually results in the last line in the code reporting that there are not enouph closing curley brackets:

Console showing mismatched curley brackets

The error will often look like this in the IDE:

Code missing a closing curley bracket

Notice that the if statement is missing a closing bracket. As a result, the code does not know where the if statement should end. This is one of the reasons that keeping code well indented can help spot these kinds of errors (notice how you can see reasonably quickly where the closing curley bracket is missing).

As a rule of thumb:

  • When there is an opening curley bracket ({), add a level of indentation
  • When there is a closing curley bracket (}), remove a level of indentation

Code outside of methods

One common error, especially in modules like this, is putting code inside a class instead of a function (aka a method). This is fairly common for programmers coming from languages Python, which don’t use classes as much fully object-oriented languages.

When using classes, be aware that they should only (directly) contain the following things:

  • Properties (variables that live inside classes) which need to exist within the class or object scope (again, we’ll talk about this more next study block)
  • Methods (functions that live inside classes) which contain code to execute
  • Other classes (inner/nested classes)

The code you write can (and should) live inside methods. When copying, moving or writing code, students will sometimes remove a method decleration accidently, or forget to include one. As a result, code ends up in the class directly rather than inside a method. The resulting error messages are usually quite verbose.

Console showing code at the class level

You can usualy spot this when the if statements have red swiggly lines:

Code sample with class-level code

The fix is to figure out why the code is not inside a method. In this case, it’s because I’ve deleted the method decloration it was inside of:

The fixed code example

Unbracketed Blocks

Some programming languages let you omit the brackets for simple if statements and loops, in cases such as the above. You need to be careful doing this, because it means the next statement is the body of the of the if statement, and all statements after that are actually not inside the if statement.

In other words, the following expression:

if (myThing)
	statement1;
	statement2;

(rather counter-intuatively) actually means:

if (myThing) {
	statement1;
}
statement2;

This is one of the motiving reasons that Python made the whitespace meaningful (so you couldn’t write code like this). It’s also the reason most style-guides do not permit unbracketed if, and for expressions, as then people know it’s a bug not intentional behaviour. This also shows us a limitation of languages like C#, the whitespace is meaningless. Just because it is indented it does not mean it is within the scope of the statement. The curley brackets control this instead.

As a rule of thumb, in C# always include brackets when using if statements and loops, as it makes it easier to figure out what is going on.

Blocks

Some (control-flow) statements, such as if, while, and for have their own scopes (curley brackets) are not single-line statements. We don’t (usually) put semicolons after these in C#. Instead, we write the code that should be executed inside the curley brackets.

For example:

if (myCond) {
	statement1;
	statement2;
}

This will execute statement1 and statement2 iff (if and only if) myCond is true.

If you do accidently put a semicolon on these, the block is actually empty. Consider the following code snippet (notice the extra semicolon):

if (myCond);
	statement1;
	statement2;

This actually means:

if (myCond) {
}
statement1;
statement2;

Ie, statement1 and statement2 will run regardless of if myCond is true or false. This is something that takes a bit of getting used to in C# and related languages.

As a rule of thumb, if you are putting curley brackets on it, it probably doesn’t need a semicolon.

Running unit tests in Visual Sudio


Last updated 0001-01-01

Graduation Cap Book Open book GitHub Info chevron-right Sticky Note chevron-left Puzzle Piece Square Lightbulb Video Exclamation Triangle Globe