Machine Problems: Loops

Infinite Loops

1. print "z"
2. Go to #1.
3. Stop.


1. print "Help"
2. Go to #1.
3. Stop.


1. n = 1
2. print n
3. n = n + 1
4. go to #2
5. Stop


1. n = 1
2. print n
3. n = n + 2
4. go to #2
5. Stop


1. n = 12
2. print n
3. n = n - 3
4. go to #2
5. Stop

Loops that Stop

Sometimes you want to end a loop.
1. w = 1
2. print w
3. w = w + 1
4. go to #2 if w < 10.
5. Stop



1. w = 10
2. print w
3. w = w - 1
4. Stop if w < 3
5. go to #2



1. i = 0
2. print "help"
3. i = i + 1
4. Go to #2 if i < 7
5. Stop


1. i = 12
2. print "Q"
3. i = i - 1
4. Stop if i = 0
5. Go to #2

It is often useful to have students try to explain what they are doing. The point is not to correct them, the point is to make them explitize what they are doing, so that their skills will transfer better. You can ask "What do these instructions do?" or "What is i doing in these commands?"

Incompetent Programs

This is a reasonable set of instructions. What will the machine do?
1. n = 1
2. print n
3. Stop if n = 10
4. n = n + 1
5. Go to #2

Some silly kindergartners wrote these instructions. They made a mistake. But the machine just follows instructions. Be the machine!
1. n = 1
2. Stop if n = 10
3. n = n + 1
4. Go to #2

1. n = 1
2. print n
3. Stop if n = 10
4. n = n + 1
5. Go to #1

1. n = 1
2. print n
3. Stop if n = 10
4. n = n + 2
5. Go to #2

1. n = 1
2. print n
3. Stop if n < 10
4. n = n + 2
5. Go to #2

1. n = 1
2. print n
3. Stop if n < 10
4. n = n + 1
5. Go to #5

Be the Machinemaster


Write commands that will make the machine keep printing the letter z.

Write commands that will make the machine print the letter z 1032 times.

Write commands that will print out the numbers from 1 to 978.

Write commands that will print out the numbers from 0 to 500, counting by 2's.

Write commands that will print out the numbers from 100 to 0, in that order.

Lecture

There two different concepts here. The first is the idea of a loop. The simplest manifestation of that is a loop that never stops. The second idea is a way of getting out of the loop. Sometimes this is when some event has occurred -- you are printing out the numbers from 1 to 100 and you have reached 100. The other possibility is when you are printing out "z" 100 times. Now you need a counter.

So, the following are three different programs. In theory, students should be able to say that the second has a loop and the third is a loop and a counter. In practice, my third graders couldn't do this at all.
1. Print "Ashley"
2. Stop

1. Print "Ashley"
2. Go to #1
3. stop

1. i = 0
2. print "Ashley"
3. i = i + 1
4. stop if i = 50
5. go to #2
6. Stop

First Day at the Real Machine

I used these problems for their first day at actually telling the computer what to do. The first is a tradition, so don't change it.

Make the machine print out the message "Hello World".

Make the machine keep printing out the same message over and over again.

Make the machine print out a message 50 times.

Change the number of times it prints out the message. (Note to teachers: This should be a simple change in one command.)

Make the machine print out the numbers from 1 to 50.

Change the commands so it does the numbers from 1 to 100. Change the commands so that it counts by two's, or counts by three's.

Make the machine print out the "squares" of the numbers from 1 to 50

What is the sum of the numbers from 1 to 100?

"If"

Backing up for a moment, the loops introduce the "if" conditional -- Stop if i = 100. I have not made much of this because my students never had any trouble with it, perhaps because it matches normal English so well. You could teach the "if" conditional before teaching loops, but there isn't that much motivation for it, especially compared to its use in exiting loops. But just for the sake of thoroughness, a spagetti problem with conditionals.

A mnemonic. The "less than" sign (<) is smaller on the left than the right, and it says that the number on the left is smaller than the number on the right. The "greater than" sign (>) is larger on the left than the right and it says that the number on the left is larger than the number on the right.

1. a = 1
2. b = 1
3. go to #10 if a = 1
4. a = a + 37
5. go to #12 if a > 50
6. a = a + 37
7. go to #12 if b > 50
8. a = a + 37
9. go to #12 if a > 50
10. a = a + 3 if b = 0
11. go to #4 if a = 1
12 print a
13. print b
14. stop