Lesson How Do You Make Loops and Switches?

Quick Look

Grade Level: 5 (4-7)

Time Required: 1 hours 45 minutes

(Two 50-minutes sessions)

Lesson Dependency:

Subject Areas: Computer Science, Science and Technology

NGSS Performance Expectations:

NGSS Three Dimensional Triangle
3-5-ETS1-2
MS-ETS1-3

Photo shows a girl lying on the floor, working on a laptop computer. A diagram with the words "I am in a loop!" surrounded by a blue oval-shaped ring with four red arrows pointing in a counter-clockwise direction.
Students learn how to program using loops and switches
copyright
Copyright © (left) 2004 Microsoft Corporation, One Microsoft Way, Redmond, WA 98052-6399 USA. All rights reserved; (right) 2013 GK-12 Program, Computational Neurobiology Center, College of Engineering, University of Missouri http://office.microsoft.com/en-us/images/results.aspx?qu=laptop&ex=1#ai:MP900442369|

Summary

Students learn how to program using loops and switches. They see how loops enable us to easily and efficiently tell a computer to keep repeating an operation. They also see that switches permit programs to follow different instructions based on whether or not preconditions are fulfilled. Using the LEGO® MINDSTORMS® robots, sensors and software, student pairs perform three mini programming activities using loops and switches individually, and then combined. With practice, they incorporate these tools into their programming skill sets in preparation for the associated activity. A PowerPoint® presentation, pre/post quizzes and worksheet are provided.
This engineering curriculum aligns to Next Generation Science Standards (NGSS).

Engineering Connection

Loops and switches are important programming components in any language. Loops are important because nearly every computer program must iterate operations many times in order to complete tasks. Indeed, performing tedious operations repeatedly was one of the foremost motivations for the invention of the computer. Switches are almost equally important; they enable programs to accommodate a variety of situations. Further, switches in the LEGO MINDSTORMS NXT programming environment are used similarly to "if/else...then" statements, which are used in other more common programming languages.

Learning Objectives

After this lesson, students should be able to:

  • Explain what loops and switches are and identify programming situations in which loops and switches should be utilized.
  • Write programs that incorporate loops and switches.

Educational Standards

Each TeachEngineering lesson or activity is correlated to one or more K-12 science, technology, engineering or math (STEM) educational standards.

All 100,000+ K-12 STEM standards covered in TeachEngineering are collected, maintained and packaged by the Achievement Standards Network (ASN), a project of D2L (www.achievementstandards.org).

In the ASN, standards are hierarchically structured: first by source; e.g., by state; within source by type; e.g., science or mathematics; within type by subtype, then by grade, etc.

NGSS Performance Expectation

3-5-ETS1-2. Generate and compare multiple possible solutions to a problem based on how well each is likely to meet the criteria and constraints of the problem. (Grades 3 - 5)

Do you agree with this alignment?

Click to view other curriculum aligned to this Performance Expectation
This lesson focuses on the following Three Dimensional Learning aspects of NGSS:
Science & Engineering Practices Disciplinary Core Ideas Crosscutting Concepts
Generate and compare multiple solutions to a problem based on how well they meet the criteria and constraints of the design problem.

Alignment agreement:

Research on a problem should be carried out before beginning to design a solution. Testing a solution involves investigating how well it performs under a range of likely conditions.

Alignment agreement:

At whatever stage, communicating with peers about proposed solutions is an important part of the design process, and shared ideas can lead to improved designs.

Alignment agreement:

Engineers improve existing technologies or develop new ones to increase their benefits, to decrease known risks, and to meet societal demands.

Alignment agreement:

NGSS Performance Expectation

MS-ETS1-3. Analyze data from tests to determine similarities and differences among several design solutions to identify the best characteristics of each that can be combined into a new solution to better meet the criteria for success. (Grades 6 - 8)

Do you agree with this alignment?

Click to view other curriculum aligned to this Performance Expectation
This lesson focuses on the following Three Dimensional Learning aspects of NGSS:
Science & Engineering Practices Disciplinary Core Ideas Crosscutting Concepts
Analyze and interpret data to determine similarities and differences in findings.

