Home Software Development Guide Programming Concepts Programming Languages And Syntax

Programming Languages And Syntax

by wforbes

(Part 6 of my Programming Concepts series)

Just like the spoken and written languages we use to communicate with each other like English, Spanish, Mandarin, Farsi, Arabic, and so many more; we use Programming Languages to communicate instructions to computers.

Each line in this paragraph is formed into a sentence. The main goal of each sentence is to express a few individual pieces of information. Each sentence contributes to the larger group of ideas that comprise this paragraph. Very similarly, most programming languages are structured by expressions (like sentences), made up of statements ( like words or phrases). These expressions can be included into code blocks, or blocks of code (like groups of ideas), and are often formed as a function or a method (like a paragraph).

Just like the differences between human languages, what makes Programming Languages unique from each other, are their syntax. The ordering of terms, the structure of their contents, and the rules that define their proper format are a language’s syntax. In English, our sentences all end with punctuation like periods or question-marks, and in many programming languages it’s required to end a statement with a semi-colon ( ; ). In order for English to make sense, certain types of words are ordered before or after other ones so that rules of grammar are followed. Similarly, programming languages rely on a specific ordering of the statements in each expression. In programming, statements are the individual portions of expressions that don’t resolve or return any value on their own.

What does all of this look like? Let’s think of a simple set of questions and sentences that we can form together into a small paragraph, then translate it to expressions formed into a code block and used in a function with a programming language.

Sentences and Paragraph:
What tax bracket would I be in if I earned $120,000 per year? The first earnings up to $9875 are taxed at 10%. The earnings between $9876 and $40125 are taxed at 12%. The earnings between $40126 and $85525 are taxed at 22%. The earnings between $85526 and $163300 are taxed at 24%.

Expressions and Function.
(With the JavaScript programming language):

var answer = findTaxBracket(120000);
console.log(answer);

function findTaxBracket(income) {
	var taxBracket;
	taxBracket = (income < 0) ? ( "" ) : ( "0%" );
	taxBracket = (income > 0 && income <= 9875) ? ( "10%" ) : ( taxBracket );
	taxBracket = (income > 9875 && income <= 40125) ? ( "12%" ) : ( taxBracket );
	taxBracket = (income > 40125 && income <= 85525) ? ( "22%" ) : ( taxBracket );
	taxBracket = (income > 85525 && income <= 163300) ? ( "24%" ) : ( taxBracket );
	return taxBracket;
}

This may not all make sense to you right now, but here’s an explanation of the code and how it relates to our sentences and paragraph. I’ll try to explain all the parts of the code, but don’t be too concerned with all of this if you don’t instantly understand everything. I don’t expect you to. We’ll go back over all these details in future pages when we’re actually learning to code.

First, on line #1, we ask the computer our question with an expression “var answer = findTaxBracket(120000);” which the equivalent of asking ‘What tax bracket would I be in if I earned $120,000 per year?’. With this, we are calling a function named findTaxBracket and giving it our earnings (120000) as an argument so the computer can find it for us. An argument is information that you send to a function to help it do what it needs to do. We store the result of that function in a pocket of the computer’s memory, a variable (var), named ‘answer’. A programmer would read this first line of code as “The variable named answer gets the result of the function named findTaxBracket when the number 120000 is passed as an argument”.

Then, in line #2, we print the answer to the javascript console so we can see it, by using the expression ‘console.log(answer)’…. sort of like the sentence “Tell me what the answer is!”. We do this by calling a special function built in to JavaScript that we can use, and passing our own variable named answer to it as an argument.

The rest of the code is the function named findTaxBracket. This is what’s used to answer our question. This is what we’re instructing the computer to do when it needs to use a function called findTaxBracket, like we what we did on line #1. As a programmer, you will write many functions, but there are already many functions built into the programming languages that are already written for us. Programmers use a mix of their own functions and pre-built functions to create their programs. Let’s explore the basics of what writing our own functions entails, using our example.

