Loops Revisited

//Loops Revisited
Loops Revisited 2016-12-23T10:50:48+00:00

The ‘While’ Loop

Earlier you were introduced to the counted loop – one that happens a specific number of times.  These are also called Definite Loops. Often, we need to loop through a section of code but we don’t know how many times; it could be forever or to could be never.  These are called Indefinite Loops.

While loops enable you to write code that loop depending on conditional values held in your program.  From your work in the last chapter you should realise that computers can only make choices / decisions that return either True or False.  These decisions are referred to as Boolean because there are only those two options – there is no in-between or maybe.

While loops check the required condition before entering the loop.  They then only execute the loop while the answer is True; hence the name while.  On the right (or below in a narrow screen) is a reminder of the way we can compare two values / variables in computer code.


This example shows the basic way that While… …EndWhile loop functions.

Very basic while loop

This program simply carries on until the user enters Zero.  This is not particularly useful as it does nothing with the numbers nor does it tell the user what to do.

However, do notice though that the input statement is repeated inside the loop as well as before entering the loop – it is essential that the variable used to control the loop gets changed inside the loop – other wise the loop will go on forever!  Also note that the decision has been modified between the flowchart and the code – they both do exactly the same – it is the labelling on the decision box output lines that dictates the comparison used.

The following example is more informative and useful – it tells the user what to do and if they don’t do it, it repeats until they do.

More useful while loop

This final example uses a ‘While… …EndWhile’ loop to get the user to keep entering numbers and adds them up as they go.  It then outputs the sum of all the numbers input.

This is a more practical example of how these loops can be used.

Very useful while loop There is one issue in both the program code and the flowchart – I have assumed that the variable ‘Tot’ equals zero at the start.  This is not good practice – always initialise variables to a known value before using them.

Now is a good time to check that you really understand those comparisons and what happens if you compare letters and numbers and even empty strings.  While… …EndWhile loops are useful tools to test that you are comparing the correct value sin the correct way.

Available conditions

This is used to test if two values are exactly the same.  The values can be literal, i.e. a specific value entered directly into the code or variables.
Symbol used: =
Example: If (Name = “Jane”Then… (The variable ‘Name’ contains the letters making the word ‘Jane’.)
This is used to test if two values are NOT exactly the same.  Again the values can be a literal and a variable or two variables.
Symbol used: <>
Example: If (Clock.Hour <> 6) Then… (It is not between 6 and 6:59:59 999 in the morning)
This is used to see if the first value / variable provided is bigger then the second value/variable provided.  This works with words as well as numbers with “B” being greater than “A”.  Note that “a” is also greater than “A” because of the way characters are coded using the ASCII or UTF-8 coding formats.
Symbol used: >
Example: If (Clock.Day > 29) Then… (It cannot be February!)
This tests both equality and being greater than.  It always compares the first value with the second so again to return “True” the first value must be the same or more than the second value.
Symbol used: >=
Example: If (TextWindow.CursorLeft >= 10) Then… (Something has already been typed on the screen or the cursor has been moved)
You can use this to test  to see if the first value/variable is smaller than the second value/variable.  Again, this works with both letters and numbers.
Symbol used: <
Example:  If (Clock.Year < 2020) Then… (It’s not the Year 2020 yet)
This condition enables you to test for equality and being less than.  As before it always compares the first with the second value/variable and returns “True” if the first value is the same as or less than the second.
Symbol used: <=
Example: If (Clock.Month <= 3) Then… (It’s Winter)
Mid Topic Tasks

The ‘While’ Loop extended

So far you have only needed to test one Boolean Expression.  In the same way that you can join questions together to make complex If… …Then… …Else… …EndIf statement, you can do the same with the conditions in loops.  I.e. it is possible to test more than one expression at the same time.  In theory you can ask to test as many as you like but it gets very difficult to predict if the overall answer will be true or false.  In practice, we try to limit ourselves to two or three.  (Your teacher might want to introduce you to ‘Truth Tables’ here.)

To make two Boolean Expressions work and return either ‘True’ or ‘False’, you need to be able to join them together.  This is done using the keywords ‘AND’ or ‘OR’.  Here’s an example…

While using compound decisions The brackets are not strictly needed in this example but it does help to identify each part.  Notice that valid number are between 5 AND 10 but our conditions are joined using OR and test for values OUTSIDE the range.

Lateral Thinking

If you have tackled the key task in the ‘End of Topic’ tasks then well done.  However, there is another way making use of Array.InArray().  As an extra task, can you develop this alternative solution.

This is one of the great things about programming computers that spills out into real life.  There are always different ways of achieving the same end result.  Some are quicker, some are more efficient but there is usually, always several ways of getting to where you want to go – remember this when choosing school, college or life options.

? End of philosophy sub-section. ?

End of Topic Tasks