Alignment agreement:

There are systematic processes for evaluating solutions with respect to how well they meet the criteria and constraints of a problem.

Alignment agreement:

Sometimes parts of different solutions can be combined to create a solution that is better than any of its predecessors.

Alignment agreement:

Although one design may not perform the best across all tests, identifying the characteristics of the design that performed the best in each test can provide useful information for the redesign process—that is, some of the characteristics may be incorporated into the new design.

Alignment agreement:

  • Test and evaluate the solutions for the design problem. (Grades 3 - 5) More Details

    View aligned curriculum

    Do you agree with this alignment?

  • Apply the technology and engineering design process. (Grades 3 - 5) More Details

    View aligned curriculum

    Do you agree with this alignment?

  • Judge technologies to determine the best one to use to complete a given task or meet a need. (Grades 3 - 5) More Details

    View aligned curriculum

    Do you agree with this alignment?

  • Develop innovative products and systems that solve problems and extend capabilities based on individual or collective needs and wants. (Grades 6 - 8) More Details

    View aligned curriculum

    Do you agree with this alignment?

  • Refine design solutions to address criteria and constraints. (Grades 6 - 8) More Details

    View aligned curriculum

    Do you agree with this alignment?

  • Describe how new technologies have helped scientists make better observations and measurements for investigations (e.g., telescopes, electronic balances, electronic microscopes, x-ray technology, computers, ultrasounds, computer probes such as thermometers) (Grade 5) More Details

    View aligned curriculum

    Do you agree with this alignment?

Suggest an alignment not listed above

Worksheets and Attachments

Visit [www.teachengineering.org/lessons/view/umo_computerprogram_lesson03] to print or download.

Pre-Req Knowledge

Ability to write and download simple LEGO MINDSTORMS NXT programs.

Introduction/Motivation

(Be ready to show students the 26-slide Loops and Switches Presentation, a Microsoft® PowerPoint® file, to teach the lesson, as well as a computer/projector with Internet access to show a short online video. In advance, make copies of the Loops and Switches Pre/Post Quiz, provided as attachments and slides, and the Loops and Switches Worksheet. Student groups also need LEGO robot components, as described in the Background section.)

As computer use becomes more and more widespread, programming is emerging as an essential skill for many engineers and scientists, as well as for future engineers and scientists—like you! Two fundamental components of every programming language are loops and switches. What are they?

Loops enable programmers to quickly tell a computer to repeat a sequence of instructions until some condition has been fulfilled. In the previous two lessons, we have emphasized that programming is algorithmic—and all good algorithms depend on performing iterations of a process until a problem is solved.

Switches provide the backbone of conditional programming and are the LEGO MINDSTORMS NXT equivalent of "if/then" statements, which are used is many different programming languages. Using switches enables programs to be versatile in responding to different situations. For instance, this flexibility enables a chess-playing program to make appropriate moves, depending on its opponent's previous moves.

Continue by showing the presentation and delivering the content in the Lesson Background section. Following the lesson, refer to the associated activity Using Waits, Loops and Switches to have students complete a somewhat complicated challenge that requires them to program their LEGO robots using a combination of loops, switches and wait blocks

Lesson Background and Concepts for Teachers

The third lesson in this unit provides an introduction to programming with loops and switches as well as a detailed walkthrough for using these programming tools in the LEGO MINDSTORMS NXT software. Present the lesson using the content provided in the slide presentation, as described below.

During the lesson, each group needs the following materials:

