Course F - Conditionals 1 - Conditionals with the Weather Predictor

This lesson introduces the most basic decision-making tool in programming.

  • Computer science
  • conditionals
Print lesson
  • Ages 10+
  • 65+ mins
  • JavaScript Blocks

හඳුන්වාදීම

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

Open

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).

4 steps

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 code

let Random_Weather = 0
input.onButtonPressed(Button.A, () => {
    Random_Weather = Math.random(2)
    if (Random_Weather == 0) {
        basic.showString("Raining")
        basic.showIcon(IconNames.Umbrella)
    } else {
        basic.showString("Play Outside!")
        basic.showIcon(IconNames.Rollerskate)
    }
    SmoothOut_Animation()
})
function SmoothOut_Animation()  {
    basic.pause(1000)
    basic.clearScreen()
}
          

The finished code is available here.

Challenge 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 code

let Random_Weather = 0
input.onButtonPressed(Button.A, () => {
    Random_Weather = Math.random(2)
    if (Random_Weather == 0) {
        basic.showString("Sunny / Warm")
        basic.showIcon(IconNames.TShirt)
    } else {
        basic.showString("Play Outside!")
        basic.showIcon(IconNames.Rollerskate)
    }
    SmoothOut_Animation()
})
function SmoothOut_Animation()  {
    basic.pause(1000)
    basic.clearScreen()
}
          

The finished code is available here.

Challenge 3

Change the if statement to predict if it will be Snowy / Cold? Add words, icons, show LEDs to make pictures...

View code

let Random_Weather = 0
input.onButtonPressed(Button.A, () => {
    Random_Weather = Math.random(2)
    if (Random_Weather == 0) {
        basic.showString("Snowy / Cold")
        basic.showIcon(IconNames.Chessboard)
    } else {
        basic.showString("Play Outside!")
        basic.showIcon(IconNames.Rollerskate)
    }
    SmoothOut_Animation()
})
function SmoothOut_Animation()  {
    basic.pause(1000)
    basic.clearScreen()
}
          

The finished code is available here.

Independent 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 answer

Answers will vary but... example - If teacher is mad then be quiet else talk

Question 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

The code would show "EAT ALONE"

Question 3

Finish this if statement


If I_AM_HUNGRY THEN
   your_answer
else
   your_answer
        
Show answer

Answers will vary, for example:


If I_AM_HUNGRY THEN
   show string "I eat food"
else
   show string "I don't eat"
        
Selecting this opens external content from our support system, which adheres to their privacy policy.