Monday, September 26, 2011

The Marvelous IF-ELSE Life of the King's Turtle

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.

Fido, the King's prized pet turtle, lived a charmed life. He spent his days in the garden fountain, swimming and sleeping. He did not have any magic powers, aside from the ability to amuse himself for an hour by staring at a pebble, but King Fredrick was quite fond of him. The castle's servants took good care of him. They made sure that his fountain was always mostly clean -- Fido did enjoy the occasional patch of slime on the ground.

Fido lived by a series of simple rules. In fact, since his brain was roughly the size of a pebble, they were incredibly simple IF-ELSE style rules. These rules made up Fido's entire daily routine. For example, he had very simple logic to determine when he ate:

IF he is hungry

This logic worked out great for Fido, because he ate when he was hungry. And, as a natural consequence, he did not eat when he was not hungry. It was quite a good system.

For some aspects of life, the IF statement could have two different actions depending on the condition. For example, when he was swimming:

IF the fountain is on
play in the fountain
swim around the large rock

Obviously, Fido enjoyed the fountain more than the rock.

Sometimes the decisions would be complex enough to require a series of chained IF-ELSE statements:

IF it is sunny
sit in the grass
ELSE IF it is warm
go swimming

The gardener responsible for taking care of Fido often joked that: "All that turtle does is eat, sleep, and swim", which was not far from the truth. The logic that ruled Fido's life consisted of about fifty different actions contained within chained and nested IF-ELSE statements.

A scholar had once spent a week studying Fido, and he had managed to record the entire logic for Fido's routine on a single scroll of parchment. If Fido had been intelligent enough to understand what that meant, he might have been offended. Instead, he sat in the grass -- it was sunny.

Then, one day, the unthinkable happened. The gardener decided that Fido was probably getting bored, so he added a SECOND large rock. This addition threw off Fido's IF-ELSE based routine completely. It took almost a full week for Fido to come up with a new routine that suited his new environment. In the end, he added another IF-ELSE:

IF he is closer to the right rock
swim around the right rock
swim around the left rock

Thus order was restored to his life.


For more discussion of IF-ELSE also see Learning IF-ELSE the Hard Way.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.