A couple of months ago, I wrote about what a program is and explained a little about the different kinds of computer programs.  Today I want to extend that lesson a little by talking about some basic syntax concepts of programming.

 

Let’s start by defining what I mean by syntax.  Programming languages, like spoken languages, have rules about how they are used.  In spoken English, for example, the basic structure of the simplest sentence is to have a noun followed by a verb and concluded with a period.  In a programming language, most lines of code have a keyword or a set of mathematical operators, and many lines have variables in them.  Just as English uses a period to designate the end of a sentence, programming languages have some way of delimiting the end of a line of code.  The rules which govern that delimitation plus how operators or keywords or the like are used are the syntax rules of the language.

 

No two programming languages are exactly alike–that’s by definition–but they all have similarities.  I’ll begin with the end:  Every programming language, as I stated, has a different way of showing where the end of a line of code is.  For some languages, this is as simple as a CRLF, also known as a Carriage Return – Line Feed.  In simpler terms, it means hitting the Enter key at the end of a line.  Carriage Return is a bit of a misnomer, dating from the days of using typewriters when one would actually move the typewriter’s physical carriage to the start of a new line.  The carriage was returned to its starting point; thus a carriage return.  The mechanism for this also rolled the paper up so typing would continue on a new line rather than typing over the last one, an action called a line feed.  Computers separate these actions into two pieces, and it is possible to have an LF without a CR and vice versa.  (Although having one without the other can be a little ugly on the screen!)

 

Most modern languages, however, use a semi-colon as their delimiter.  This is both powerful and problematic.  It’s powerful because one can put several instructions on a single line, but doing so can make the code harder to read.  Sure, we’re used to paragraphs in written speech, using periods as a delimiter, but most written speech is not a list of instructions.  Lists of instructions are often put on separate lines for clarity.  Consider this example:

 

Method 1:

 

1.  Open the cookie package.
2.  Remove part of the cookie dough and put it on the cookie sheet.
3.  Place the cookie sheet in the oven.

 

Method 2:

 

1.  Open the cookie package.  2.  Remove part of the cookie dough and put it on the cookie sheet.  3.  Place the cookie sheet in the oven.

 

The second method works, but the first is clearer.  Meanwhile, I am now hungry for cookies.

 

Much like our written language, programming languages have delimiters beyond what is used to indicate the end of a statement.  Just as we delimit what we say into paragraphs, programming languages like to delimit blocks of code.  This is primarily done through the use of curly brackets (‘{‘ and ‘}’).  Some simply use a keyword for this kind of delimitation.  For example, one language might have something like this:

 

if (condition)
{
Do some things;
}

 

While another language would have something like this:

 

if condition
Do some things
end if

 

“Do some things” is not literal; it is what programmers call “pseudo-code.”  It’s not real code and no computer would understand it, but we humans can see it and understand it’s an abbreviation of sorts for regular computer code.

 

The above examples bring to light the next pair of concepts in programming:  Conditional statements and variables.  However, I have already gone long on this post so I will save those concepts for next time.  See you then.