What is Syntax in Programming

Put simply, syntax are the rules which define how each programming language should be written. It applies to the spelling, grammar, and symbols of every line of an application’s code.

I’m Jamie Lewis, and I’ve been forced to abide by the harsh rules of syntax since I first started writing my own programs twenty years ago!

This article will discuss exactly what syntax is, why it’s important, and why you might receive syntax errors when trying to run your code. This is an extremely important topic to understand, especially for anyone seriously considering a future in coding!

So, to quote the syntax of the classic BASIC programming languages, let’s “GOTO” the first section of this article!

What is Syntax in Computer Programming

Syntax is the way in which software must be written in order for it to be understood by a computer. This includes the words, grammar, punctuation, structure, and spacing of an application’s code. While many programming environments might let you use words you’d actually find in the dictionary, they’ll only be understood if that language’s syntax rules are being followed.

In the early days of computing, programming commands were incredibly cryptic. Whether you were using punched cards or a fluorescent screen on which you could type assembly instructions, there were some painfully exact rules which had to be followed at all times. This was, essentially, the syntax for this era of computing.

As computers became more powerful – and programming practices more refined – so too did newer languages come along which were easier to understand and to write. They still each have their own syntax though and, no matter how much leeway a language provides, you can still expect to encounter serious headaches if these rules aren’t followed.

Why is Syntax Important in Programming

If you neglect to follow the syntax of whichever programming language you’re using, your computer is most likely not going to understand what you’re telling it to do. This might result in your application running incorrectly, or in it not running at all; neither of these outcomes are ideal.

Remember, a computer has no innate intelligence and so is reliant on semantic rules to provide meaning and an understanding to the code it interprets. If these rules are not followed exactly, it suddenly will have no idea how to process the instructions you’re providing to it.

What is Syntax in Programming Example

Let’s look at a basic Java program which will demonstrate syntax in action!

    public class HelloWorld {

    public static void main(String []args) {

       System.out.println(“Hello, World!”);

    }

    }

As you can see, this is just a basic Hello World program; however, it does demonstrate some of the fundamental syntax rules which have to be followed when writing Java. 

For one, the word “System” on line 4 is capitalised. This is because package names in Java use lower-case characters. If you type line 4 with a lowercase “s”, you’ll receive an error because Java will think you’re looking for a (nonexistent) package called “system”.

Other important components of the syntax here are the brackets and quotation marks used when entering literal strings, and also the semicolon following the println command. Java’s syntax can be quite unforgiving, but even this small program provides a good demonstration of what syntax actually is.

What is Syntax in Python Programming

Another great language with which to demonstrate syntax examples is Python. Not only is Python incredibly popular these days, it also uses some pretty straightforward rules when it comes to syntax. And, unlike Java, it tends to be a little more lenient.

Let’s write a very simple program in Python:

    #!/usr/bin/python3

    i = 2

    if (i==2):

        print(“It does!”)

This program simply assigns a value of 2 to variable ‘i’. Then, if the value of i is equal to 2, it prints a string text to the console. As part of the if statement (lines 5 & 6), I’ve simply entered 4 spaces before the print command.

Now, let’s try to write the same program again. This time, though, we’ll not bother with any spaces before the print command.

    #!/usr/bin/python3

    i = 2

    if (i==2):

    print(“It does!”)

Try to execute it, and you’ll receive a syntax error (actually, you’ll explicitly receive something like an ‘IndentationError’, but this is just one possible type of syntax error in Python). This is because – in Python – the sequence to be executed upon a successful condition check must be on a new line and must be indented with at least one space or tab.

This example illustrates one of the most significant syntax rules to be found in Python; using whitespace appropriately! Rather than heavily relying on curly brackets, semicolons, colons, or other rather cryptic symbols, Python almost mandates that you type your code in a somewhat legible manner by requiring whitespace to break up chunks of code.

Also imperative in the syntax of Python 3 are the brackets and quotation marks which must enclose every text string (similar to our Java example above, though without the semi-colon!).

What is Syntax in C Programming

Like Java, C’s syntax can seem a little more overwhelming at first!

    #include <stdio.h>

    int main () {

    int i = 2;

    if(i==2) {

       printf(“It does!\n” );

    }

    return 0;

    }

You’ve probably already figured it out, but this program does the exact same thing as the Python code we looked at above; the only difference is that it’s written in C, and it uses more than double the lines. Unlike Python, though, the above program can also be written using just one line:

    #include <stdio.h>

    int main () { int i = 2; if(i==2) {printf(“It does!\n” );} return 0; }

Our interpreter or compiler won’t have an issue with this, and the output will be the exact same. This is because C does not necessitate line breaks as part of its syntax (unlike Python). 

The only problem with writing the code this way is that it solves no problems, and causes several. It’s far-less readable, meaning that any colleagues who need to look at your code later will have a hard time figuring out exactly what’s happening. 

It’s also far easier to make mistakes when writing code this way; for example, it’s harder to see exactly which of our opening brackets we’re closing when there are three on one line.

Put simply, the difference between the two languages is that ‘C’ expects all punctuation rules to be followed exactly (for example, the consequential code block if the condition is true must be enclosed by curly brackets and have a semicolon at the end).

This is just a basic example of some of the early syntax rules you’ll encounter when learning C, but – even at this level – it should help you to understand why Python is often the preferred option for beginning programmers!

Common Types of Error in Programming

As you can guess, syntax errors are one of the most common sources of errors in programming. However, they do take on a variety of forms! These often include:

  • Using the wrong case For example, typing ‘system.out.println’ rather than ‘System.out.println’.
  • Misspelling a word entirely No serious programming environment allows for words to be misspelt; “print” and “prnt” are not the same thing to your computer, even though it might seem obvious to us!
  • Missing a punctuation mark Typing ‘print(“Hello World!”)’ will work, whereas typing ‘print”Hello World!”)’ almost certainly won’t!

There are also other error types you’ll most likely encounter at some point. A few examples are:

  • Logic errors A logic error tends to occur without being immediately obvious at first; typically, a program will appear to work but it won’t behave as intended by the author. This can often be caused by forgetting to use consistent numerical formats (such as attempting to compare a Floating-Point variable with an Integer).
  • Arithmetic errors Any programmer who has inadvertently attempted to divide by zero will already be familiar with this type of error!
  • Runtime errors You’ve probably encountered a runtime error even if you’ve never written a line of code. A runtime error occurs when a program cannot run on the client’s computer due to missing components (for example, many modern applications rely on Visual C++ to be installed by the user before they can run).

Closing Thoughts

Syntax in programming are the rules which have to be followed when using a programming language. Not using syntax correctly will result in errors, misbehaving programs, and endless headaches, so it’s important to try to avoid this at all costs!

Now that you understand what syntax is, why not take a look at some of our other programming articles? Likewise, we always enjoy reading feedback from visitors and encourage you to keep checking back for new content on a whole host of topics!

Leave a Reply

Your email address will not be published.