Day 1: What Are Loops? Presentation Outline (Slides 1-10)

  1. Administer the pre-quiz by handing out paper copies; the quiz is also on slide 2. The answers are provided for the teacher on slide 3 for discussion after students have completed the quiz.
  2. Talk about the lesson objectives, as provided on slide 4: To learn about two new programming tools: loops and switches, and how to use them with LEGO robots.
  3. Tell students: A loop is an object that repeats an embedded set of commands. This is useful if one needs to efficiently tell a robot to repeat a process many times.
  4. Walk through slides 5-10 to provide students with a detailed explanation of situations in which loops are helpful and how to implement loops with the LEGO MINDSTORMS NXT software. A definition and explanation of loops is provided (slide 5), as well as a link to a short YouTube video showing an assembly machine in a factory. An example task—programming a robot to move once around in a square—is presented (slide 6), and two programming solution are given, first without using a loop (slide 6) then using a loop (slides 7-10). Explain the loop block icon's five Control options for how long to keep looping commands: forever, sensor, time, count, logic. Emphasize how much faster and simpler it is to use a loop than to repeat the same set of commands over and over.
  5. Hand out the worksheet and direct students to answer the three questions in Part 1, for which they essentially repeat the slide material, but in their own words. Check their answers to verify that they understand the concepts before moving on.

Day 1: Mini-Activity 1 Presentation Outline (Slides 11-13)

  1. (slide 11) Introduce a mini-activity programming challenge: Program your robot to go forward until it hits a wall, then back up one rotation and turn left. Have the robot repeat these instructions until it detects a sound, and then stop.
  2. Divide the class into student pairs and direct them to work together to read and complete worksheet Part 2, which involves writing a program with the LEGO MINDSTORMS NXT software to perform the mini-activity task.
  3. Oversee student groups as they brainstorm, program, download and test their programs using LEGO taskbots with touch and sound sensors installed. A programming solution is provided on slides 12-13.
  4. As a group, discuss what students thought about the mini-activity. Ask them to write on the classroom board their observations, experiences and conclusions. In particular, address any difficulties that students encountered and make sure they understand how to solve them. Show students the mini-activity solution.

Day 2: What Are Switches? Presentation Outline (Slides 14-18)

  1. Walk through slides 14-18 to provide students with a detailed explanation of switches and how to implement them with the LEGO MINDSTORMS NXT software. A definition and explanation of switches is provided (slide 14).
  2. Tell students: A switch is an object that gives different commands depending on which state it is in.
  3. Present an example task: Program a robot to end up exactly 24 inches from a wall no matter where it starts (slide 15). Go through the steps for how to use a switch to make a working program for the LEGO taskbot (slides 16-18). Explain how this means that the robot will always be in one of two states, either closer or farther than 24 inches from the wall. Show students how to select the sensor they want to use for input, and how to specify what happens if the condition is satisfied or not—via the LEGO software.

Day 2: Mini-Activity 2 (Slide 19-20)

  1. Direct student pairs to complete worksheet Part 3 before using the software to program the robot.
  2. (slide 19) Then direct them to complete mini-activity 2: to program their robots to end up exactly 24 inches from a wall no matter where they start it. As mentioned in an earlier slide, this means that the robot can be in one of two states, either closer or farther than 24 inches from the wall, so students should use a switch statement to program for this task.
  3. Oversee students as they program, download and test their programs on LEGO taskbots with the touch and sound sensors installed. A programming solution is provided on slide 20.
  4. As a group, discuss what students thought about the mini-activity. Ask them to write on the classroom board their observations, experiences and conclusions. In particular, address any difficulties that students encountered and make sure they understand how to solve them. Show students the mini-activity solution.

Day 2: Mini-Activity 3 and Conclusion (Slides 21-26)

  1. Direct student pairs to complete worksheet Part 4 before using the software to program the robot.
  2. (slide 21) Then direct students to complete mini-activity 3: Program the robot to move forward until it detects a sound. If the touch sensor is pressed when it hears a sound, turn left. Otherwise, turn right.
  3. Oversee students as they program, download and test their programs on LEGO taskbots with the touch and sound sensors installed. A programming solution is provided on slides 22-23.
  4. As a group, discuss what students thought about the mini-activity. Ask them to write on the classroom board observations, experiences and conclusions. In particular, address any difficulties they encountered and make sure they understand how to solve them. Show students the mini-activity solution.
  • Administer the post-quiz by handing out paper copies; the quiz is also on slide 24. The answers are provided on slide 25. This concludes the lesson. Slide 26 contains vocabulary terms and definitions. Next, conduct the associated activity.

