Introducing conditions[edit | edit source]
So we know how to store values but apart from showing one output or another what can we do? How are they useful? Here the answer, conditionals. Conditionals are a way of doing things only under certain circumanstances (specified by you), let's compare it with someone who will follow all your orders ... let's see ... an slave! So you have your slave but it turns out that your slave it's a bit silly and won't do anything you don't tell it to do and you want it to take to you a mug of coffee, so he will take a mug and fill it with coffee from the kettle, but wait, what if there's not coffee in the kettle? Well he'll just take you a mug of air, so if we want to assure he'll take us a mug of coffee we will have to tell him to make coffee, but only if there's not, because if there's coffee in the kettle it will make a mess. So the orders we passed to him would look something like this:
If there's not coffee in the kettle then make some coffee Take a mug Fill the mug with coffee from the kettle Bring me the mug
Hmm, this looks like pretty much like code, let's try to reproduce it in Nimrod:
if theresNotCoffeInTheKettle(): makeSomeCoffee() takeAmug() fillTheMug() bringTheMug()
Note:You may remember those parenthesis from the previous examples, just try not to worry about them right now.
As you can see there's a new command the if part, what it will do it's see if there's coffee in the kettle, and then do whatever there's after the colon (in this case the instruction to make some coffee). So far we learned two things how to execute commands if a given conditions is met and the fact that a computer it's a dumb (but impressively fast) slave.
More conditions[edit | edit source]
Nice, this way our slave will certainly bring us coffee but what if we wanted our slave to make coffee only if there's coffee? Well let's see an example
if theresNotCoffeeInKettle() and theresCoffee(): makeSomeCoffee() takeAMug() fillTheMug() bringTheMug()
Before continuing explaining let me clarify something, the if expression what does it's to check the if what it's passed to it it's the same as something know as true. true it's a Boolean type (named after mathematician George Boole, the one who had the idea of working with two values true and false) so the operators we are going to see next what they do it's to take one or two of these Boolean type and transform them.
So it's as simple as that the 'and' operator takes two (Boolean) values and returns true only if the two are true, else it returns false.
Now let's return to the first example and consider that our slave does not know the instruction theresNotCofeeInTheKettle() but he knows theresCofeeInThekettle() (the same but inversed), so what we do? Let's "take" the "not" outside
if not theresCofeeInTheKettle(): makeSomeCoffee() ...
As you can see the not inverts whatever it takes, if it takes a true it will return a false, but if it takes a false it will return a true.
We also have 'or' which receives two values (just like 'and') and you probably guessed it, it will return true as long as one of the two operands are true (or both of them).
Joining them all[edit | edit source]
You can use as much of these as you want, you can even repeat them several times. But it may not do those in a correct order so how we instruct him which order it should take , just like in mathematics, with parenthesis.
if not expression1 and expression2:
What this does it's first to apply the 'not' to expression1, then it will apply the "and" to the inverse of expression1 (because of the 'not', remember) and expression2. But we wanted something different, we wanted to do a 'and' from expression1 and expression2, and then invert that, how it's that done?:
if not (expression1 and expression2):
If you managed to reach here congratulations now you know pretty much everything about Booleans, now my tip is that if you feel somehow tired take a rest or even go to sleep, because you deserve it.