Fundamental concepts of programming Part #4 Syntax

4 Major Fundamental concepts of programming:

  1. Variables
  2. Control Structures
  3. Data Structures
  4. Syntax

What is syntax

Syntax in computer programming is simply the set of rules that uses a combination of symbols that are considered to be correct in a programming language. In C++, a simple example of a syntax rule that is followed is placing a semi colon “;” at the end of each line of a variable declaration and in the body of the code.

The best analogy for defining syntax in programming is like the grammatical rules in the English language. When you took an English class in school, you leant about placing full stops, commas and quotation marks in your writing. When you are learning programming, you learn about placing the correct syntax in your code. If you don’t follow the rules of the syntax in a programming language, you’ll get errors and your code will not run until you fix all of them.

Example of syntax in C++

Example syntax in C++

There are four parts to using the correct syntax in this example in C++. Lets break it down step by step.

  1. The first step is declaring the variable type. In this example we have declared a “string” variable type. We talked about variables in the first article of this series. I mentioned about the different types of variables in programming and how to use the correct one.
  2. The second part is creating the variable name which we have named “DisplayTitle”. This variable declaration has followed the syntax rules as there are no spaces and special characters in the declaration.
  3. The third part is setting the value of the variable. In this example, the value set to the variable is “This is my program”. In programming, strings are defined by a line of characters containing alphabetical letters, numbers and special characters in quotation marks (“”).
  4. The final part of the correct syntax is ending the declaration with a semi colon (;). In almost every line of code in C++, it will end with a semi colon, as they mark the end of a line. There are certain exceptions to semi colons, for example, bodies of functions and control structures use curly braces {} to mark the beginning and end. It is just like putting a full stop at the end a sentence, without them, our sentences would never end.

A painful stage for beginner programmers is learning how to fix and understand syntax errors. As your learning develops over time, you will become much more comfortable fixing syntax errors in a matter of minutes. Remember to choose an IDE (Integrated Development Environment) that you can easily understand and the IDE has built in syntax checkers that will check whether your code syntax is correct or not. An awesome IDE like Visual Studio, will show you exactly which line your syntax is incorrect and will even give you a clear hint of what your suppose to put in to fix it.

Summary

Syntax is known as the rules in a programming language. If the syntax is incorrect in the code, the code will not run. Syntax can be painful to learn, especially for beginner programmers but the pain can be minimized by using a IDE that you are comfortable will and can easily understand.

Share this post