![]() Whenever your function is called, the only information you are allowed to take in is what's described in your Domain. 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.Īctivities: 3) Conditionals and Piecewise FunctionsĮxplain to the class that they will be playing the role of Function Machines, following a few simple rules: 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. Explicit questions make it easier to read and maintain programs.įunctions that use conditions are called piecewise functions, because each condition defines a separate piece of the function. If we define x = 10, then we first check if x > 10 (FALSE), then we check x 10) and (x <= 10). 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. Let's look at a conditional piece by piece: (x > 10) -> "That's pretty big" 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. 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. update-danger always added or subtracted the same amountĬonditionals let our programs run differently based on the outcome of a condition. ![]() safe-left? always compared the input coordinate to 0, no matter what that input was.green-triangle always made green triangles, no matter what the size was.Up to now, all of the functions you’ve seen have done the same thing to their inputs: If I sneeze, then you say "Gesundheit.".If I say "question," then you raise your hand.Can we come up with another conditional?.If you weren't, the condition would be false, then the reward would not apply.If you were, the condition would be true, then you would get the reward.The condition was if you were quiet for 30 seconds.Ask the class "What was the condition of the reward?".Otherwise, point out that they were not completely quiet for a full thirty seconds, so they do not get the reward.If the students succeed, point out right away that they succeeded, so they do get the reward.Let the class know that if they can be completely quiet for thirty seconds, you will do something like:.We can start this lesson off right away.Piecewise Function - a function which evaluates the domain before choosing how to create the range.Conditional - a code expression made of questions and answers. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |