Syntax, statements and expressions

Syntax

Syntax is the set of rules about how to use a particular language. In the same way that English has rules, so do programming languages.

Program statements have to be used in certain ways for the computer to recognise them. For example, in Python 'print' is always typed in lower case and then followed by an argument - the thing you are asking it to print. If you don't type print ("Hello World") exactly as it is shown here, you will get a syntax error.

Some programming languages have been developed to help avoid syntax errors. Scratch is a programming language that is built out of visual blocks rather than written language. This avoids syntax errors by removing the need to type in statements.

Statements

A statement is a single action in a computer program.

In a recipe for making a sandwich, a statement could be written as 'place bread down' or 'spread butter'. A simple statement in a computer program could be this one, written in Python:

print ("Hello World")

Statements can be more complex and contain many other smaller elements. Here is another example in Python:

if (c!="q") or (c!="quit") : print (“alert”)

In a computer program, statements might include some of the following actions:

  • input data - for example, ask the user a question or collect a value from a sensor
  • process data - for example, add two values together or make a decision when a value reaches a certain amount
  • output data - display some results or play a sound

Expressions

Expressions are used within statements when working with values. For example, 2+2 is an expression that returns the value of 4. In a computer program, an expression to work out your age might look like this:

AgeNextYear=CurrentAge+1
Move on to Test
next