Get job ready skills with Codenga     |       Career Paths 40% OFF     |        Limited time only

22h 17m 58s
close
Cart icon
User menu icon
User icon
Lightbulb icon
How it works?
FAQ icon
FAQ
Contact icon
Contact
Terms of service icon
Terms of service
Privacy policy icon
Privacy Policy
Zdjęcie główne artykułu.

What is syntax

When talking about programming the topic of “syntax” often comes up. You’ve probably heard phrases like “the syntax of the language is rather simple” or “that language’s syntax is a bit more complex”. So what is it all about?

Programming languages can be compared to the kind of language we use in our everyday lives - they have a set structure. To be able to use a language, you need to know its structure first; without it you couldn’t communicate. This is the case for programming languages too - without knowing their syntax you can’t use them to write code.

It’s safe to say, then, that syntax is key, and that’s why this is the first step you take when learning a programming language.

The basics of syntax

Syntax can be defined as a set of rules the language has to adhere to. The rules describe how to put characters together to create correct declarations in a given programming language. It’s a lot like creating sentences in a natural language - to create them correctly, you need to know some rules; without them you wouldn’t be able to put together a statement that’s understandable to others.

Programming is, in other words, writing instructions that are understandable for the computer. That’s why they need to be written in a way that will let the computer understand - they need to comply with the syntax.

What syntax include

Each programming language has its own strict rules. They describe things like:

  • how to form variable declarations, functions and other elements;
  • how to add comments in the code;
  • how to write blocks of code;
  • when to use white spaces (eg. spacebar).

These are the guidelines we start with when learning a new programming language. It is the very fundamental step, essential to be able to move forward - syntax is every language’s core.

Syntax errors

Syntax errors are the bane of beginner programmers’ existence. They appear because of an instance of incorrectly applied syntax.

Let’s take a look at this simple sentence:
“programming Consists of writing. instructions that for, the computer understandable. Are.”

There are a few mistakes here: incorrectly applied full stops and commas, wrong case of some letters, words out of order etc. Despite that, we can still understand the general message of the statement - the natural language is very forgiving in that sense, especially when spoken.

When it comes to programming languages it’s the complete opposite. The code is written for the computer to interpret, and the computer can only interpret simple, clear instructions. There’s no leeway for such deviations from the guidelines here - one misplaced spacebar could decommission the entire program. The programming syntax are much more restrictive than the ones for natural languages.

Luckily, getting the hang of the most important elements of the syntax isn’t very hard. After a while they will become muscle memory to use and they’ll almost write themselves :) There are also tools that automatically find some of the typical syntax errors in your code to help you learn and improve.

Syntax and semantics

We’ve mentioned that each programming language has its own syntax. Let’s try then, to write the same example code down in a few popular languages.

Python


name = "Teddy"
print(name)


Java


class Main {  
  public static void main(String args[]) {
    String name = "Teddy";
    System.out.println(name);
  }
}


Both codes do the exact same thing - they declare a variable with assigned value “Teddy” and print the value out in the console. It could be stated then, that they both have the same meaning, semantically they are the same.

The difference here obviously lies in the syntax. In Python’s case, the syntax is visibly simpler, while Java has a few more syntactical elements. This example presents in a clear way that different languages have different, unique syntax.

It’s often said that Python is a very beginner friendly language, and the reason behind that is its simple and straightforward syntax. The same effect can be reached with a smaller amount of syntactical elements. Java on the other hand is a bit more “standard” - the syntax rings familiar to a lot of other popular languages like C++ or C#. Either way each language has its own syntax and mastering it is the very foundation of learning how to code.