PAT - Conditional (Lesson)

Conditional

Introduction

In a previous module you worked on learning the basic constructs of Alice and how to put a basic program together by following directions and modifying the programs to add your own "twist".  The last module led you into some advanced concepts by playing follow-the-leader. Now it is time to look at some of the coding statements in more detail to have a better understanding of what they do.

A Condition is a choice - based on a truth decision

Boolean Expression

A condition is a choice, based on a truth decision. In programming a condition is a choice of whether the expression is true or false.

This is called a Boolean expression. When we ask the question if orange juice is orange, the condition, orange juice is orange is an expression of truth. Yes there are variations of orange, but for general purposes we consider the color of orange juice orange.  

Think of other conditions that we consider all the time that we create decisions with.  

Conditional Example

This conditional is called a nested condition because there is an if (condition) followed by another if (condition). The ifs are nested, one inside the other.  Each of the ifs create a condition choice based on the truth of the condition following the if. Look at the two choices above, do they say the same thing, one with a nested if and the other with a compound condition using and? The else statement is missing and not needed in either one. It is a consequence of not following the if that if it is raining outside and I go outside without the umbrella I will get wet.  

Test Yourself 1

Okay, let's try a basic if else combination.  Read the following code, determine your response and then check your answer. The variable nbr is assigned the number 5. Click on the link below the problem to reveal the answer.

nbr = 5                                                                                                        

if nbr < 5

      turn right .25

else

      turn left .25

Check your answers here! Links to an external site.

Test Yourself 2

Now let's try a more complicated if else combination, called a nested if because an if is inside another if. Read the following code, determine your response and then check your answer. The variable nbr is assigned the number 5.

if nbr < 3

      turn right .25

else

if nbr <= 6

    turn left .25

else

if nbr < 9

    turn right .5

the next line of code in the program whatever it may be

Answer each of the following questions based on the above code. Then, click on the link to open the document with the answers

What happens if nbr = 9?

What happens if nbr = 2?

What happens if nbr = 7?

What happens if nbr = 6?

Check your answers here! Links to an external site.

Let's Revisit Our Example

The following statements are the same as the above with the else added in all possible places.

Conditional Example

Is the result of the else obvious in both cases?

Yes, the else is the opposite, the negation of the compound if statement condition. Note that the negation of "and" is "or."

Review the following statement again:

Conditional Example

Does this if statement mean the same as the one at the woman's left hand (remember state left, not your left)?

Yes, because both it is raining and wanting to stay dry must be true to take the umbrella, and "and" condition requires both as truth. So if both have to be true, one or the other must be false (assume an umbrella is available) to make the statement false. So it is not raining or I want to get wet (don't want to stay dry). Notice that without the else, the same conclusions may be drawn.

Another way to say the negative of something is to use the word "not" in front. In coding the ! in front means not. For example

!raining would mean that it is not raining

!(x > 5) would mean x is not greater than 5 meaning x is less than or equal to 5, x <=5.

IMAGES MODIFIED BY GAVS FROM SUBSCRIPTION AND USED ACCORDING TO TERMS OF USE.