2.3. Writing Statements

I've talked a lot about writing many small instructions to the computer such as “add one number to another” or “display the word hello on the screen”. When we actually write source code, we call each of these a statement, that is, a few words or symbols arranged to make one self-contained specific instruction.

A statement does something. It's a code that has an effect in your program and that means a statement must be complete. It must make sense in the same way that a written or spoken sentence must make sense.

If I said

“change the color of…”, you'd wonder the color of what exactly. If I said

“Add 99 to…”,

“Print…” ,

“Move…”,

you'd understand these words fine, but you'd recognize these as being incomplete. So, there's a difference between just knowing words of a programming language and writing a statement that uses those words.

Now usually statements are just a few words that often fit easily on a single line in your source code to say what is being done and what is it being done to.

For example,

“Change the color of the background to light blue”.

“Add 99 to whatever the current score is”.

“Print this current document to the default printer”.

“Move this spaceship graphic 5 pixels to the right”

Again, all these are specific small self contained instructions.

Now occasionally a statement can be just one word. The same way that you can have a complete sentence in English of just one word, although that's usually in response to something.

Image

Fig 2.3.1: Sample one-word statements written in programming

But most of the time we need a few words, a few pieces to our statements. So, these are the kinds of statements we can write, but as you can probably tell, I'm not writing them here using any, particular programming language, just this informal description.

So, let's just take one:

“Add 99 to the current score”

and see this written as a statement in a few different programming languages.

Now this always assumes that, whatever language we’re using, maybe for example we're writing a simple game, we've decided we want to keep track of and change a number that we'll call score, just as someone might scribble a score on a chalkboard and change that score while a game is being played.

So, let's write this statement in a few languages, beginning with an older programming language called COBOL:

COBOL Syntax:

Add 99 to score.

Applescript Syntax

set score to score + 99

Swift, Ruby, Python (and others) Syntax:

score = score + 99

The last syntax (score = score + 99) is a very, common way of doing it but it can confuse people new to programming because they think it looks like one of these algebraic equations they have to figure out somehow. No, it's nothing like that. This is our statement. This is our instruction. We're not asking anything. We are telling the computer to do something here. The equal sign in this statement is not a question. It is a command.

You can think of this equal sign here as meaning “is now made equal to”, so when we execute this one statement, the score is now equal to whatever it was previously plus 99. In a lot of other languages, the syntax is almost identical except we need to add a semi-colon at the end.

C, C++, PHP, Java (and others) Syntax:

score = score + 99;

You see, no matter what language, it's very common to write every separate statement on a separate line in your source code file.

Now sometimes a statement can be split across multiple lines. Usually that's not required; it just makes things easier to read.

Ending Statements

In many languages, having statements on separate lines is not enough. You must also explicitly end each statement with a special character to indicate the statement is now complete, just like having a period or full stop at the end of a sentence in English.

Image

Fig 2.3.2: Ending statements with semi-colon

It's what tells the compiler: this statement is finished. Go ahead and convert it into machine code. The most common character to end the statement is the semi-colon. You'll find this used in C language and in many languages based on or influenced by C, for example, Java, C#, PHP, Objective-C and C++. It is very common to see this semi-colon whereas in COBOL, or at least some versions of COBOL, it's the same idea, but then you would end the statement with a period.

In other languages like Ruby, Python and Swift for example, you don't need a special character. You just end each statement with a line break. You don't need a period. You don't need to semi-colon. Just by having separate lines, each one is treated as a complete statement.

Again, right now, don't worry about memorizing what language exactly does what. That's not important here. We're working on recognizing what all these languages need to do. They might do them differently. They might have a slightly different syntax, but they all do them.

Even without memorizing any of the syntax, you start to recognize that a lot of this is shared across languages. If you learn how to add one number to another number in Ruby, it's going to be the same in Python, in Swift, and almost the same in C, Java, PHP and many others.

So, let's move on. I'm going to talk for a moment about the benefits of writing our instructions so that they're not targeted at one specific programming language. In the next section, we're going to move on to the syntax you don't see (the invisibles) such as the tabs, the blank lines, the spaces or whitespace of a programing language.