« Middle School Courses
This course is included with our Coding/STEAM Curriculum - Middle School Plan

Lesson Plan

Lesson: While Loops

Time: 40+ mins

Introduction

In this lesson, students will use conditional “while” loops to iterate through instructions.

Vocabulary

  • While Loop: A set of code that repeats while a certain condition is true. The loop will end when the condition becomes false. A “while” loop in Python has the following syntax:
  • Iteration: A single repetition of a loop. We often talk about loops in terms of the number of iterations that it has.
  • Iterator Variable: The variable that stores a value which will determine the number of iterations. This variable must be initialized before the loop starts and will be used in the condition of the while loop. The variable must also be incremented inside the while loop to avoid an infinite loop.
  • Infinite Loop: A loop that will repeat forever, usually due to the iterator variable not being incremented. You want to avoid these as much as possible as they will crash the website. For example, the following piece of code would cause an infinite loop:
    while (4==4):
        print(‘hi’)
  • Accumulator Variable: A variable defined outside of a loop that accumulates a value during the loop. This is often used to calculate the sum or product of a series of numbers or variables.
  • Factorial: The factorial of a number "n" is the product of all numbers between 1 and "n," including "n." This is written mathematically as "n!".

Objectives

Students will...
  • Recognize patterns in their code
  • Use while loops to reduce the number of lines of code that they need
  • Write conditional statements to control while loops
  • Use variables to control iteration and accumulate values
  • Control a program’s flow by using break and continue statements

Materials

  • Computers (1 per student) with student account access to Tynker.com

Warm-Up (5 minutes)

Ask students to describe or provide an example for at least three of the following terms:
  • Variable
  • Boolean
  • Math operators
  • Turtle graphics
  • Loops
  • Branching

Activities (35 minutes)

Facilitate as students complete the While Loops modules on their own:
1. While Loops (Tutorial)
    • Students will read a short document that explains “while” loops.
    • Is the animated iteration description moving too quickly? Click the pause button to stop the animation, and click it again to continue playing.
    • Are students struggling with the “Print every year of life” puzzle? Tell them to use the code in the “iteration” section as a reference.
    • Tell students to click the “Next” button (located at the bottom of the document) to move on to the next module.
2. Components of a While Loop (Tutorial)
    • In this module, students will read a short document that explains three components that determine the behavior of a “while” loop.
    • Are students struggling with the “Counting down” puzzle? Give a hint: Tell them to include this in their solution:          n = n - 1
    • Optional: Ask students “What is the difference between a ‘while’ loop and an ‘infinite’ loop?”
3. Repeating Instructions (Tutorial)
    • Students will read a short document that explains how a “while” loop provides a way to repeat instructions (rather than typing the same set of instructions multiple times). Additionally, students will see how to combine “while” loops with turtle graphics.
    • Are students struggling with the “Print hello” puzzle? Give a hint: Tell them that one possible solution involves creating another variable to count up to “n.” In other words, they will need to include a line of code that increase the variable by 1.
    • Optional: Challenge the adventurous coder to find at least two different solutions for the “Print hello” puzzle.
    • Tell students to click the “Next” button to move on to the next module.
4. Accumulating Results (Tutorial)
    • Students will read a short document that explains situations where they will have to accumulate a result over multiple iterations of a loop.
    • This module includes three different puzzles with provided hints.
    • Tell students to click the “Next” button to move on to the next module.
5. Branching In A While Loop (Tutorial)
    • Students will read a short document that explains branching in a “while” loop.
    • Are students struggling with the “Collatz conjecture” puzzle? Give a hint: Tell students to use a modulus operator (%). Also check that students understand that “3n + 1” needs to be expressed as “3 * n + 1”
    • Are students struggling with the “LCM of 3” puzzle? Give a hint: Tell students to use the code in the “LCM” example as a reference.
6. Break (Tutorial)
    • Students will read a short document that explains the break instruction.
    • After students play the first example, remind them to remove the break instruction from the program. Did the output change? Why?
    • Are students struggling with the “Is it prime” puzzle? Give a hint: Tell students to try using an “else” statement.
    • Optional: Discuss with your students the purpose of including a break instruction as part of their “factorial” puzzle solution.
7. Continue (Tutorial)
    • In this module, students will read a short document that explains the “continue” instruction.
    • Optional: Ask students, “Are the continue instruction and break instruction the same?” (No, unlike the break instruction, the continue instruction does not end the loop--it only ends the current iteration.)
    • Students will need to solve one puzzle in this module. If students are struggling, direct their attention to the provided hint or encourage them to brainstorm possible solutions with a neighbor.
8. Quiz
    • This quiz requires students to apply concepts from this lesson to solve 4 different puzzles. There are no multiple choice questions.

Discussion Questions/Follow-Up Activities (20 minutes)

Lead a discussion with your students:
    • How are loops helpful?
    • Who can describe one way you applied loops in today’s lesson?
    • Why is it important to indent your code?
    • How is a “while” loop different from an “infinite” loop?

US Standards

  • CCSS-ELA: SL.8.1, RI.9-10.3, RI.9-10.6, RI.11-12.3, RI.11-12.6, L.9-10.3, L.9-10.6, L.11-12.3, L.11-12.6
  • CCSS-Math: HSN.Q.A.1, HSN.Q.A.2, HSN.Q.A.3, MP.1, MP.2, MP.4
  • CSTA: 2-AP-11, 2-AP-12, 2-AP-13, 2-AP-15, 2-AP-17, 3A-AP-17, 3B-AP-11, 3B-AP-12
  • CS CA: 6-8.AP.11, 6-8.AP.12, 6-8.AP.13, 6-8.AP.15, 6-8.AP.17, 9-12.AP.12, 9-12.AP.14, 9-12.AP.16
  • ISTE: 1.c, 1.d, 4.d, 5.c, 5.d
This course is not part of your plan. Please upgrade to view all answer keys

Class Presentations

These student-facing slide presentations help educators seamlessly run Tynker lessons in a virtual or physical classroom setting. Each lesson has its own set of slides that introduce the big ideas, suggest unplugged activities, and include a section for each activity module. While running lesson slides, you can switch back and forth between the activity, the slides, answer keys and other lesson materials.
A sample slide presentation is available for your review. Please log in to view all the class presentations available with your plan..
Lesson 1
Welcome to Python
32 Slides
Lesson 2
Variables and I/O
29 Slides
Lesson 3
Data Types
29 Slides
Lesson 4
Math Operators
32 Slides
Lesson 5
Boolean Logic
35 Slides
Lesson 6
Turtle Graphics
31 Slides
Lesson 7
Branching
27 Slides
Lesson 8
While Loops
27 Slides
Lesson 9
Strings
32 Slides
Lesson 10
Lists
28 Slides
Lesson 11
For Loops
24 Slides
Lesson 12
Functions
28 Slides
Lesson 13
Dictionaries
20 Slides
Lesson 14
Classes and Objects
24 Slides
Lesson 15
Recursion
20 Slides