The first statements on line #3 are what’s known as the function declaration. With this we’re declaring to the computer this is a function, this is it’s name and the data it needs! The word ‘function‘ starts it off, letting the computer know – “Ok, this is going to be a function”. Then the name of our function is next so that it knows “Ok, when you see this name used in other places of this program, run this function.”. After that, you see the word (income) in parenthesis. This word in the parenthesis is called the parameter for the function. It links with the argument in parenthesis from line #1 so that the computer knows… “When this function is used, I should expect a piece of data to come with it”. Using a parameter lets us use this function with any income number, not just ‘120000’… that way we can use this function more than just once! Finally, at the end of line #3 we see an opening curly brace { . That opening curly brace { lets the computer know – “Ok, everything in the next lines will be a part of this function named findTaxBracket, that has one parameter named income.” Then the computer will know that our function is over when it sees the closing curly brace } on line #11. Everything between the opening curly brace on line #3 and the closing curly brace on line #11 is considered “Inside” of this function, this is the function body.

Inside of the findTaxBracket function, we have all the code we need to find what the tax bracket is for our answer. First, on line #4, we reserve a pocket of computer memory, a variable (var), named taxBracket. This gets our answer ready, but it doesn’t give it a value. It’s empty right now. The code on lines #5 through #9 are conditional expressions that consider whether a condition is True or False, and puts a value into taxBracket depending on the result. Each one considers something about our income parameter. It uses operators, which are all the symbols you see there like the ‘less than <‘ and ‘greater than >’ symbols, the ampersand symbols ‘&’, the question mark symbol ‘?’, the colon symbol ‘:’ to form each of these conditional expressions. I’ll spare you of the exact details here, but you can translate these expressions like this:

[line 7]: taxBracket = (income > 0 && income <= 9875) ? ( “10%” ) : ( taxBracket );
[line 8]: taxBracket = (income > 9875 && income <= 40125) ? ( “12%” ) : ( taxBracket );

Line 7: The taxBracket variable gets the result of a conditional expression where if the income parameter is greater than 0 and less than or equal to 9875, it should get the value “10%”, otherwise it should get the value it already had.
Line 8: The taxBracket variable gets the result of a conditional expression where if the income parameter is greater than 9876 and less than or equal to 40125, it should get the value “12%”, otherwise it should get the value it already had.

And really, these are just more exact ways of saying the sentences from our first example:
The first earnings up to $9875 are taxed at 10%.
The earnings between $9876 and $40125 are taxed at 12%.

Finally, at the very end of our function before the closing curly brace } we let the computer know that we’re done with the taxBracket variable and the value in that pocket of computer memory it represents should be brought to the line of code that called the function. In our case, it’s brought back to line #1 and put into the variable named answer. We do this with the return statement by typing “return taxBracket;”.

Feel free to study this and explore these coding terms that I outlined in bold text on your own. It’s not incredibly important for you to know them right this moment, but I took the time to introduce you to all of these so that you get some insight into the next topic described here – Syntax.

Programming Language Syntax

What makes Programming Languages unique from each other, just like the differences between other type of language – is its syntax. The ordering of terms, the structure of their contents, and the rules that define their proper format are a language’s Syntax. In English, our sentences all end with punctuation like periods or question-marks, and in many programming languages it’s required to end a statement with a symbol like a semi-colon ( ; ). Often English is written in a way that certain types of words are ordered before or after other ones to enforce grammar rules, and similarly, Programming Languages rely on a specific ordering of the each of the parts in thier code.

A major difference between our spoken and written human languages and a computer’s programming languages is that these rules of syntax are usually extremely strict. This is the first major hurtle that new programmers will face when starting out. Errors in syntax made by the programmer when writing source code will almost always lead to the computer either being unable to create a computer program from it or running into trouble while attempting to run the program. Tracking down and fixing syntax errors is an incredibly large part of the work involved with programming. Since those who are new to programming may not be used to examining code very closely for problems, they may spend 10% of their time initially creating the code and the other 90% of their time going back through it for small errors and problems.

Unlike humans who can read a jumbled sentence with poor grammar, punctuation mistakes, and misspelled words here or there, yet still understand the general gist of what the writer intended; Computers are almost totally unable to determine the implied meanings to things. For example, if you leave out a semi-colon from the end of a statement (like forgetting to end your sentence with a period), the computer may think that it should read that statement AND the next statement together as one single statement. You didn’t tell the computer the statement was over with the semi-colon, so it just kept going! Other common examples might be things like misspelling the name of something or placing one expression before another on accident. The computer has no way to consider how it can find the implied meaning behind what you intended to code, so your program won’t work as you intended and probably won’t even be able to be started until you find the errors.

