Dark theme

What's in a language

[ << ]

Most languages work the same way and have the same bits you can use to solve problems. Below are outlines of these bits plus examples from Scratch and Python.

It is worth noting that all that was needed to write the programs that landed people on the moon was the first four: variables; operators; branching; and loops. The rest came later.

1) Variables: these work like "x" in algebra. They are made up of a label/name and a value this label/name is attached to. Whenever we use the name, we get the value. They are used to give a program flexibility and for storing the answers to maths and other questions.

For example, your program might ask a user their name and store it in a variable so that the program can later produce personalised messages like "I'm sorry, Dave, I'm afraid I can't do that" without having to have "Dave" written into your code everywhere. If we'd written "Dave" into our code – what we call "hardwiring" – it would be useless for anyone else; much better for the program to ask them their name when they start it and store the name for later use.

Once you have a variable set up, you can use its name and get whatever is associated with it. So, for example, we could do:

x = "Hello World"
say x

and the computer would print "Hello World" to the screen. Note that it doesn't matter to the computer what the name is, but we usually call it something sensible so we know what it is. In the case above "text" would be better than "x", whereas for numbers "x" might be better.

Here's setting up a variable with a value in Scratch and Python:

Scratch:
Screenshot: Scratch variable setup
Python:
text = "Hello World"

2) Operators: these change or compare variables. So, for example, when variables are numbers, you can do maths on them with "+" "-" etc. or compare them with < (less than) or > (greater than). As "=" in most languages makes a variable equal some value, to check whether two things are equal, you often use "==" (one exception is Scratch, which uses "=" for both; see below).

Screenshot: Scratch variable mathsx = 2 + 2

3) Branching: this lets you say "if something is true, do one thing, else do something different.

Screenshot: Scratch if-else block

if text == "Hello World":
   print (text)
else:
   print "I haz a sad"

4) Loops: this lets you repeat something a number of times.

Screenshot: Scratch loop block

for range(1, 3):
   print ("Hello World")

5) Events: code triggered when something happens.

Screenshot: Scratch event block

def onKeyPress(event):
   if event.char == '<Space>':
      print ("Hello World")

6) Procedures: little bits of code someone else has written you can reuse, for example, code to get today's date. Their code is hidden away inside a single name that you can use to run it. This is called "calling" a procedure. You can also write your own.

In the examples below, current year and strftime("%Y") are both calling procedures written by other people to get the current year. They hide a lot of complicated code behind these short phrases. When the computer sees a name it recognises it runs off and executes the code behind the name, returning any answer to the place where it was used. The code then carries on as normal. Procedures are called "functions", "subroutines", or "methods" in some languages, and some languages have different names depending on what they are used for: so, for example, Python calls procedures "functions", except for some special cases where it calls them "methods".

Screenshot: Scratch getting date

print (time.strftime("%Y"))

7) Objects: variables attached to code rather than words or numbers. In the examples below, "sprite1" is a load of code all bundled up together, including its own variables and procedures, so you can have variables inside other variables.

Screenshot: Scratch getting date

x = sprite1()

8) Libraries: collections of variables, objects and procedures written by other people that you can draw into your code ("import" or "link") to do complicated jobs. Libraires save you having to write massive amounts of code: you just write the code to hold together and run the code you get from libraries. Libraries are sometimes called "packages", "modules", "dlls" in different languages.

Scratch doesn't have libraries

In Python, the time in:
print (time.strftime("%Y"))
is a library.

With these eight basic things, and their related code, you can build pretty much every program in existence. You put them together to make instructions or "statements" about what the computer should do, and put these together to make a program.

We'll now have a quick look at how we'd use these components to build a program.