6 Conditionals and Booleans
6.1 Motivating Example: Shipping Costs
In Functions Practice: Cost of pens, we wrote a program (pencost) to compute the cost of ordering pens. Continuing the example, we now want to account for shipping costs. We’ll determine shipping charges based on the cost of the order.
Specifically, we will write a function addshipping to compute the total cost of an order including shipping. Assume an order valued at $10 or less ships for $4, while an order valued above $10 ships for $8. As usual, we will start by writing examples of the addshipping computation.
Do Now!
Use the is notation from where blocks to write several examples of addshipping. How are you choosing which inputs to use in your examples? Are you picking random inputs? Being strategic in some way? If so, what’s your strategy?
Here is a proposed collection of examples for addshipping.
addshipping(10) is 10 + 4 addshipping(3.95) is 3.95 + 4 addshipping(20) is 20 + 8 addshipping(10.01) is 10.01 + 8
Do Now!
What do you notice about our examples? What strategies do you observe across our choices?
Our proposed examples feature several strategic decisions:
Including 10, which is at the boundary of charges based on the text
Including 10.01, which is just over the boundary
Including both natural and real (decimal) numbers
Including examples that should result in each shipping charge mentioned in the problem (4 and 8)
So far, we have used a simple rule for creating a function body from examples: locate the parts that are changing, replace them with names, then make the names the parameters to the function.
Do Now!
What is changing across our addshipping examples? Do you notice anything different about these changes compared to the examples for our previous functions?
The values of 4 and 8 differ across the examples, but they each occur in multiple examples.
The values of 4 and 8 appear only in the computed answers—
not as an input. Which one we use seems to depend on the input value.
6.2 Conditionals: Computations with Decisions
To ask a question about our inputs, we use a new kind of expression called an if expression. Here’s the full definition of addshipping:
fun addshipping(orderamt :: Number) > Number: doc: "add shipping costs to order total" if orderamt <= 10: orderamt + 4 else: orderamt + 8 end where: addshipping(10) is 10 + 4 addshipping(3.95) is 3.95 + 4 addshipping(20) is 20 + 8 addshipping(10.01) is 10.01 + 8 end
In an if expression, we ask a question that can produce an answer that is true or false (here orderamt <= 10, which we’ll explain below in Booleans), provide one expression for when the answer to the question is true (orderamt + 4), and another for when the result is false (orderamt + 8). The else in the program marks the answer in the false case; we call this the else clause. We also need end to tell Pyret we’re done with the question and answers.
6.3 Booleans
Every expression in Pyret evaluates in a value. So far, we have seen three types of values: Number, String, and Image. What type of value does a question like orderamt <= 10 produce? We can use the interactions prompt to experiment and find out.
Do Now!
Enter each of the following expressions at the interactions prompt. What type of value did you get? Do the values fit the types we have seen so far?3.95 <= 10 20 <= 10
The values true and false belong to a new type in Pyret, called Boolean.Named for George Boole. While there are an infinitely many values of type Number, there are only two of type Boolean: true and false.
Exercise
Explain why numbers and strings are not good ways to express the answer to a true/false question.
Exercise
Why did we not enter orderamt <= 10 at the interactions prompt to explore booleans?
6.3.1 Other Boolean Operations
There are many other builtin operations that return Boolean values. Comparing values for equality is a common one: There is much more we can and should say about equality, which we will do later [ReExamining Equality].








In general, == checks whether two values are equal. Note this is different from the single = used to associate names with values in the directory.
The last example is the most interesting: it illustrates that strings are casesensitive, meaning individual letters must match in their case for strings to be considered equal.This will become relevant when we get to tables later.












Do Now!
Can you compare true and false? Try comparing them for equality (==), then for inequality (such as <).










Why use these operators instead of the more generic ==?
Do Now!
Trynumequal("a", 1) stringequal("a", 1)
Therefore, it’s wise to use the typespecific operators where you’re expecting the two arguments to be of the same type. Then, Pyret will signal an error if you go wrong, instead of blindly returning an answer (false) which lets your program continue to compute a nonsensical value.





