Chapter 2: Statements

Most programming languages have the concept of a statement. A statement is a command that the programmer gives to the computer. For example:

print "Hello, world!"

This command has a verb (“print”) and other details (what to print). In this case, the command print means “show on the screen,” not “print on the printer.” The programmer either gives the statement directly to the computer (by typing it while launching a special program), or creates a text file with the command in it. You could create a file called “hi.txt” using a program like Notepad, put the above command in it, and give the file to the computer. (The details of how to do all this are language- and system-specific, so they won’t be covered here. This is just an introduction to the concepts.)

If you have more than one command in the file, each will be performed in order, top to bottom. So the file could contain:

print "Hello, world!"
print "Strange things are afoot..."

The computer will perform each of these commands sequentially. It’s invaluable to be able to “play computer” when programming. Ask yourself, “If I were the computer, what would I do with these statements?” If you’re not sure what the answer is, then you are very likely to write incorrect programs. Stop and check the manual for the programming language you’re using.

In the above case, the computer will look at the first statement, determine that it’s a print statement, look at what needs to be printed, and display that text on the computer screen. It’ll look like this:

Hello, world!

Note that the quotation marks aren’t there. Their purpose in the program is to tell the computer where the text begins and ends, just like in English prose. The computer will then continue to the next statement, perform its command, and the screen will look like this:

Hello, world!
Strange things are afoot...

When the computer gets to the end of the text file, it stops and waits for further instructions. This whole process is called running or executing the program. When you run a program, the computer looks at your program file and performs the appropriate actions for each statement.

There are many different kinds of statements, depending on which programming language is being used. For example, there could be a beep statement that causes the computer to output a beep on its speaker, or a window statement that causes a new window to pop up.

Also, the way statements are written will vary depending on the programming language. The programming language Python uses statements that look like the above. In C you would write:

puts("Hello, world!");
puts("Strange things are afoot...");

Note a few differences:

  1. The parameters (the text in quotation marks that print uses to determine what to display) are surrounded by parentheses.
  2. Each statement is terminated by a semicolon.
  3. The verb print is called puts, for “put string”. The word string is the computer term for some text. It comes from “a string of letters.” The “put” means “put on the screen.”

These differences are fairly superficial. The set of rules like the first two is called a programming language’s syntax. The set of verbs is called its library. It’s a bit like a natural language’s grammar and vocabulary.