# Lesson 8: Conditionals and Piecewise Functions

## Overview

Currently, even when passing parameters to functions, our outputs follow a very rigid pattern. Now, suppose we want parameters with some values to create outputs using one pattern, but other values to use a different pattern. This is where conditionals are needed. In this stage students will learn how conditional statements can create more flexible programs.

## Agenda

### Getting Started

### Activity

## Anchor Standard

### Common Core Math Standards

**F.IF.7.b**- Graph square root, cube root, and piecewise-defined functions, including step functions and absolute value functions.

## Objectives

### Students will be able to:

- Understand that piecewise functions evaluate the domain before calculating results.
- Evaluate results of piecewise functions.

## Links

**Heads Up!**Please make a copy of any documents you plan to share with students.

### For the Teacher

- Conditionals and Piecewise Functions - Slide Deck

## Support

### Report a Bug

# Teaching Guide

## Getting Started

### Conditionals

- We can start this lesson off right away
- Let the class know that if they can be completely quiet for thirty seconds, you will do something like:
- Sing an opera song
- Give five more minutes of recess
- or Do a handstand

- Start counting right away.
- If the students succeed, point out right away that they succeeded, so they
*do*get the reward. - Otherwise, point out that they were not completely quiet for a full thirty seconds, so they
*do not*get the reward. - Ask the class "What was the
*condition*of the reward?" - The condition was if you were quiet for 30 seconds
- If you were, the condition would be true, then you would get the reward.
- If you weren't, the condition would be false, then the reward would not apply.

- Can we come up with another conditional?
- If I say "question," then you raise your hand.
- If I sneeze, then you say "Gesundheit."
- What examples can you come up with?

Up to now, all of the functions you’ve seen have done the same thing to their inputs:

- green-triangle always made green triangles, no matter what the size was.
- safe-left? always compared the input coordinate to 0, no matter what that input was.
- update-danger always added or subtracted the same amount

Conditionals let our programs run differently based on the outcome of a condition. Each clause in a conditional evaluates to a boolean value - if that boolean is TRUE, then we run the associated expression, otherwise we check the next clause. We've actually done this before when we played the boolean game! If the boolean question was true for you, you remained standing, and if it was false you sat down.

Let's look at a conditional piece by piece:

(x > 10) -> "That's pretty big" (x < 10) -> "That's pretty small" else -> "That's exactly ten"

If we define x = 11, this conditional will first check if x > 10, which returns TRUE, so we get the String "That's pretty big" - and because we found a true condition we don't need to keep looking.

If we define x = 10, then we first check if x > 10 (FALSE), then we check x < 10 (FALSE), so then we hit the *else* statement, which only returns something if none of the other conditions were true. The *else* statement should be considered the catch-all response - with that in mind, what's wrong with replying "That's exactly ten"? What if x = "yellow"? If you can state a precise question for a clause, write the precise question instead of else. It would have been better to write the two conditions as (x > 10) and (x <= 10). Explicit questions make it easier to read and maintain programs.

Functions that use conditions are called piecewise functions, because each condition defines a separate piece of the function. Why are piecewise functions useful? Think about the player in your game: you’d like the player to move one way if you hit the "up" key, and another way if you hit the "down" key. Moving up and moving down need two different expressions! Without conditionals, you could only write a function that always moves the player up, or always moves it down, but not both.

Now let's play a game.

## Activity

### Conditionals and Piecewise Functions

Living Function Machines - Conditionals:

Explain to the class that they will be playing the role of Function Machines, following a few simple rules:

- Whenever your function is called, the only information you are allowed to take in is what's described in your Domain.
- Your function must return only what is described in your Range.
- You must follow the steps provided in your definition - no magic!

This time, however, everyone will be running the same function. And that function is called 'simon_says' and it has the following Contract:
simon_says: String -> Movement

Given a String that describes an action, produce the appropriate movement. If an unknown action is called, lower both hands.

Examples

simon_says("left hand up") = RaiseLeftHand simon_says("right hand up") = RaiseRightHand simon_says("left hand down") = LowerLeftHand simon_says("right hand down") = LowerRightHand

Definition

simon_says(action) = cond { "left hand up" : RaiseLeftHand, "right hand up" : RaiseRightHand, "left hand down" : LowerLeftHand, "right hand down" : LowerRightHand, else : LowerBothHands }

Review the contract parts: name, domain, range, parameters (input types), return types (output values)

Say to the class: “Here is what the initial code looks like. We will add several clauses but the clauses that are there will always be there and the final else action (often called the default result) will always be LowerBothHands

`simon_says("right hand up")`

`simon_says("left hand up")`

