What is Programming Syntax?

Programming syntax refers to what you type to create the code, which will be executed by the computer. Different programming languages have different syntax, similarly to how natural languages differ (English, Spanish, French, etc). The difference between programming syntax can be minor, think of the word ‘color’ and ‘colour’, or very different, think of the words ‘hello’ and ‘bonjour’. The meaning of the words mean the same thing, regardless of the syntax, but the letters and spelling are different.

All of the below programming languages achieve the exact same thing, they all print/display the string “Hello World” to the console/screen. This is a very simple program, but it shows how the syntax of various programming languages differ.

The below code is written in JavaScript. In JavaScript to print something to the console all we have to do is call the function ‘log’ within the ‘console’ object, we use the dot seperator ‘.’ between the object and object’s function. The parameter given to the log function is the string we want to print, this string is wrapped in double quotes ‘”‘ and encased in parathesis ‘(..)’. At the end of the line of the code we suffix it with a semi-colon ‘;’. This is the only optional section of this line of code, we can remove the semi-colon, but the rest of the code is required.

Java is an object orientated programming languages, so we first must define a class. Within the class we must define a method called ‘main’, the definition of this method is the entry point for a Java program. The entry point must be public and static and return nothing (void) and take a string array as a parameter. Within the main method we call the ‘println’ method from the ‘System’ object and pass the string value to that method. We end every line of executable code with a semi-colon, this is non-optional. Every code block is enclosed in curly bracket ‘{..}’, this includes classes, methods, if statements, etc.

With PHP all we have to do to print a string value to the console is to call the ‘echo’ function followed by the string we wish to print. Observe how the parameter is surrounded in parathesis like with other languages, in PHP the echo function is the only function that doesn’t use parathesis. We also must end every line with a semi-colon.

When writing C++ code with must create a entry point function, in this case the ‘main’ function. This function must return an exit value, an integer. A 0 represents that the program ended without any errors. We must also import the ‘iostream’ library and call the cout function of the standard library. We then use the angle brackets ‘<<‘ to send the string value to the function, with an suffixed semi-colon. The angle brackets are only used with certain brackets, such as the standard iostream library, using with use parathesis to enclose parameters like with other programming languages.

Powershell has a ‘Write-Host’ function that we call to print values to the console, we pass the parameter we wish to print with the use of parathesis. We also do not end the lines of code with semi-colons.

The Basic programming languages executes statements using sequential numbers, it will execute the line of code starting with the number 10 and then the line starting with the number 20. The first line calls the ‘PRINT’ function with the string value we wish to show, and we don’t use parathesis to enclose parameters. We also don’t have to end lines of code with semi-colons. The next line of code, the one beginning the number 20, using the ‘END’ function to exit out of the program.

The examples shown are simple pieces of code from different programming languages. Do not let the difference between these examples inform your choice of which programming languages to choose. You should choose a programming languages based on what you which to build, as some languages are better for building certain types of programs. The syntax of the language is something you will get used to after programming in the language.

Leave a Reply