You will often want to change the flow or outcome of your script based on a condition or series of conditions. Is one number bigger than another? Is it the last day of the month? Does a string contain a certain value?
The if statement
Most programming languages make use of conditionals – or the if statement – and with them your code can make its own decisions based on the values of your data.
Consider the following mashlet:-
Natural
set x to 10
printline "x is {{ x }} which is less than 10"
Standard
x = 10
printline("x is {{ x }} which is less than 10")
x is 10 which is less than 10
If you run this within the MaSH editor you’ll see the text x is 10 which is less than 10 print in the console. Clearly this isn’t true because x is actually 10. We don’t want to print the statement if it isn’t true – that would be lying – so we need a way to check a condition and only run our printline statement under certain conditions.
Natural
set x to 10
if x is less than 10
printline "x is {{ x }} which is less than 10"
end
Standard
x = 10
if (x < 10) {
printline("x is {{ x }} which is less than 10")
}
Output
Running our code again we see that nothing is written to the console – honesty has prevailed! So what’s happening here? Conditionals allow us to only execute blocks of code if a certain condition (or group of conditions) is true. In MaSH conditionals can use both natural language and more conventional operators. These are shown below:-
Natural Language | Standard |
---|---|
is | == |
is not | != |
is less than | < |
is less than or equal to | <= |
is more than | > |
is more than or equal to | >= |
The else if statement
Of course, we may also want to do something if x is more than 10. Since x is a variable and variables’ values can change, we should leave our first check but we can add an else if to our if statement with a different condition like so:-
Natural
set x to 20
if x is less than 10
printline "x is {{ x }} which is less than 10"
else if x is more than 10
printline "x is {{ x }} which is more than 10"
end
Standard
x = 20
if (x < 10) {
printline("x is {{ x }} which is less than 10")
} else if (x is > 10) {
printline("x is {{ x }} which is more than 10")
}
Output
x is 20 which is more than 10
The else statement
MaSH now tells us that 20 is indeed more than 10. However if we reset x to its original value of 10 and run the above script you’ll see that nothing is printed. That’s because we have a condition for less than and a condition for more than but no condition which will be true if x is exactly 10. We can solve that with a final else:-
Natural
set x to 10
if x is less than 10
printline "x is {{ x }} which is less than 10"
else if x is more than 10
printline "x is {{ x }} which is more than 10"
else
printline "x is 10"
end
Standard
x = 10
if (x < 10) {
printline("x is {{ x }} which is less than 10")
} else if (x is < 10) {
printline("x is {{ x }} which is more than 10")
} else {
printline("x is 10")
}
Output
x is 10
The else clause is a bit like a final catch-all – if none of our conditions have been satisfied our script will fall back to whatever code we have in there.
Stacking Else If Clauses
Note that you are not just limited to a single else if clause within a conditional, they can be stacked as shown below:-
Natural
set x to 0
if x is 0
printline "x is 0"
else if x is less than 10
printline "x is {{ x }} which is less than 10"
else if x is more than 10
printline "x is {{ x }} which is more than 10"
else
printline "x is 10"
end
Standard
x = 0
if (x == 0) {
printline("x is 0")
} else if (x < 10) {
printline("x is {{ x }} which is less than 10")
} else if (x > 10) {
printline("x is {{ x }} which is more than 10")
} else {
printline("x is 10")
}
Output
x is 0
Chaining conditions
Often we have to make life decisions based on more than one factor being true at the same time – if we are out of milk AND there is no delivery tomorrow I shall pop to the shops. There are also decisions which depend upon one factor or another being true – if we are out of milk OR we are out of bread I shall go shopping.
We can also make more complex decisions in our mashlets by chaining conditions together using operators. As elsewhere in MaSH you can use the natural language forms and and or or the more conventional && and ||.
The following mashlet will only print a message if our variable is between 5 and 10.
Natural
# We only want to print a message if our variable is more than 5 but less than 10
set x to 7
if x is more than 5 and x is less than 10
printline "x is {{ x }} which is just where we want it to be"
end
Standard
# We only want to print a message if our variable is more than 5 but less than 10
x = 7
if (x > 5 && x < 10) {
printline("x is {{ x }} which is just where we want it to be")
}
Output
x is 7 which is just where we want it to be
To accomplish this we use the and operator. Why would we not use the or operator?
Let’s set the value of x to 2. Our first condition x is more than 5 would be false (2 is more than 5) but our second condition x is less than 10 would be true (2 is less than 10). Remember that when using the and operator all of our conditions must be true for our code to be executed whereas with the or operator we only require one of them to be true.
You can chain as many conditions as you like but remember that the more conditions that you have the more complex to read your code will become.