Associated Activities

  • Using Waits, Loops and Switches - Students are given a somewhat complicated challenge that requires them to program their LEGO robots using a combination of loops, switches and wait blocks. The robot must perform different tasks, depending on input received from a sound sensor and two touch sensors. This activity helps students envision how similar logic programs are incorporated into the many everyday devices we use.

    Watch this activity on YouTube

Vocabulary/Definitions

loop: A command or "operator" that repeats a set of commands.

switch: In programming, an object that gives different commands depending on which state it is in.

Assessment

Pre-Lesson Assessment

Pre-Quiz: Before starting the lesson, administer the three-question Loops and Switches Pre/Post Quiz by handing out paper copies (also on slide 2). Have students answer as best as they can in order to assess their base knowledge about loops and switches. Answers are provided on the Loops and Switches Pre/Post-Quiz Answer Key (and slide 3). Administer the same quiz at lesson end.

Post-Introduction Assessment

Worksheet & Iterations: For all three mini-activity programming tasks, make sure that student pairs first complete the appropriate parts of the Loops and Switches Worksheet so they have good ideas about what they want to program before they write their programs and download them to LEGO taskbots for testing. Like engineers, it is rare for students to create programs perfectly on the first attempt, so expect them to take some time and repeated efforts to make them work as intended. The teacher should, however, know the solution well (provided in the slides) and ask pertinent questions to guide students to successful solutions. Review students' worksheet answers to gauge their comprehension of the concepts covered.

Lesson Summary Assessment

Post-Quiz: At lesson end, administer the Loops and Switches Pre/Post Quiz again by handing out paper copies (also on slide 24). Expect students to be much more comfortable answering the questions on the post-quiz once the lesson is complete. Compare students' answers to their pre-quiz answers to assess their comprehension of the lesson content. Answers are provided on the Loops and Switches Pre/Post-Quiz Answer Key (and slide 25).

Additional Multimedia Support

While introducing the concept of loops, show students a video clip of an assembly machine (1:41 minutes): www.youtube.com/watch?v=AwJflkd6Y_o

Subscribe

Get the inside scoop on all things TeachEngineering such as new site features, curriculum updates, video releases, and more by signing up for our newsletter!
PS: We do not share personal information or emails with anyone.

More Curriculum Like This

Upper Elementary Lesson
How Does a Robot Work?

This lesson introduces electricity, batteries and motors using a LEGO® MINDSTORMS robot. The associated activity guides students to build a simple LEGO set-up and see the practical implementation of the concepts discussed.

Upper Elementary Lesson
What Is a Program?

Using a few blindfolds and a simple taped floor maze exercise, students come to understand that computers rely completely upon instructions given in programs and thus programs must be comprehensive and thorough. Students create and test basic programs, first using just the LEGO intelligent brick, an...

Upper Elementary Lesson
How Do You Make a Program Wait?

Building on the programming basics learned so far in the unit, students next learn how to program using sensors rather than by specifying exact durations. Working with the LEGO® MINDSTORMS® EV3 robots and software, they learn about wait blocks and how to use them in conjunction with move blocks set ...

References

NXT User's Guide. Accessed Jul 17, 2013 http://goo.gl/wuhSUA

Copyright

© 2014 by Regents of the University of Colorado; original © 2013 Curators of the University of Missouri

Contributors

Riaz Helfer, Pranit Samarth, Satish S. Nair

Supporting Program

GK-12 Program, Computational Neurobiology Center, College of Engineering, University of Missouri

Acknowledgements

This curriculum was developed under National Science Foundation GK-12 grant no. DGE 0440524. However, these contents do not necessarily represent the policies of the National Science Foundation, and you should not assume endorsement by the federal government.

Last modified: June 14, 2019

Free K-12 standards-aligned STEM curriculum for educators everywhere.
Find more at TeachEngineering.org