Lesson 6: Variables and If Statements
45 minutes
Overview
In this lesson, students are introduced to variables, the counter pattern, and if-statements. Students will use these concepts in the context of programming the micro:bit and creating more complex input/output behaviors, such as counting the number of button presses before having the micro:bit light up. The concepts in this lesson are used to make more complex and fulfilling apps in future lessons, and students may need to refer back to the videos and examples in this lesson as they continue to master these concepts.
Question of the Day: How can I create more complex programs that keep track of information and make decisions?
Standards
AP - Algorithms & Programming
- 2-AP-11 - Create clearly named variables that represent different data types and perform operations on their values.
- 2-AP-13 - Decompose problems and subproblems into parts to facilitate the design, implementation, and review of programs.
- 2-AP-19 - Document programs in order to make them easier to follow, test, and debug.
CS - Computing Systems
- 2-CS-02 - Design projects that combine hardware and software components to collect and exchange data.
- 2-CS-03 - Systematically identify and fix problems with computing devices and their components.
Agenda
Objectives
Students will be able to:
- Use if-statements to make decisions when creating apps
- Use the counter pattern to update variables when creating apps
- Use variables to store information when creating apps
Preparation
- Prepare a board and USB cable for each pair of students
- Check the "Teacher's Lounge" forum for verified teachers to find additional strategies or resources shared by fellow teachers
Links
Heads Up! Please make a copy of any documents you plan to share with students.
For the teachers
- Variables and If Statements - Slides
For the students
- If Statements - Resource
- If Statements - Video
- The Counter Pattern - Video
- The Counter Pattern - Resource
- Variables - Resource
- Variables - Video
Teaching Guide
Warm Up (5 minutes)
Distribute: Pass out a board and USB cable to each pair of students. Let students know that they should not yet plug the boards in.
Transition to Code Studio: Have students get started in Code Studio right away
Starting Right Away: This lesson is packed with new content and includes several videos that help introduce students to these new ideas. In the interest of giving students the most amount of time to practice these new concepts, we recommend jumping straight into Code Studio and starting the lesson there.
Activity (35 minutes)
Transition: Send students to Code Studio
Guide to Programming Levels: Additional guidance for programming levels is provided in the Guide to Programming Levels. This document includes strategies and best-practices for facilitating programming levels with students.
Troubleshooting Devices: If student devices aren't able to connect, they can click the Help and Tips tab and then access the Maker Setup page (click the image to see more). This will let them re-reconnect their board to App Lab.
If students continue to have issues, refer to the Troubleshooting section of our Maker Support Article
Discussion Goal: Students should notice that the names in the var blocks are repeated in the code. They may guess that the numbers assigned to these names at the top will be substituted in farther down the code. Even if students aren't sure what the screen will look like, they may be more sure that the rectangles will all look the same because they have the same width and height
Video: Show students the Variables video in the slides.
Options for Watching Videos: Videos can be a good opportunity to re-group the class and ensure everyone gets the same information before proceeding in the lesson. Videos are included in the teacher slide deck to be watched as a class, but are also included as individual levels if students need to watch individually at a later date. We recommend watching videos as a class when possible and allowing the students to discuss and synthesize the topics with partners or as a full class.
Reviewing the Video: After watching the video, consider printing or displaying the corresponding Lesson Resource included as a PDF in the student resources of the lesson plan. This can be a helpful visual cue for students as they continue working through levels that require them to apply what they learned from the video, or can be used in a class discussion to summarize and clarify the content of the video. You can also remind students that these resources are available in the Help and Tips tab on individual levels.
To encourage active engagement and reflection, use one or more of the strategies discussed in the Guide to Curriculum Videos.
Video: Show students the The Counter Pattern video in the slides.
Reviewing the Video: After watching the video, consider printing or displaying the corresponding Lesson Resource included as a PDF in the student resources of the lesson plan. This can be a helpful visual cue for students as they continue working through levels that require them to apply what they learned from the video, or can be used in a class discussion to summarize and clarify the content of the video. You can also remind students that these resources are available in the Help and Tips tab on individual levels.
Video: Show students the If Statements video in the slides.
Reviewing the Video: After watching the video, consider printing or displaying the corresponding Lesson Resource included as a PDF in the student resources of the lesson plan. This can be a helpful visual cue for students as they continue working through levels that require them to apply what they learned from the video, or can be used in a class discussion to summarize and clarify the content of the video. You can also remind students that these resources are available in the Help and Tips tab on individual levels.
Discussion Goal: Students should notice that both if-statements use the value 315, and some students might use their mouse cursor to hover over the app and notice that 315 is close to the horizon created by the hills and trees. Students should also notice that the background changes between two different colors - orange and gray. Connecting these two observations together, students might predict that this will change when the "sun sets" behind the horizon, changing colors between day (orange) and night (gray).
Formative Assessment: This level can be used as a formative assessment. A rubric is provided in the level, and written feedback can be given to students. Click here to learn more about giving feedback to students.
Wrap Up (5 minutes)
Journal
Prompt:
- What was a moment from today where you felt successful?
- What is a question or point of confusion you still have from today?
Have students write their responses in their journal. They can also share one of their responses with you as they leave for the day, or you can check their journals later.
Formative Assessment: There are a lot of places in this lesson where students may get lost or confused and not feel comfortable speaking up, especially if other students in the class have prior experience with variables and if-statements. Use this prompt as a way to determine if you need to check in with students at a later lesson, or revisit a concept from today. This is especially true if students have varied levels of experience with programming before this unit.
This work is available under a Creative Commons License (CC BY-NC-SA 4.0).
If you are interested in licensing Code.org materials for commercial purposes contact us.