Introducing Loops

Home/Making Progress.../Introducing Loops
Introducing Loops 2016-12-23T09:30:56+00:00

Making programs work harder using loops

So far all your programs have been linear – they start at line 1 and flow consecutively to the last line of the code.  Although this works for small programs, it just won’t hack it if you are going to get the computer to do the hard work for you.  Your aim should be to write as few lines as possible while still making the program do all it can do.

Flowchart to loop

A loop to draw a square.

Introducing the counted loop

Loops enable your program to go back over a block of statements as many times as you need.

Counted loops enable you to repeat lines of code a set number of times.  To illustrate counted loops we are going to go back to Turtle Graphics.

The ‘Flow Chart‘ on the right shows how we could get the turtle to draw a square by repeating two commands 4 times.  Flow Charts are an important tool to help use develop programs.  It is important to realise that not all shapes in the flowchart convert into single statements in your programming language, while some shapes are combined when converting into code.

Turtle Polygons

Box5P

Counted Loops
This button will show you the structure of a counted loop in Small Basic.  It is very similar in most programming languages.

Box5G

The code
This button will show you the code created from the flowchart.  Flowcharts enable you to plan your programs without necessarily knowing the details, but being able to convert them into code is an essential skill. to master.

Using counted loops with other code

Of course, counted loops are not just part of turtle programming.  They can be used whenever actions need to be repeated a set number of times.

Times Table loop

A loop to output the 7 times table.

This next program will display the 7 times table using a counted loop.

This flowchart starts with the process and decision boxes that you need to now view as the ‘For…To…Step’ loop statement.  The decision this time is to test to see if we have exceeded 12 – we are going to do the 7 times table up to 12 times.

Straight after the decision box, we set a variable ‘Product’ to equal the value of the loop variable times 7.  It is perfectly OK to use the loop variable but it can be unpredictable to alter it; far better to let your program look after its values.

The final flowchart symbol in the loop indicates that we are going to output some information to the screen – in this case the value of the loop variable followed by some literal text and finally the value of the ‘Product’ variable.

Check over on the right (or below) how this looks as Small Basic code.

End of Topic Tasks

The Times Table code

Box5G

The code
This button will show you the code created from the Times Table flowchart.  You are advised to copy the code (sorry – you will have to type it yourself) into Small Basic to see exactly how it works.  You should then modify it to see if you can create other times tables.

Box5P

Counted Loops 2
This button will show you other examples of counted loops and explain what they do.

Another Example

Simple Coloured Bar
Although the TextWindow is very basic, it is still possible to ‘draw’ on it.  The flowchart shown in the popup makes use of the fact that changing the background colour only affects the background of characters printed after it.  By using spaces – ” ” you can print coloured blocks on the existing screen background.  Thus it is possible to create simple bar charts especially when using loops to make each bar a different length!