- both hands should be up`simon_says("right hand up")`

- both hands should still be up`simon_says("left hand down")`

- left should be down, right should be up`simon_says("right hand up")`

- left should be down, right should be up`simon_says("hokey pokey")`

- both hands should be down`simon_says("left hand up")`

- left hand should be up`simon_says("right up")`

- trick, there are no matches so the else statement is called

If anyone makes a mistake, they must "reboot" by sitting down and waiting for the next round to start.

Say to the class: “Now we're going to rewrite our function a little bit - instead of taking a String as its Domain, simon_says will take a Number. Here's what our new function looks like:

simon_says(action) = cond { (action < 10) : RaiseLeftHand, (action < 20) : RaiseRightHand, (action > 20) and (action < 50) : LowerLeftHand, (action > 50) and (action < 100) : LowerRightHand, else : LowerBothHands }

Continue playing using numbers in the `simon_says`

function, such as `simon_says(15)`

, which should result in `RaiseRightHand`

. As students get comfortable with the new rules, you can throw in some trick questions, such as `simon_says(20)`

or `simon_says(50)`

, both of which should call the else statement. You can extend this activity in many ways, for example:

- Call the function with a simple expression, such as
`simon_says(30 / 2)`

- Add more conditions of your own
- Create multiple functions and divide the class into groups
- Allow students to take over as the 'programmer'

### Connection to Mathematics and Life

There are piecewise functions in mathematics as well. The absolute value function y = |x| can be re-written as

y = { -x : x<0 , x : x>0, 0 }

Note that in mathematical terms, the clause for the domain is usually listed second instead of first.

A data plan on a phone bill might be structured as:

- $40 for less than 5 GB
- $ 8 per GB for 5-10 GB
- $12 per GB for using more than 10GB

This could be graphed with the following piecewise function y = { 40: x<5, 8x: 5 =< x =< 10, 12x: x>10 }

Another very common piecewise functions is for taxi cabs.

- $3 for 0 to 2 miles
- $1 for each partial mile after that

This could be graphed with the following piecewise function y = { 3: x<2, [[x]]+2: x>=2 } where [[x]] is the greatest integer function or what is often called a floor function in computer languages. The greatest integer function returns the greatest INTEGER less that the current value. For instance [[2.9]] is 2 and [[3.1]] is 3.

- Levels
- 1

### Student Instructions

## Standards Alignment

#### View full course alignment

#### Common Core Math Standards

**EE** - Expressions And Equations

**6.EE.9**- Use variables to represent two quantities in a real-world problem that change in relationship to one another; write an equation to express one quantity, thought of as the dependent variable, in terms of the other quantity, thought of as the independent va**7.EE.4**- Use variables to represent quantities in a real-world or mathematical problem, and construct simple equations and inequalities to solve problems by reasoning about the quantities.

**F** - Functions

**8.F.1**- Understand that a function is a rule that assigns to each input exactly one output. The graph of a function is the set of ordered pairs consisting of an input and the corresponding output.1**8.F.2**- Compare properties of two functions each represented in a different way (algebraically, graphically, numerically in tables, or by verbal descriptions). For example, given a linear function represented by a table of values and a linear function represented

**IF** - Interpreting Functions

**F.IF.7.b**- Graph square root, cube root, and piecewise-defined functions, including step functions and absolute value functions.

**MP** - Math Practices

**MP.1**- Make sense of problems and persevere in solving them**MP.2**- Reason abstractly and quantitatively**MP.3**- Construct viable arguments and critique the reasoning of others**MP.4**- Model with mathematics**MP.5**- Use appropriate tools strategically**MP.6**- Attend to precision**MP.7**- Look for and make use of structure**MP.8**- Look for and express regularity in repeated reasoning

**NS** - The Number System

**6.NS.8**- Solve real-world and mathematical problems by graphing points in all four quadrants of the coordinate plane. Include use of coordinates and absolute value to find distances between points with the same first coordinate or the same second coordinate.

**OA** - Operations And Algebraic Thinking

**5.OA.1**- Use parentheses, brackets, or braces in numerical expressions, and evaluate expressions with these symbols.**5.OA.2**- Write simple expressions that record calculations with numbers, and interpret numerical expressions without evaluating them. For example, express the calculation “add 8 and 7, then multiply by 2” as 2 × (8 + 7). Recognize that 3 × (18932 + 921) is three t

**Q** - Quantities

**N.Q.1**- Use units as a way to understand problems and to guide the solution of multi-step problems; choose and interpret units consistently in formulas; choose and interpret the scale and the origin in graphs and data displays.**N.Q.2**- Define appropriate quantities for the purpose of descriptive modeling.