βΏ Nested Loops
September 28, 2016π― Learning Target
I can use nested loops to solve programming problems.
Do Now (in Google Classroom)
Why are loops useful?
Letβs look an example. Write code to draw two squares, next to each other.
Nested Loops
A loop in a loop.
In other wordsβ¦
How many times will the sprite take 20 steps?
Lab 2.2 - Itβs Brick
In this lab, you will use nested loops to draw a large brick wall using as little code as possible.
Part 1 - Build a Brick
- Write a SNAP script to draw a single 20x10 βbrickβ in the lower left corner of the stage when the green flag is clicked.
- Modify your code to draw two bricks side by side. The bricks should share a short edge.
- Now modify your code again to build a full row of bricks across the entire length of the stage. Use loop to keep your code as concise as possible. Remember that the stage is 480 pixels wide and 360 pixels tall (by default- you can change that).
Part 2 - Build a Wall
- Now that you can build a row of bricks, add code to build a second row above the first row. The bricks in the second row should share a long edge with those in the first row, but should be βoffsetβ so that the ends of the second row bricks are at the middle of the first row bricks.
- Modify your code to build more rows, alternating back and forth between the βregularβ and βoffsetβ rows. Use nested loops to keep your code concise.
- Finish off the wall the by building alternating rows all the way to the top of the stage. Remember that the stage is 360 pixels tall. Your final wall should look like this:
Can you build the whole wall with one nested loop?