image Tutorial on Advanced Conditional Statements

To avoid run-on sentences, each sentence can only allow for 2 conditional expressions in the antecedent. You may ask, what if you want to include 3 conditionals? For example, you can say, "When the rabbit empowered and the rabbit is red..." but not "When the rabbit is empowered and the rabbit is red and the fox is scared..." since that statement has three conditional expressions in the antecedent.

To address this, we first discuss how to reduce a compound conditional involving 2 expressions into a single expression. To do this, we use Boolean attributes as modifiers. For example:

Compound condition Single condition
When a fox sees a rabbit and the rabbit is empowered, then the fox is scared. When a fox sees an empowered rabbit, then the fox is scared.
When a fox is scared and the fox collides with a rock, it explodes. When a scared fox collides with a rock, it explodes.

Now, let us think about conditionals involving 3 expressions. Let us consider a situation where the fox will explode if the following 3 conditions are met:

To handle these 3 conditions, we add an attribute "scared" to describe the fox when it sees an empowered rabbit. Then, we combine the scared feature with the third condition to complete the game plan. The above table in fact gives the solution to this very case!

How about compound conditions joined by 'or', such as "When the fox collides with a rock or the fox collides with a brick, ..."? Such conditions should be broken into two separate sentences:

If you wish to describe the negation of the or condition, for example: Let us think about this for a minute. The otherwise will happen only if the fox does not collide with a rock nor brick. So we can write the following:
In addition to conditional statements, you can also specify how fast each object moves: There are 50 frames in a second. So in this example, the fox is effectively moving at 100 pixels per second. The maximum rate that you can set is 10 pixels per frame (effectively 500 pixels per second). Note, however, that we cannot specify the speed in this format - pixels per unit time (e.g., per second). Instead, we use pixels per frame as in the example.
We can also change the color of characters. A list of available colors is listed near the bottom of the right panel.
Try it yourself: Try playing around with multiple conditionals and see if you can break them down to several conditional sentences.

Suppose you want to write a game plan in which a bird only dies if it has:

How would you write this as a series of conditional statements?

Let's try another example: how would you describe a game plan such that an alien explodes only after it is shot 3 times? Hint: create 3 adjectives that describe the alien's state each time it is shot. Also, you may want to change the color of the alien to a specific color each time it is shot.

Quiz 1: Consider the following 2 sentences:

When an alien is shot, it explodes. When an alien that is not yellow is shot, it turns yellow.

Will any alien ever turn yellow? How would you fix it?

Quiz 2: What will the following do?

When an alien that is not yellow is shot, it turns green. When an alien that is not yellow is shot, it turns yellow.

The order of the sentences matter! Try reversing the order of these two sentences in your program and see what happens!! Can you explain why this happens?


Complete Sample Games for Advanced Conditional Statements

// Game #1. Destroy the green foxes.


// Game #2. Complex Logic Network: Get to the top by touching the left-moving bricks.



Programming Concepts (optional material):
Recall that an object has some member functions, or methods, that perform specific actions for the object. Example methods include "move" and "wander." When performing these actions, one can also pass parameters, which provide additional details to how the action should be performed. For example, in the following sentence, "fox wanders around at 2 pixels per frame", the 2 pixels per frame is a parameter that tells the method "wander" how fast to move the object. If no parameter is given, then the default speed is used.

We also have parameters in real life. When we go online shopping, we can choose filters such as the category, color, size, etc. of the desired item. Those chosen filters are parameters that tell the website what items to display.


Next Page

Tutorials Home