Let’s look at variations of our example from earlier on this page. Here’s the way I typed it before:

What tax bracket would I be in if I earned $120,000 per year? The first earnings up to $9875 are taxed at 10%. The earnings between $9876 and $40125 are taxed at 12%. The earnings between $40126 and $85525 are taxed at 22%. The earnings between $85526 and $163300 are taxed at 24%.

Here’s a variation where I transpose phrases, move things around and misspell things:

If you make $9875 then u get taxed 10%. From $9876 to $40125 you are taxed 12%, but $40126 to $85525 ur taxed 22%, Earnings $85526 to $163300 will be like 24 percent. Which tax bracket wud I be if I made $120k?

It gives all the same information, but leaves out that we’re talking about annual earnings. It puts the question at the end of the paragraph instead of the beginning. The sentences aren’t all formed in the same way, one-sentence lists two brackets of earnings instead of one bracket like the others. It’s not pretty, but you can still read it and make out what the question is. Look back through it and try to find all the differences and ‘improper’ grammar used.

Now let’s look at a couple examples that would make the example code not work correctly…

vari answer = findTheTaxBracket[120,000);

Should be:

var answer = findTaxBracket(120000);

Problems: The computer needs “var” to be those exact three letters, so adding the extra ‘i’ in ‘vari’ would cause an error. The function is named incorrectly, that extra ‘The’ in the name would cause an error if the function isn’t declared with that name. The first parenthesis is actually an opening square bracket, so that would cause an error. There’s a comma in the number 120000, so the computer would actually think you’re giving it two arguments: ‘120’ and ‘000’, instead of one argument of 120000.

findTaxBracket<incme> function {

Should be:

function findTaxBracket(income) {

Problems: The keyword ‘function’ is put after the name of the function and the arguments, so the computer wouldn’t know you’re about to create a function and get confused. The parenthesis here are actually angle brackets … so the computer would think it’s something else entirely. The word income is misspelled, so if that doesn’t match everywhere else you spelled income correctly, it’s not going to know where to get the first reference of income.

As you can tell, things can get messed up easily within your code. Misspell anything, use the wrong symbol anywhere, or use the incorrect order of words on any line of code and there’s a high chance that your code won’t do what you expect it to do, or won’t do anything at all!

Every programming language has a different set of syntax rules. It’s a major part of what makes one programming language different from another. Some languages don’t use curly braces at all, they usually phrase their declarations differently, they might not require semi-colons, and more broadly, some languages can do things that others can’t do. All these individual little nuances are a major part of what goes into becoming truly exceptionally good at using a programming language.

Luckily, if you remember back to Page 4 of this series, programming languages each have something called a compiler which inspects the code you write before it’s turned into a computer program. The compiler will usually give you hints, using error messages, about where your mistakes are in your code and sometimes – how you can fix them. This can make the process of writing code much easier! They will often show you the area of the code that has the issue (usually by the line number) and the type of error that you made.

From there, if the compiler’s error messages can’t help, your task may include using a search engine, a documentation website, or a reference book to more fully understand how you can fix the error. StackOverflow.com is an amazing Question/Answer site where programmers ask each other questions and you can find great answers if you get stuck on a difficult problem. We will talk about what compilers do, how to use them, and some great examples of where to find solutions later, but right now it’s important that you know you aren’t completely helpless when it comes to fixing these errors in the software code you write.

Every type of programmer, from the newbie beginner level all the way up to the expert senior-level developer and beyond will make mistakes in their code. The more comfortable you are with accepting this and enjoying the process of fixing your mistakes, the faster you will become at finishing code and progressing to be a better programmer. I’ve found that while you’re learning it’s a great exercise to experiment with code, see what works and what doesn’t. Make mistakes and figure out how to fix them. Test different ways to structure your code and come up with new ideas. You may find a new way to write your code that helps you see things in a different way!

Now that we’ve covered some of the beginner concepts behind the source code of computer programs, let’s explore some of the thinking that programmers use to create code.

(coming soon) Continue to Part 7 “Thinking Like A Programmer: Building an Algorithm” >