Intermediate Conditionals
Contents
Introduction
We often want to be able to " conditionally " do things in our programs – we want to be able to say “if this thing is true, then do X but if this other thing is true, then do Y.” It’s like when we wake up in the morning – “ if it’s raining outside, then I take an umbrella, else I wear sunglasses.” We can do things conditionally in our programs using if statements and if/else statements .
What is it?
A conditional is a statement that only runs under certain conditions.
Notice that conditions are either “True” or “False.” There is no assessment of a condition that evaluates “Banana” for example.
Think about it!
What other things do you do during the day under certain conditions?
Activities
Bee Conditionals – Puzzle 1
A conditional statement is code that functions differently depending on the conditions it encounters. In the first Bee Conditionals puzzle on Code.org, you’ll use conditionals with the bee to help deal with some mysterious purple flowers. We don’t know if those flowers have nectar or not, so you’ll need to use conditionals to make sure that you collect nectar if it’s there, but you don’t try to collect nectar from a flower that doesn’t have any.
Bee Conditionals – Puzzle 2
In the first Bee Conditionals puzzle, we only looked at simple conditionals called if statements, such as: “If there is one nectar, collect it.” Basically, we are saying if a statement is true, do something. In this second puzzle, we are going to look at what to do if that statement is not true - we call these if/else statements.
Conditionals with Cards
Conditionals with Cards. This activity demonstrates how conditionals can be used to tailor a program to specific information.
Start this card game by creating a few programs as a team that depend on things like a playing card’s suit, color or value to award or subtract points. You can write the program as an algorithm or in CoffeeScript.
Here’s a sample algorithm:
If (CARD is RED)
- Award YOUR team 1 point
Else
- Award OTHER team 1 point
This program has you choose a card. If the card is red, your team gets a point. Else, the other team gets a point.
Here’s sample of the same program in CoffeeScript:
if card.color == 'red'
- award_points(myTeam, 1)
else
- award_points(otherTeam, 1)
Next, divide your team into two smaller groups and give each group a pile of cards. Put one of your “Programs” in a place where every team member can see it. Have each group take turns drawing cards and following the program to see how many points they score in each round. Play several times with several different programs For an added challenge, you can try to nest conditionals inside one another:
If (CARD is RED)
- Award YOUR team 1 point
Else
-
If (CARD is higher than 9)
- Award OTHER team 1 point
-
Else
- Award YOUR team the same number of points on the card
This program has you choose a card. If the card is red, your team gets a point. Else, the card must be black. If your black card is higher than 9, then the other team gets a point, else your card must be black and lower than or equal to 9, and you get as many points as are on your card.
Here’s the same program in CoffeeScript:
if card.color == 'red'
- award_points(myTeam, 1)
else
-
if card.value > 9
- award_points(otherTeam, 1)
-
else
- award_points(myTeam, card.value)
CoffeeScript is often called a “little programming language.” It is a more legible and minimal way to write JavaScript, another popular programming language, exposing the good parts in a simple manner.
Three Strikes Dice Game
Imagine a game where you roll a six-sided die to score points. If the number you rolled is a 2 or a 5, you get a “strike”. Otherwise, you get however many points you rolled. You keep rolling to score points, but if you get 3 strikes, you’re out, and your turn is over. The player with the highest score wins.
Computers are actually very good at rolling dice in the virtual world. You just tell it to pick a random number between 1 and 6, and then write the number on screen.
Try this now in Pencil Code using the following blocks:
Note: you can find the “button” block in the Snippets section.
Now press the blue “play” button to execute your code. Press the button that appears to roll the die and see the result. Keep pressing over and over again to see the random numbers in action!
When we use the write block, we need to put double-quotes around the text. Look again at the example above. See how we put the variable inside the text? You need to put your variable between #{ and } if you want to show that variable’s value inside the text.
How might we tell the computer to add strikes if it’s a 2 or if it’s a 5? Here’s what our program might look like using “pseudo code”:
set strikes to 0 set score to 0
when the “Roll Dice” button is clicked:
- set x to a random number between 1 and 6
- write “You rolled a ___”
-
if x is equal to 2:
- add 1 to strikes
- write “Strike!”
-
else, if x is equal to 5:
- add 1 to strikes
- write “Strike!”
-
else:
- add x to points
-
if strikes is greater than or equal to 3:
- write “3 strikes, you’re out!”
-
else:
- write “Score: ____ / Strikes: ____”
When run, your program output should look like something like this:
- You rolled a 6
- Score: 6 / Strikes: 0
- You rolled a 3
- Score: 9 / Strikes: 0
- You rolled a 5
- Strike!
- Score: 9 / Strikes: 1
- You rolled a 2
- Strike!
- Score: 9 / Strikes: 2
- You rolled a 6
- Score: 15 / Strikes: 2
- You rolled a 4
- Score: 21 / Strikes: 2
- You rolled a 5
- Strike!
- 3 Strikes, you're out!
Now, use Pencil Code to create the Three Strikes Dice Game.