The IF-ELSE statement is a computational construct that allows the program to branch off and execute one of two different blocks of code. The IF statement starts by evaluating a Boolean clause. If this clause evaluates to true, the block of code conditioned on this IF statement is executed. If an ELSE statement is present, it can provide a block of code to execute in the case where the Boolean clause evaluates to false.
Ann had learned the value of IF-ELSE statements at a very early age. When she was only three years old, she was given VERY strict instructions from the castle's head chef NOT to randomly eat things in the kitchen. Specifically, she was told:
IF the food is on the 'finished' table
You can eat it.
ELSE
Do not eat it.
Of course, as any three year old is bound to do, Ann ignored these instructions. She would sneak into the kitchen and eat pieces of fruit off of the chef's prep table. Each time that the chef caught her, he would give her a lecture about obeying the IF statement. His lectures would last a full ten minutes and include at least one remark about "kids these days". Ann enjoyed listening to him describe the branching logic of the IF statement almost as much as she enjoyed sneaking fruit. In fact, some days she even made sure that she was caught so that she could listen to his rants.
Then one day, the chef was preparing eight-chili soup for dinner. As the name implies, the ingredients involved eight different types of chilies, including the ultra-hot Dragonia chili. Unfortunately for Ann, the Dragonia chili looked remarkably like a strawberry. She snuck two, and popped them in her mouth. The resulting pain was intense. Ann's eyes did not stop watering for three weeks. After that, Ann never disobeyed the chef's IF statement again.
As she grew older and more responsible, the chef added a chained IF statement to allow Ann access to a wider variety of food:
IF the food is on the 'finished' table
You can eat it
ELSE IF the food is on the 'raw ingredient' table AND you ask first AND the chef says "yes"
You can eat it
ELSE
Do not eat it.
Having learned her lesson from the chili pepper incident, Ann dutifully obeyed this new expanded rule. Food that was on the finished table was always okay to eat. Otherwise, if the food was on the 'raw ingredient' table, she knew that she would have to ask the chefs first. The logic was simple and covered the important cases.
Of course, those rules still did not stop Ann from trying to trick her younger brother into eating Dragonia chilies. She even succeeded -- twice.
-------------------------------------
For more IF-ELSE fun, see The Marvelous IF-ELSE Life of the King's Turtle.
Using an ELSE clause that is the logical compliment of the IF clause is a little misleading, since you could have omitted the ELSE clause to achieve the same effect. The basis for the story is great, though!
ReplyDeleteThat is a very good point. In these cases the ELSE clause is not actually necessary. However, the chef really did enjoy explaining things that way. I think it was part of his culinary training.
ReplyDeleteIndeed, the chef's training (based on 'always give answers&results' rather than 'just give imperative commands') always uses ELSE, and that training prevents incidents like The Day the Zune Stood Still [ http://www.zuneboards.com/forums/showthread.php?t=38143 ]
ReplyDelete"Using an ELSE clause that is the logical compliment of the IF clause is a little misleading, since you could have omitted the ELSE clause to achieve the same effect."
ReplyDeleteMore precisely, if you use an ELSE clause which amounts to "do nothing", then you could have omitted the ELSE clause to achieve the same effect.
Even that is true only in an imperative programming style; in a functional programming style, omitting the ELSE clause means "don't produce any result at all," which is almost never what you want.
Anyway, it might be good to augment this story with another one in which the THEN and ELSE clauses both require you to do or specify something. For example, "What shall we have for breakfast? IF it's a weekday, THEN milk and cereal; ELSE pancakes and maple syrup."
Good points. I added a new story with an alternate example of IF-ELSE:
ReplyDeleteThe Marvelous IF-ELSE Life of the King's Turtle
http://computationaltales.blogspot.com/2011/09/marvelous-if-else-life-of-kings-turtle.html