So far all the programs that we’ve written always do the same thing. The code starts at the top and works its way to the bottom. If we want to write programs that are more interesting we need to allow our code to make decisions.
Our main tool for this is called the “mámá statement allows us to make a
The syntax for a má statement is as follows
má < some check > {
<code to execute if the check is true>
} nó {
<code to execute if the check is false>
}The nó
Let’s start with the program we wrote
We use the ceistscríobh action to write it on the console.
Let’s expand the program to print a special message if the name entered is “Setanta”. We use the == operator to má statement to
We can run this code, if we enter our name as “Setanta”, then the program prints “

má statement in actionThis is the code we wrote:
ainm := ceist("Cad is ainm duit?")
má ainm == "Setanta" {
scríobh("Fáilte romhat Setanta")
} nó {
scríobh("Dia duit", ainm)
}On line 1,
On line 3 we wrote má ainm == "Setanta". This makes a check if the ainm variable is "Setanta".
On line 4 we filled in scríobh("Fáilte romhat Setanta") as the code we want to be ran if the check was true.
On line 5 we used the keyword nó
On line 6 we filled in scríobh("Dia duit", ainm) as the code to run if the check was false.
To test your
Remember that Setanta doesn’t require you to type the fadas, so you can type ma or no if you need to
Setanta has two special values, called booleans (named after George Boole who was professor of Mathematics at University College Cork). Those values are fíorbréag. (As usual you can ignore the fadas and type fior or breag)
“fíorbréag” is short for “
The results of expressions like x == "Setanta" are booleans.
We used the == operator to check equality in our earlier example, but we can do much more.
Setanta allows us to use operators to
| Operator | Meaning |
|---|---|
== |
Check if two values are equal. |
!= |
Check if two values are not equal. |
> |
Check if the left value is |
< |
Check if the left value is |
>= |
Check if the left value is greater or equal to the right. |
<= |
Check if the left value is less or equal to the right. |
Using these operators gives fíor if the check is true, and bréag if the check is false.
Try running this code:
You can see that it prints “bréag” on the console. This is because 5 is not less than or equal to 3.
Here is some partially filled out Setanta code. 100 is less than 20 * 6 - 18 * (2 * 1/2)
You should see that the code prints “fíor”, which is correct.
What if we want to check more than
We do this with three &), “|) and “!).
The & operator is the “and” operator. It returns fíor if both sides are true. For example:
If you run this code you will see that it prints “fíor”, then “bréag”, “bréag” and then “bréag” again.
This is because:
"hello" == "hello" and 5 > 2 are true, so the result is true (fíor)."hello" == "hello" is still true, but, 5 > 6 is not true. So the result is false (bréag)."hello" == "goodbye" is not true. So we know both sides aren’t true, and the result must be bréag again."hello" == "goodbye" is not true and 5 > 6 is not true. So we know both sides aren’t true, and the result must be bréag again.The | operator is the “or” operator. It returns fíor if either side is true. For example:
If you run this code you’ll see that it prints “fíor”, then “fíor”, then “fíor”, and finally “bréag”.
This is because:
"hello" == "hello" and 5 > 2 are true, so the result is true (fíor)."hello" == "hello" is true, and 5 > 6 is false. But as at least one of them is true, the final answer is fíor."hello" == "goodbye" is false, and 5 > 2 is true. But as at least one of them is true, the final answer is fíor."hello" == "goodbye" is false, and 5 > 6 is false. As neither of them are true, the final answer is bréag.The !) operator. The ! operator is very simple. It takes a boolean and inverts it. So !fíor is bréag and !bréag is fíor.
We can use this to check that something is false:
You can combine má and nó into one statement to chain together má statements. This allows you to check different conditions one after the other, with the first successful check being executed.
For example; Here is some code that asks the user for their
Try it out with some different ages, try an adult age, a teenager age and a child’s age!
There are a few things to notice about that program. First thing we should explain is the first line. We used the go_uimh action, which we haven’t seen before.
go_uimh is shorthand for “ceist action returns the text the user typed. We need to turn it into a number so that we can compare it with 18 and 13 to decide if they are an adult, a teenager or a child. In our case, the ceist action returns the text the user typed. We need to turn it into a number so that we can compare it with 18 and 13 to decide if they are an adult, a teenager or a child.
After the user has given us their age, it gets stored in the aois variable, and the computer moves on to the má statement.
aois is greater than 18. If it is, it will write "Is duine fásta thú"aois is greater than or equal to 13. If that’s true then it will write "Is déagóir thú""Is páiste thú"