Objective

To introduce the concept of repetition and show how this can be used in computer programs.

To learn that repetition is more commonly called a loop.

To learn the different types of loops; to introduce the pattern for a loop that repeats while a condition is true.

The class will practice loops by adapting the previous timesquestion program.

Creating these program will require the class to use the text editor (either Atom or LiteIDE) to create and save the source code file and the terminal/command line to run their program.

Learning outcome

The class should be able to:

  • Start their text editor
  • Create a source code file
  • Save a source code file
  • Build and run their program
  • Understand what repetition is
  • Understand what repetition is used for
  • Understand that repetition is more commonly called a loop
  • Understand that there are different kinds of loops
  • Understand the loop pattern to loop while a condition is true
  • Understand when the condition is tested in such a loop
  • Adapt the timesquestion program to create the timesquiz program

Teaching prerequisites

The pupils must have encountered the comparison operators, <, > and = in mathematics. Comparison is fundamental to loops as well as selection so the pupils must be already familiar with the idea from mathematics.

In addition to this, the pupils need to have covered Lessons 1-9 in this series.

Repetition. The program demonstrates the use of repetition to continually ask the pupil a multiplication until they answer correctly.

Selection. The loop in the program is controlled by a selection.

Variables. The program uses variables for user input and the variables are used again within the condition that controls the loop as well as within the loop itself.

Input and Output. The program asks the user to input their answer and outputs if they are correct.

Sequences. The program demonstrates a sequence of instructions that are executed in order to print the results to the terminal window.

Maths. The example program,timesquiz will ask the pupils to solve multiplications from the 1-12 times-tables.

Drawing Squares

Notes for Slide 2

The first challenge asks pupils to draw or think about shapes.

Ask the pupils to draw a square, an equilateral triangle and a regular pentagon or select these shapes from a set of irregular shapes. Ask the pupils how they drew (or would draw) these shapes? What steps did they take? How many steps did they need for each shape?

Obviously a square has 4 sides with 90 degree internal angles. An equilateral triangle has 3 sides with 60 degree internal angles and pentagon has 5 sides with 108 degree internal angles

You can draw all of these shapes be repeating a pair of steps.

The second challenge is about multiplication. So

2 + 2 + 2 + 2 = 2 * 4 = 8

So multiplication is the same as repeated addition. The pattern is of the form

3 * n

Where n varies from 1 to 12.

So a times-table is generated be repeating (almost) the same sum.

Repeating Lines

Notes for Slide 3

You can draw a square by repeatedly drawing a line and then turning 90 degrees at the end to give you the direction of the next line.

The pattern is the repeated pair of steps to draw the side and then rotate.

If you repeat this 4 times you will draw a square. It doesn’t matter what the initial orientation of the first line is. Repeating the steps still draws a square. This is an algorithm for drawing a square.

Both the triangle and the pentagon can be drawn using a similar approach. The only differences are the number of repetitions required and the size of the angle to rotate by.

Drawing Squares

Notes for Slides 4, 5, 6, 7

These slides show the square as it is drawn, after each pair of steps.

Repeating Numbers

Notes for Slide 8

The pattern in

3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12

is

3 * n

where n has the values 1 to 4 and increases by one at each step.

The pattern in

4 * 3 = 12
5 * 4 = 20
6 * 5 = 30
7 * 6 = 42

is

m * n

where m has the values 4 to 7 and increases by one at each step. And n has the vales 3 to 6 and increases by one at each step.

Both patterns can be repeated to generate the tables.

Repetition

Notes for Slide 9

Both the pattern for drawing squares (and equilateral triangles and regular pentagons) and the pattern for multiplication as repeated addition and the pattern to create the times tables are all examples of repetition.

Repetition means to repeat a sequence of instructions - i.e, just part of the program - over and over again. Either for a fixed number of times or until some condition becomes false.

Programs can have many repetition statements in them.

Repetition makes the program both shorter - so less typing - and easier to understand. Repetition makes the program logic clearer. Repetition also means that the program more closely resembles the algorithm that it claims to implement. Look at the square drawing algorithm. It is clearer to say “Repeat this pair of steps 4 times” instead of listing the pair of steps 4 times.

