What is programming?

Programming is…

Before we start learning how to write and use Setanta, we should ask:

What is programming?

Computers today are very smart, they can do calculations no human could ever do in their head, and in the blink of an eye. However, computers need a lot of help, and programming is how we help them.

When we write programs, we write down instructions for the computer to follow. The computer will read our instructions and follow them exactly.

What’s a programming language?

A programming language is a language that the computer can understand, and we use it to write our instructions.

Modern programming languages are also designed so that people can understand them too. Setanta is one of those languages, but it’s a little bit different.

Almost every programming language is designed to be close to English, but Setanta is different. When we write Setanta code, it’s almost like we are writing the instructions in Irish.

Syntax

Syntax is a fancy name for the set of rules that define a programming language. Spoken languages like Irish and English also have a syntax, for example in English and Irish, every question must end with a question mark.

Programming languages have similar rules that define how you have to write things in the language.

Simple instructions

To introduce programming and Setanta, let’s look at some simple instructions.

Scríobh

Scríobh” is the Irish word for “write”. Setanta has an action called scríobh that we can use to write text onto the console. We use the scríobh action like this:

scríobh("Your text in here")

You can put any text you like in between the double quotes (") and scríobh will write it out on the console. Try it out here!

Change the text “Put your text here” to something else and run the code. See if you can make it print your name! Here’s a quick GIF of something you could change it to.

Changing scríobh text

What if I can’t type “í”?

Setanta understands that you might not know how to type fadas (áéíóú), so all of Setantas built-in actions and keywords work just as well without the fadas.

Try replacing scríobh with scriobh (no fada) in the code above and you’ll see that it still works.

What if I want to use a double quotes (") in my text?

You have two options to use a double quote in your text, the first is to “escape” the double quote by writing “\"”, like this:

"This text has \"double quotes\" in it"

The second option is to use single quotes (’) to define your text, like this:

'This text has "double quotes" in it'

Calculations

We’ve seen that Setanta can write text, but we can do much more than that. For example, Setanta can do calculations for you! Run this code and see how Setanta does all the maths for you. Setanta will calculate the answer of “28 + 36 * 2” and write it on the console.

You can change the values of the numbers or add in more. Try replacing “28 + 36 * 2” with a different maths expression. You can use all the symbols that you already know, for example:

+” for addition, “-” for subtraction, “*” for multiplication and “/” for division.

You can also use brackets to build up complicated expressions, Try printing these with scríobh in the editor above:

1 + 2 * (3 - 4) / 5
1 + 2 - (3 + (4 - 5))

You can also use numbers with decimal points, such as 1.2 or 123.4

Adding text

We can also use the + operator to add two pieces of text together. Try it out:

Comments

Sometimes we want to write things in our programs that we don’t want the computer to try and understand. Often these are just little notes to ourselves, or explanations of what something does.

Luckily there’s a way to do this. If we write “>--” anywhere in the program, anything we write after it on the same line will be ignored by the computer. We call these entries comments. Here’s an example:

The computer only pays attention to the first line, and ignores the second.

Comments usually go until the end of the line, but we can end a comment early by typing “--<”, these comments are called inline comments.

scríobh(1 + >-- Inline comment --< 2)

Combo Time

When the computer is following the instructions in our Setanta program it starts at the top and works its way down the list. This means if we want to do one instruction after the other, all we have to do is put it on the next line. Check out this code:

If you run this code, it will first print “Before sleeping”, then it will pause for 2 seconds and print “After sleeping”. This is because we used the codladh action. “Codladh” translates into English as “sleep”. We use codladh when we want the computer to wait for a while before moving on to the next instruction.

The codladh action takes a number of milliseconds (in this case 2000), and when the computer reaches the codladh instruction, it will wait for that many milliseconds before proceeding.

The steps the computer takes are:

  1. Read the first line “scríobh("Before sleeping")” and write “Before sleeping” on the console as instructed.
  2. Read the second line “codladh(2000)” and sleep for 2000 milliseconds (2 seconds).
  3. Read the third line “scríobh("After sleeping")” and write “After sleeping” on the console.
  4. Finished.

Variables

One of the most important abilities that a computer has is memory. When we write a program, we can ask the computer to remember certain values, and we can ask it to recall them later. To do this we use something called a variable.

A variable is like a container that we can put a value inside. We can name the container and use the name when we want to refer to the container.

Making variables

To make a new variable we use the “:=” symbol. For example

bia := "sceallóga"

This code makes a new variable called bia, and stores the value "sceallóga" in that variable. Now if we called scríobh(bia) it would remember that we asked it to store “sceallóga” in the variable bia, and it would write “sceallóga” on the console.

Try it out now!

Explanation

  1. The first line creates a new variables bia, with the value "sceallóga".
  2. On the second line, the computer remembers that "sceallóga" is stored in the variable bia, and it uses that value to write “sceallóga” on the console.

Updating variables

When you make a variable, it’s value is not fixed forever. You can change the value of the variable throughout your program. When we want to change the value of a variable we use a single equals sign (=). This is different from the := symbol used to make a new variable.

Try running this program here:

Notice how the first time ainm is printed, the value is "Setanta", but the second time it is "Cú Chulainn". This is because the value of the variable has been changed.

We can actually make reference to the old value of a variable when we update a variable. Take a look at this code:

When this code is run it writes 12 on the console.

Explanation

  1. The first line creates a new variable called x, with the value 2.
  2. The second line tries to update x with a new value, which it gets by calculating x + 10. This evaluates to 12 because the current value of x is 2. We then store this new value of 12 into the variable x.
  3. The third line then writes the value of x on the console, which is now 12.

Challenge

Change this code so that it writes "bainne" on the console:

Click here for the answer

What’s next?

Now that we’ve seen some simple instructions for using the console, we’ve explored how to sequence instructions and learned how to store values in memory, we can use these new abilities on the stage!