Programming with Loops

Learning Goals

Slide navigation: Forward with space bar, , or PgDn. Backwards with or PgUp.

Step 1: Two Owls

Continue with https://hourofcode.com/vidcard. Erase any work you may have and instead start with this code:

var owl = graphic("crazyowl.png")
owl.scale = 0.5
owl.x = 100
owl.y = 0

Now put a second owl to the right.

  1. Call it owl2
  2. Set owl2.x to 200.

.png

Step 2: Four Owls

Now make four owls.

.png

Step 3: Owls with Loop

Step 4: Many More Owls

Step 5: Owls and a Bug

Step 6: Alternate Owls and Bugs

.png

It's getting tricky. Can you place

type = 2

and

type = 1

into the right spots so that the type changes each time: 1 2 1 2 1 2 ...

Step 7: Vertical Owls

Back to just owls. Erase everything. Copy the code from Step 4, but now arrange the owls vertically:

.png

Step 8: Diagonal Owls

Und schräg?

.png

Step 9: Two Rows of Owls

.png

Tip: Copy the code from Step 4 again. Copy it one more time so you get two rows. Unfortunately on top of each other. Fix the y in the second loop.

Step 10: Many Rows of Owls

.png

For four rows, you could copy again and once again. But why copy when you have loops? Here is a loop that makes many rows. Each row is another loop. A loop in a loop!

var m = 0
var n = 0
while (m <= 400)
{
  while (n <= 600)
  {
    var owl = graphic("crazyowl.png")
    owl.scale = 0.5
    owl.x = n
    owl.y = m
    n = n + 100
  }
  n = 0
  m = m + 100
}

Just try it out. And then try bringing the owls closer to each other.

Step 11: Owls and Bugs Checkerboard

This brain teaser is here because I wasn't sure if any of you are programming geniuses. Don't worry if you have no idea.

Can you alternate owls and bugs, like on a checkerboard?

.png

Step 12: Breathe Deeply and Relax

This was a rapid fire introduction into programming with loops. It is nice that one can explore loops so nicely with JavaScript.

What did we learn?