image Tutorial on Advanced Conditional Statements 2

When writing more complex conditional statements, it is easy to fall into logical errors. For example, consider the following game plans:

There is one fox, one rabbit, and 30 carrots.
The fox runs around and the rabbit runs around.
When the rabbit hits a carrot, it becomes empowered for 3 seconds.
When the rabbit is empowered, the fox becomes frozen.
The game plan seems logical at first, but if you execute it, you will find that once the fox is frozen, it will never move again, even when the rabbit is no longer empowered. This is because we never described how a fox can become not frozen.

Thus, we would need to add a sentence, "When the rabbit is not empowered, the fox becomes unfrozen."

There is one fox, one rabbit, and 30 carrots.
The fox runs around and the rabbit runs around.
When the rabbit hits a carrot, it becomes empowered for 3 seconds.
When the rabbit is empowered, the fox becomes scared.
When the fox is scared, it is frozen.
Otherwise, the fox is not frozen.
This is slightly more complicated example with a new problem. Again, once the fox is frozen, it will never move again. This is because nothing describes how to make the fox not scared! So once the fox becomes scared, it will be scared forever and thus, frozen forever. How would you fix the game plan?

Now, if you wish to say 'When the rabbit touches a fox or a coyote, it dies., the compiler will automatically convert the sentence into two sentences, namely,

This is ok since the rabbit will die by touching either fox or coyote.

However, suppose you wish to follow the previous sentence with another sentence starting with 'Otherwise', it might not work correctly. For example:

In this situation, the second sentence starting with 'Otherwise' will not be executed correctly, since it will only check if the rabbit is touching the coyote (remember that the first sentence was broken into two sentences).

One way to fix this is to separate the antecedent clauses in the first sentence. For example:

Note that we convert the first antecedent clause 'rabbit touches a fox' to a Boolean attribute x1 for the rabbit. Then, in the third sentence above, we combine the two distinct and separate antecedent clauses, namely 'rabbit touches a coyote' and 'rabbit is x1'. Such a compound antecedent expression is allowed. Try it out and see that it works!
Complete Sample Games for Advanced Conditional Statements

// Game #1. Avoid the bamboos, dinos, and get to the star.


// Game #2. Bone Finder: find all 7 bones without exploding 12 walls



Programming Concepts (optional material):
In the above lesson, the fox has two Boolean attributes: frozen and scared. Since each Boolean attribute can only take on the values of either true or false, there are 4 combinations, namely: (1) scared and frozen, (2) scared and not frozen, (3) not scared and frozen, and (4) not scared and not frozen.

If the fox has 3 Boolean attributes instead of 2, how many combinations will there be? If you came up with 8, you are right! Consider a true-false quiz containing 5 questions. How many different answer sheets can there be? One can answer 'true' to all the questions, all the way to another that answers 'false' to all the answers. If you enumerate all the possible combinations, there would be 32 possible answer keys.

In fact, if there are n Boolean attributes, there will be 2 to the power of n combinations. If we have 10 attributes, the number of combinations quickly grows to 1024! This is referred to as combinatorial explosion. This means that the number of combinations will grow exponentially with the number of items (attributes in this case) that we need to consider.


Next Page

Tutorials Home