Modelling



Loops

At its simplest, a Loop in programming involves making a section repeat. It means that instead of going on a different path, like selection, you repeat the same path.

There are two basic types of loop - definite and indefinite:

  • definite - loops for a specified number of times
  • indefinite - loops for an unkown number of time depedning upon some condition

For example, say that we want to find the squares of all the numbers between 5 and 10

We can use the function that we created for a sqare number repeatedly but with a different number.

We could do it long-hand like this:

console.log(calculateSquare(5))
console.log(calculateSquare(6))
console.log(calculateSquare(7))
console.log(calculateSquare(8))
console.log(calculateSquare(9))
console.log(calculateSquare(10)

That is not efficient and repeating lines of code always introduces the risk of human error and typos.

In programming, we could divert the route through the program using an definite loop and write the line of code just once.



The Algorithm

Diagrams are good at showing Selection - this is a Flow Chart. It shows the order of processing for the algorithm with the added advantage of showing the route too

In this example, we want to output the squares of all numbers between 5 and 10. Notice that:

  1. a counter is used to do the counting
  2. the question is asked to determine if the final value has gone past the top limit
  3. there code is repeated dependent upon that condition

image of loop


The Algorithm in Words - Pseudocode

When writing code, we will often have an interim stage where we write the program using mainly English.

This method is called PSEUDOCODE and is independent of any actual programming language.

We could write this code one of TWO ways - using a definite loop or an indefinite loop:

Definite Loop - Pseudocode

Here we use a FOR loop.

This method has the instructions for the counter to change as part of the FOR instruction.

There is no TEST - it is definite.

image of fucntion
Indefinite Loop - Pseudocode

Here we use a WHILE loop.

This method has the instructions for the counter to change as part of the Loop code.

There is a TEST BEFORE every repetition - it is indefinite.

image of fucntion


The Program as JS Code

Here is how the code for the program is written in JS.

The concept is still that there is a controlling MAIN routine which calls a SUBROUTINE

Generating a Random Number

This uses built-in functions:

  1. Math.random() generates a number between 0 and 1
  2. Math.random() + 100 makes the number between 0 and 100
  3. Math.floor() rounds the number down

The IF statement

This uses brackets:

  1. the condition is in round brackets
  2. each path is in a set of curly brackets
  3. else seperates the paths

the logic is: IF (condition is TRUE) follow the first path ELSE follow the second path

image of fucntion


Running the Function

Here is how the code for MAIN routine and SUBROUTINE are written in JS.

The concept is that there is a controlling MAIN routine which calls a SUBROUTINE

This video shows the order in which the program is executed.