Course F - Conditionals 1 - Conditionals with the Weather Predictor
This lesson introduces the most basic decision-making tool in programming.
Introduction
This lesson introduces the most basic decision-making tool in programming. If statements allow us to make decisions based on our data.
In many cases we might compare two numbers or strings(words). Then depending on what we find, we can make our program react in an intelligent way.
In our real life, we make if then decisions every day when we look outside. The weather determines what we wear and do. If it is windy then we might put on a light jacket or fly a kite. If it looks like rain we might grab an umbrella. This lesson uses the real-life example of weather to experiment with the 'if...then...else' logic block.
This first lesson allows for two situations but the second lesson allows for more. 'If...then...else' logic blocks can be extended to deal with an infinite number of situations. Feel free to use the gear on the 'If...then...else' logic block to extend the possibilities.
Our micro:bit course lessons are tailored to apply knowledge obtained from the Code.org CS Fundamentals. Before students begin these Course Lessons, we encourage students to first complete all CS Fundamentals as a prerequisite. Students should be familiar with conditionals from Code.org CS Fundamentals.
Teacher Guide
Activity
What you'll need
- 1 - micro:bit
- 1 - USB cable
- 1 - AAA battery pack (optional)
- 2 - AAA batteries (optional)
Documentation
if (true) {
} else if (false) {
} else {
}
if
Conditionally run code depending on whether a Boolean condition is true or false.
let item = 0
item = 0
setItem
Use an equals sign to make a variable store the number or string you say.
let item = Math.random(5)
random
Return a pseudorandom number between 0 and limit
basic.showIcon(IconNames.Heart)
showIcon
Shows the selected icon on the LED screen
basic.showString("Hello!")
showString
Show a number on the LED screen. It will slide left if it is bigger than the screen.
input.onButtonPressed(Button.A, () => {})
onButtonPressed
Start an event handler (part of the program that will run when something happens, like when a button is pressed).
Guided Practice
We will make instructions or code online for the micro:bit. Next, go to https://makecode.microbit.org
let Random_Weather = 0
input.onButtonPressed(Button.A, () => {
Random_Weather = Math.random(2)
if (Random_Weather == 0) {
basic.showString("Raining")
} else {
basic.showString("Play Outside!")
}
SmoothOut_Animation()
})
function SmoothOut_Animation() {
basic.pause(1000)
basic.clearScreen()
}
The micro:bit can predict the weather!
When button A is pressed a random number is picked. The number is either a 0 or a 1.
If the number is 0 it means it is raining.
If the number is 1 then it is not raining and we should go outside to play.
NOTE: The unused blocks at the bottom of the code are meant to be used in the challenges.
The finished code is available here.
Challenges
Challenge 1
Can you add in more animation to make the output about raining more meaningful?
Hint: Use the umbrella icon. You should also try to use the skate icon.
View codeChallenge 2
Change the if statement to predict if it will be Sunny / Warm? Add words, icons, show LEDs to make pictures...
Hint: You will need to change the inner content of the 'if...then...else' block. There is no need to change any numbers or anything involved with Random_Weather
View codeChallenge 3
Change the if statement to predict if it will be Snowy / Cold? Add words, icons, show LEDs to make pictures...
View codeIndependent Practice
Recreate the weather predictor. Create code that predicts something or makes a decision. Be prepared to tell a story about your code. Rename Random_Weather and other words to connect with your theme.
Example - You could predict what your future job or you could make a decision to go shopping or read a book. The possibilities are endless.
Quiz
Use these questions to reflect on the activity and challenges.
Question 1
What is another real life situation, we have not talked about yet, that we could use an 'if...then...else' statement to decide?
Show answerQuestion 2
What would the code below show?
Set DONUTS to 12
if DONUTS > 20 then
show string "SHARE"
else
show string "EAT ALONE"
Show answer
Question 3
Finish this if statement
If I_AM_HUNGRY THEN
your_answer
else
your_answer
Show answer
Learn More
This lesson has been developed in collaboration with K12 Maker Integration