The hard part is spotting a pattern that can be repeated. This will only come with practice.

Loops

Notes for Slide 10

Programmers call repetition loops. A sequence of instructions is looped over a number of times.

Programmers also talk about different types of loops. So there are loops that repeat while a condition is true - called a while loop. There are loops that repeat a fixed number of times - called a for loop. There are loops that run forever - called an infinite loop.

The difference between the loops is presence or absence of the condition. For loops also have the additional property of specifying the step-size of the loop.

The lesson is going to look at while loops. The next lesson will look at for loops. These are a simple extension to a while loop.

The Loop Pattern

Notes for Slide 11

Go uses the keyword for to introduce a loop, regardless of which type of loop it is.

The pattern for a loop that repeats while a condition is true is

for condition {
    statements-to-repeat
} // last brace

The condition is tested before the loop starts. If the condition is true then the statements-to-repeat are executed. If the condition is false then the statements-to-repeat are skipped over and execution continues after the last brace. In this respect, a loop behaves in a similar way to an if statement. In fact there is an implicit if test in any while or for loop.

If the condition is true the statements-to-repeat are executed. Once these are executed the condition is tested again. Think of the last brace causing jump back to the for line.

If the condition is again true the statements-to-repeat are executed again, for a second time. Once the statements-to-repeat have been executed, the condition is tested again.

This process repeats until the condition is false.

The condition is tested before each time though the loop. Each time though the loop is called a loop iteration so the condition is tested before each loop iteration including the first one.

Loop Example

Notes for Slide 12

The loop example show how to use a for loop to create a loop that repeats until the condition is true.

The example continually asks the user to have a guess at the secret word until the user guesses the correct answer (Banana).

The pupils should be able to use logical reasoning to work out what the program does when the user types “Goldfish” or “Banana” on their first guess. They should also be able to work out what happens if they type “Goldfish”, “Shark” and “Banana” on successive guesses.

Ask the pupils to write down the programs output for each guess.

    1 var word string
    2 var secretWord string
    3 secretWord = "Banana"
    4 
    5 fmt.Println("Guess the secret word.")
    6 word = simpleio.ReadStringFromKeyboard()
    7 
    8 for word != secretWord {
    9     fmt.Println("Sorry that is not the secret word!")
   10     fmt.Println("Try and guess again!")
   11     word = simpleio.ReadStringFromKeyboard()
   12 }
   13 
   14 fmt.Println("Congratulations!")
Fig-1. The loop example code

If the pupils first guess is “Goldfish” it reads “Goldfish” into the variable called word on line 6. The condition on line 8

