It’s decision time

Let’s make a choice.

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 “” statement. “Má” means “if” in English. The statement allows us to make a decision, It allows us to check if something is true, and do one thing if it is, and another if it isn’t.

The syntax for a statement is as follows

 < some check > {
    <code to execute if the check is true>
}  {
    <code to execute if the check is false>
}

The section is optional

Example

Let’s start with the program we wrote before to ask the user their name:

ainm := ceist("Cad is ainm duit?")
scríobh("Dia duit", ainm)

We use the ceist action to ask the user for their name, store the result in the ainm variable, and then use the scrí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 compare two pieces of text, and we use the statement to control the decision that’s made:

We can run this code, if we enter our name as “Setanta”, then the program prints “Fáilte romhat Setanta”, otherwise it prints “Dia duit” as before.

Demo

The statement in action

Closer look

This is the code we wrote:

ainm := ceist("Cad is ainm duit?")

 ainm == "Setanta" {
    scríobh("Fáilte romhat Setanta")
}  {
    scríobh("Dia duit", ainm)
}

On line 1, we get the name of the user as before.

On line 3 we wrote ainm == "Setanta". This makes a check if the ainm variable is equal to "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 to define what to do if the check was false.

On line 6 we filled in scríobh("Dia duit", ainm) as the code to run if the check was false.

Challenge

To test your understanding, try and change the following program so that it writes “Chocolate is the best” if the user says “Chocolate” is their favourite food, but says “I love that food” if they say something else.

Remember that Setanta doesn’t require you to type the fadas, so you can type ma or no if you need to

Click here to see the answer

Booleans

Setanta has two special values, called booleans (named after George Boole who was professor of Mathematics at University College Cork). Those values are fíor and bréag. (As usual you can ignore the fadas and type fior or breag)

fíor” translates as “true” and “bréag” is short for “bréagach” which translates as “false”.

The results of expressions like x == "Setanta" are booleans.

Comparisons

We used the == operator to check equality in our earlier example, but we can do much more.

Setanta allows us to use operators to compare numbers and text in a number of ways.

Operator Meaning
== Check if two values are equal.
!= Check if two values are not equal.
> Check if the left value is greater than the right.
< Check if the left value is less than the right.
>= 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.

Challenge

Here is some partially filled out Setanta code. Replace the “<replace-me>” text with the correct operator to check if 100 is less than 20 * 6 - 18 * (2 * 1/2)

Click here to see the answer.

You should see that the code prints “fíor”, which is correct.

And/Or/Not

What if we want to check more than one thing, for example, what if we want to check if an age is either greater than 20, or less than 10. How do we combine checks?

We do this with three powerful operators called “and” (&), “or” (|) and “not” (!).

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” and then “bréag” again.

This is because:

  1. On the first line, both "hello" == "hello" and 5 > 2 are true, so the result is true (fíor).
  2. On the second line, "hello" == "hello" is still true, but, 5 > 6 is not true. So the result is false (bréag).
  3. On the third line, "hello" == "goodbye" is not true. So we know both sides aren’t true, and the result must be bréag again.
  4. On the third line, "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.

Or

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:

  1. On the first line, both "hello" == "hello" and 5 > 2 are true, so the result is true (fíor).
  2. On the second line, "hello" == "hello" is true, and 5 > 6 is false. But as at least one of them is true, the final answer is fíor.
  3. On the third line, "hello" == "goodbye" is false, and 5 > 2 is true. But as at least one of them is true, the final answer is fíor.
  4. On the fourth line, "hello" == "goodbye" is false, and 5 > 6 is false. As neither of them are true, the final answer is bréag.

Not

The final operator for booleans we’ll look at is the “not” (!) 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:

Chaining

You can combine and into one statement to chain together 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 age, and tells them if they are an adult, a teenager or a child.

Try it out with some different ages, try an adult age, a teenager age and a child’s age!

Explained

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 “go uimhir” which means “to number”. This action takes text and converts it to a number. 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. 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 statement.

  1. First it checks if aois is greater than 18. If it is, it will write "Is duine fásta thú" and then skip to the end of the statement.
  2. If the first check failed, the computer will try the next check, which is checking if aois is greater than or equal to 13. If that’s true then it will write "Is déagóir thú" and skip to the end of the statement.
  3. Finally if both of those checks failed, then the user must be younger than 13, so it prints "Is páiste thú".