Jump to content

Nimrod Programming/IfExtended

From Wikibooks, open books for an open world

Else...

[edit | edit source]

So we know how to use the if command to it's fully extent, now it's time to continue learning. Let's take again the slave example. We want our slave to make coffee if there's not and there's coffee. But you know our slave is a dumb one and won't tell us he could not make coffee, so what then? we could consider this:

if theresCoffee(): makeSomeCofee()

if not theresCofee(): echo ("I couldn't make coffee")

Well, that would do the trick but, I'm not happy with it, if we changed the first condition you should change the second too (when programming the lesser you must change the better) and anyway, it adds unnecessary clutter so I propose this:

if theresCoffee(): makeSomeCofee()

else: echo ("I couldn't make coffee")

So what we did here is add what we call an else clause, what this clause does is to execute the code it has when the if fails. Contrary to the if it has no condition as is executed always ( when the if fails, remember).

So here it will execute always something wither the code inside the if or the code inside the else.

There's something more I have to teach you about this, so let's return to the slave example. Our slave reports us we don't have coffee, but let's get an step further and since there's a coffee shop near our house ( or mansion, after all we already have a slave :) ), and we want him/her to go and buy coffee provided we have enough money:

if theresCoffee(): makeSomeCoffee()

elif theresEnoughMoney():

  buyCoffee()

  makeSomeCoffee()

else: 
  echo ("I couldn't make coffee")

Hmm, now we have two strange things there, that elif thing and the fact we have two actions after the if ( until now we only had one ). We will leave the later for next chapter. So that elif word is the sum of else and if ( indeed elif means ELse IF ), so what this does is to execute the actions inside the elif provided that that it's condition is met. That last piece of code would read: if there's enough code make some coffee, if there's not but there's some money then buy coffee and make some coffee, finally, if there's neither enough money nor enough coffee' then report the slave couldn't make coffee. If you want to know how much important are the elif and else clauses here's the se code without them ( translated the elifs and elses to ifs):

if theresCoffee(): makeSomeCoffee()

if not theresCoffee() and theresEnoughMoney():

  buyCoffee()

  makeSomeCoffee()

if not theresCoffee() and not theresEnoughMoney(): 

  echo ("I couldn't make coffee")

As you can see we can use only ifs, this program would behave exactly like the one without ifs. Now what are you losing here is readability ( something very important because it is said people write code once but read it lots of times ), also there's an implicit rule of programming broken here: repeat yourself as less as possible, this is this way because if you want to change something like the if clause in this example and you have it everywhere even though you might replace , it's still better not to do so as replace might do you a pair of bad jokes.