word != secretWord {

Is true - “Goldfish” is not the same as the secret word “Banana” so the loop statements between the { and } are executed. So he program executes lines 9 and 10 and prints out

Sorry that is not the secret word!
Try and guess again!

and then waits for the pupil to enter the next guess on line 11.

If the pupils then enters “Shark” the process repeats. “Shark” is stored in the variable called word. Now the condition is checked again. The condition is still true because “Shark” is not the same as the secret word “Banana”.

The loop is therefore executed again and again, prints

Sorry that is not the secret word!
Try and guess again!

Now if the pupil enters “Banana” as their next guess, then word would contain the value “Banana” and the condition (“Banana” is not equal to “Banana”) would be false. Now that the condition is false the loop is skipped over and the program prints out.

Congratulations!

If the pupil had guessed “Banana” as their first guess this would also have been the result.

The timesquiz Program

Notes for Slide 13

The timesquiz is an extension of the previous timesquestion program from the if else selection lesson.

Like the timesquestion program the timesquiz program asks the pupil to solve a multiplication question from the 1-12 times tables. Also like the timesquestion program the timesquiz program tells the user if their guess in to large to small. But unlike the timesqestion program the timesquiz program will not exit until the pupil answers correctly. If the pupil guess is wrong the program asks for anther guess. The only way to exit the program is to enter the correct answer.

The timesquiz program looks like this

    1 package main
    2 
    3 import (
    4    "fmt"
    5 
    6    "github.com/gophercoders/random"
    7    "github.com/gophercoders/simpleio"
    8 )
    9 
   10 func main() {
   11 
   12    var a int
   13    var b int
   14    var answer int
   15 
   16    fmt.Println("The timesquiz shows you how to use loops.")
   17    fmt.Println("Can you remember your times tables?")
   18    fmt.Println("")
   19 
   20    a = random.GetRandomNumberInRange(1, 12)
   21    b = random.GetRandomNumberInRange(1, 12)
   22 
   23    fmt.Print("What is ")
   24    fmt.Print(a)
   25    fmt.Print(" * ")
   26    fmt.Print(b)
   27    fmt.Println("?")
   28 
   29    answer = simpleio.ReadNumberFromKeyboard()
   30 
   31    for answer != a*b {
   32        if answer > a*b {
   33            fmt.Println("Sorry, your guess was to big.")
   34        } else {
   35            fmt.Println("Sorry your guess was to small.")
   36        }
   37        fmt.Println("Try again ")
   38        answer = simpleio.ReadNumberFromKeyboard()
   39    }
   40    fmt.Println("Congratulations! You are correct.")
   41 }
Fig-2. The timesquiz code

The pupils run the program and guess incorrectly twice, then the output is similar to

The timesquiz shows you how to use loops.
Can you remember your times tables?

What is 11 * 6?
45
Sorry your guess was to small.
Try again
89
Sorry, your guess was to big.
Try again
66
Congratulations! You are correct.

Notes

Remember that the numbers for the multiplication are chosen randomly. When the pupils run the programs they will see different numbers.

The program is very similar to the previous timesquestion program.

The pupils guess is read on line 29 using the ReadNumberFromKeybaord from the simpleio package. Their guess is stored in the variable answer.

The loop extends from lines 31 to 39. The loops condition

answer != a*b

is on line 31. If the pupil guess correctly on their first guess then this condition is false. The loop is then skipped over and the next line to execute is line 40

fmt.Println("Congratulations! You are correct.")

so the complete output from the program would be.

The timesquiz shows you how to use loops.
Can you remember your times tables?

What is 11 * 6?
66
Congratulations! You are correct.

If the pupil guesses incorrectly, for example 45, then the condition on line 31 is true so the loop is executed.

The first line in the loop is an if else statement. The condition in the if else statement is

answer > a*b

This is false (45 < 66 in this case) so the next line to be executed is line 35

fmt.Println("Sorry, your guess was to small.")

The output at this point would be

The timesquiz shows you how to use loops.
Can you remember your times tables?

What is 11 * 6?
45
Sorry your guess was to small.

The remainder of the loop lines 37 and 38 now execute. Line 37 asks the user to try another guess. Line 38 is identical to line 29 and stores the pupil’s (new) guess in the answer variable.

If the user now has a guess of 89 the output would be

The timesquiz shows you how to use loops.
Can you remember your times tables?

What is 11 * 6?
45
Sorry your guess was to small.
Try again
89

At this point, execution has reached line 39 which forces execution back to line 31 to repeat the loop.

The condition on line 31 is evaluated again and again is true (89 != 66). The loop is therefore executed for a second time.

This time the condition in the if else statement on line 32 is true (89 > 66)

answer > a*b

so the next line to be executed is line 33

fmt.Println("Sorry, your guess was to big.")

The output at this point would be

The timesquiz shows you how to use loops.
Can you remember your times tables?

What is 11 * 6?
45
Sorry your guess was to small.
Try again
89
Sorry, your guess was to big.

The if else statement is now completed so the remainder of the loop lines 37 and 38 now execute. Line 37 asks the user to try another guess. Line 38 is identical to line 29 and stores the pupils (new) guess in the answer variable.

If the pupil now enters 66 as their next (3rd guess) the output would look like this:

The timesquiz shows you how to use loops.
Can you remember your times tables?

What is 11 * 6?
45
Sorry your guess was to small.
Try again
89
Sorry, your guess was to big.
Try again
66

Again the execution hits line 39 the last brace in the for loop which forces the execution back to line 31.

Now the answer is correct, the value of answer is 66 which is 6 * 11. So the loops condition

answer != a*b

is now false. This breaks the loop. The entire loop body is skipped, lines 32 to 39, and the next line to be executed is line 40

fmt.Println("Congratulations! You are correct.")

Which results in the complete output of

The timesquiz shows you how to use loops.
Can you remember your times tables?

What is 11 * 6?
45
Sorry your guess was to small.
Try again
89
Sorry, your guess was to big.
Try again
66
Congratulations! You are correct.

Having printed the congratulations messaged the program terminates.

Plenary

Notes for Slide 14

This can be carried out with the class as a whole or the pupils can do it themselves.

The objective is to change the timesquz program so. Now if the user guesses incorrectly the program should ask them another new multiplication with two new random numbers.

If the user guesses correctly the program should work as before.

The output of the revised program would be similar to

The timesquiz shows you how to use loops.
Can you remember your times tables?

What is 12 * 9?
34
Sorry your guess was to small.
Try again
What is 10 * 11?
100
Sorry your guess was to small.
Try again
What is 9 * 5?
95
Sorry, your guess was to big.
Try again
What is 12 * 7?
84
Congratulations! You are correct.

The challenge is not a hard as it seems. There are two keys to solving the challenge.

First, the pupils need to read the challenge carefully. It states that the behaviour should only be different if the pupils guesses incorrectly. This implies that program behaviour needs to change only when the condition

answer != a*b

is true. This implies that the changes need to be made to the statements that the loop will execute.

The challenge also tells them that the program should still tell then if their guess was to large or small before the new question is presented.

This implies that the changes have to happen after the if else statement inside the loop.

Secondly, the pupils need to realise that they already have the code to create two new random numbers and to ask the multiplication question earlier in the program. The code they are looking for is in lines 20-27 of the timesquiz program.

So the solution is simply to copy lines 20-27 and to paste them in between lines 37 and 38.

This gives the complete solution

    1 package main
    2 
    3 import (
    4    "fmt"
    5 
    6    "github.com/gophercoders/random"
    7    "github.com/gophercoders/simpleio"
    8 )
    9 
   10 func main() {
   11 
   12    var a int
   13    var b int
   14    var answer int
   15 
   16    fmt.Println("The revised timesquiz shows you how to use loops.")
   17    fmt.Println("Can you remember your times tables?")
   18    fmt.Println("")
   19 
   20    a = random.GetRandomNumberInRange(1, 12)
   21    b = random.GetRandomNumberInRange(1, 12)
   22 
   23    fmt.Print("What is ")
   24    fmt.Print(a)
   25    fmt.Print(" * ")
   26    fmt.Print(b)
   27    fmt.Println("?")
   28 
   29    answer = simpleio.ReadNumberFromKeyboard()
   30 
   31    for answer != a*b {
   32        if answer > a*b {
   33            fmt.Println("Sorry, your guess was to big.")
   34        } else {
   35            fmt.Println("Sorry your guess was to small.")
   36        }
   37        fmt.Println("Try again ")
   38 
   39         a = random.GetRandomNumberInRange(1, 12)
   40         b = random.GetRandomNumberInRange(1, 12)
   41 
   42         fmt.Print("What is ")
   43         fmt.Print(a)
   44         fmt.Print(" * ")
   45         fmt.Print(b)
   46         fmt.Println("?")
   47 
   48        answer = simpleio.ReadNumberFromKeyboard()
   49    }
   50    fmt.Println("Congratulations! You are correct.")
   51 }
Fig-3. The revised timesquiz code

Lines 39-47 are the new lines.

Lines 39 and 40 generate to new random numbers and store them in the variables a and b.

Lines 42-46 print the question and line 48 reads the pupils answer into the variable answer as before.

Now that the value of the variables in a and b have changed then loop test on line 31 will test the pupils answer against the new values of a and b and not the previous values.

The loop test itself is now dynamic. It changes each time the loop executes.

Search

Featured Lesson

Start here...

Hello and welcome

Hello and welcome to GopherCoders.com.

Do you want to learn to program a computer? If you answered YES you have come to the right website. Follow along with us and let us introduce you to the world of computer programming.