Before we start
What is programming ?
When we write programs, we write down
A
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
Syntax is a fancy name for the set of rules that define a programming language. Spoken languages like Irish and
Programming languages have similar rules that define how you have to write things in the language.
To introduce programming and Setanta, let’s look at some simple instructions.
“scríobh
that we can use to write scríobh
action like this:
You can put any 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.
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.
You have two \"
”, like this:
The
We’ve seen that Setanta can write text, but we can do much more than that. 28 + 36 * 2
” and write it on the console.
You can change the 28 + 36 * 2
” with a different maths expression. You can use all the
“
+
” for addition, “-
” for subtraction, “*
” for multiplication and “/
” for division.
You can also use scríobh
in the editor above:
You can also use numbers with decimal points, such as 1.2
or 123.4
We can also use the +
operator to add two pieces of text
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 “codladh
codladh
when we want the computer to
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:
scríobh("Before sleeping")
” and write “Before sleeping” on the console as instructed.codladh(2000)
” and sleep for 2000 scríobh("After sleeping")
” and write “After sleeping” on the console.One of the most
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.
To make a new variable we use the “:=
” symbol. For example
This code makes a new variable called bia
"sceallóga"
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!
bia
, with the value "sceallóga"
."sceallóga"
is stored in the variable bia
, and it uses that value to write “sceallóga” on the console.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.
x
, with the value 2.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
.x
on the console, which is now 12.Change this code so that it writes "bainne"
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!
Comments
>--
” anywhere in the program, anything we write after it on the same line will be ignored by the computer. We call these entriesThe computer onlypays 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.