If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

Main content

Teaching guide: Intro to JS - Looping

This is a teaching guide for the Intro to JS lesson on Looping.

What the student will learn

  • How to use loops to repeat code, changing a value in the code sequentially each time (like to draw a row or column of shapes).
  • The syntax for a while loop and a for loop.
  • How to nest loops inside each other, a useful technique for changing two dimensions of values (like to draw a grid of shapes).

The student will be able to write code like:

Where students struggle

  • Students sometimes forget that they actually have to use the loop counter variable in their shape commands - otherwise their program will just draw a bunch of shapes on top of each other.
  • Students may wonder when they should use a while loop versus a for loop. A while loop is the most general kind of loop, a for loop is a more specific syntax that’s great for iterating through a sequence of numbers. For most drawings and animations, students will want to use a for loop.
  • Students often forget all the parts of the for loop header. They can remember it by checking out the documentation example. Remind them that they don’t have to memorize programming syntax at first, as they will often have documentation - the important thing is to practice using the syntax and to understand it.
  • Students may write loops with the classic "off by one" error. That’s when their condition isn’t quite right, because they’re stopping one iteration too early or too late. Encourage them to think carefully about their condition, and what the values will be in the final iteration of code that will be executed.

Additional materials: Unplugged activities

An "unplugged" activity is one that you can do with students without needing to use a computer at all. They can help convey concepts in a more visceral way, and they can also be a backup activity for when computers fail.
Code.org offers a For Loops Fun activity (see overview video, lesson plan, and example in-class video).

Additional materials: Discussion questions

These are questions that you can ask students individually after they've done the lesson, or lead a group discussion around, if everyone's gotten to the same point.
  • Look back at previous programs your students have made. Find ones with rows/columns of shapes, and ask students to point out where they could use loops. Perhaps if they have time, they can upgrade them.
  • Look around the room. If you were drawing the room, where would you use loops?

Additional materials: Trivia questions

These can be fun to do as a class after everyone’s gotten through the lesson. They can also lead to discussion about which questions are the hardest. Play them on Quizizz.

Want to join the conversation?

No posts yet.