6.3.2 Combining Booleans
Often, we want to base decisions on more than one Boolean value. For
instance, you are allowed to vote if you’re a citizen of a country
and you are above a certain age. You’re allowed to board a bus
if you have a ticket or the bus is having a freeride day. We
can even combine conditions: you’re allowed to drive if you are above
a certain age and have good eyesight and—










6.4 Asking Multiple Questions
We have to be able to ask another question to distinguish situations in which the shipping charge is 8 from those in which the shipping charge is 12.
The question for when the shipping charge is 8 will need to check whether the input is between two values.
The current body of addshipping asks one question: orderamt <= 10. We need to add another one for orderamt <= 30, using a charge of 12 if that question fails. Where do we put that additional question?
An expanded version of the ifexpression, using else if, allows you to ask multiple questions:
fun addshipping(orderamt :: Number) > Number: doc: "add shipping costs to order total" if orderamt <= 10: orderamt + 4 else if orderamt <= 30: orderamt + 8 else: orderamt + 12 end where: ... end
if <Boolean expression to check>: <expression if first expression is true> else if <another Boolean expression to check>: <expression if first expression is false and second expression is true> else: <expression if both expressions are false> end
Do Now!
The problem description for addshipping said that orders between 10 and 30 should incur an 8 charge. How does the above code capture “between”?
This is currently entirely implicit. It depends on us understanding the way a if evaluates. The first question is orderamt <= 10, so if we continue to the second question, it means orderamt > 10. In this context, the second question asks whether orderamt <= 30. That’s how we’re capturing “between”ness.
Do Now!
How might you modify the above code to build the “between 10 and 30” requirement explicitly into the question for the 8 case?
(orderamt > 10) and (orderamt < 30)
Do Now!
Why are there parentheses around the two comparisons? If you replace orderamt with a concrete value (such as 20) and leave off the parenthesis, what happens when you evaluate this expression in the interactions window?
Here is what addshipping look like with the and included:
fun addshipping(orderamt :: Number) > Number: doc: "add shipping costs to order total" if orderamt <= 10: orderamt + 4 else if (orderamt > 10) and (orderamt < 30): orderamt + 8 else: orderamt + 12 end where: addshipping(10) is 10 + 4 addshipping(3.95) is 3.95 + 4 addshipping(20) is 20 + 8 addshipping(10.01) is 10.01 + 8 addshipping(30) is 30 + 12 end
They signal to future readers (including ourselves!) the condition covering a case.
They ensure that if we make a mistake in writing an earlier question, we won’t silently get surprising output.
They guard against future modifications, where someone might modify an earlier question without realizing the impact it’s having on a later one.
6.5 Evaluating by Reducing Expressions
hours = 45
if hours <= 40: hours * 10 else if hours > 40: (40 * 10) + ((hours  40) * 15) end
if 45 <= 40: 45 * 10 else if 45 > 40: (40 * 10) + ((45  40) * 15) end
=> if false: 45 * 10 else if 45 > 40: (40 * 10) + ((45  40) * 15) end
=> if false: 45 * 10 else if true: (40 * 10) + ((45  40) * 15) end
=> (40 * 10) + ((45  40) * 15)
=> 400 + (5 * 15) => 475
This style of reduction is the best way to think about the evaluation of Pyret expressions. The whole expression takes steps that simplify it, proceeding by simple rules. You can use this style yourself if you want to try and work through the evaluation of a Pyret program by hand (or in your head).
6.6 Wrapping up: Composing Functions
pencost for computing the cost of the pens
addshipping for adding shipping costs to a total amount
What if we now wanted to compute the price of an order of pens including shipping? We would have to use both of these functions together, sending the output of pencost to the input of addshipping.
Do Now!
Write an expression that computes the total cost, with shipping, of an order of 10 pens that say "bravo".
There are two ways to structure this computation. We could pass the result of pencost directly to addshipping:
addshipping(pencost(10, "wow!"))
Alternatively, you might have named the result of pencost as an intermediate step:
pens = pencost(10, "wow!") addshipping(pens)
Both methods would produce the same answer.
Exercise
Manually evaluate each version. Where are the sequences of evaluation steps the same and where do they differ across these two programs?