| The Trick Of The Mind - The Goto Fixed! |
| Written by Mike James | |||||
| Wednesday, 24 December 2025 | |||||
Page 2 of 4
What isn’t obvious at this stage is that this simple If...Then conditional instruction can cover every conditional possibility, but it can as long as the instruction following the Then can be another If and the condition can be sophisticated. For example, a two-way selection, i.e. either do A or B, can be written: If it is raining Then pick up an umbrella If it is not raining Then pick up a hat To avoid repetition, it would be more natural to say “if it is raining pick up an umbrella, otherwise pick up a hat”. Most programming language allow a more sophisticated form of the If instruction to do the same: If it is raining Then pick up an umbrella Else pick up a hat It is just a matter of convention that we use Else rather than Otherwise or some other term. The general form is: If condition Then instruction1 Else instruction2 and if the condition is true instruction1 is obeyed and instruction2 is skipped and if it is false instruction1 is skipped and instruction2 is obeyed. You can carry on like this adding additional forms of the If instruction to make things easier, but the first simple form is all you need. Using it or one of its extended forms you can build conditionals that select between any number of alternatives depending on a range of conditions. You can think of this like a railway shunting yard with a complicated rail layout that allows wagons to be shunted into any branch. It looks complicated, but the entire network is made up of simple switches that select between two options. That is, if you have an If..Then..Else you can easily build any conditional flow of control you need, but as this can be created using just If..Then this also does the job. When it comes to loops then things are just as simple. All we need is a single loop instruction: While condition Do instruction which obeys the instruction repeatedly while the condition is true. Notice that this is a little more complicated than the If because it involves a rereading of the whole text of the loop for it to make sense. The reason for this is that the condition has to be re-evaluated each time before you do the instruction again. For example: While not sweet enough Do add sugar If you just repeated Do add sugar the result would be an infinite amount of sugar being used. What has to happen for this to work properly is that you have to evaluate the condition by tasting whatever it is, if the condition is true you perform the instruction, i.e. add sugar, but then you have to go back and re-evaluate the condition, taste whatever it is again and do the instruction only if it is true. That is, when you repeat a loop you not only repeat the instruction you also repeat the evaluation of the condition. This is much more sophisticated than the conditional. It is more like a little machine for repeating something hidden in an innocent-looking control instruction. As in the case of the conditional, the While loop is sufficient to implement any type of loop, but real computer languages generally add other forms to make things easier. For example, most programming language add an enumeration loop that simply repeats an instruction a known number of times. For example: Repeat 3 Times print hello results in hello, hello, hello. You don’t really need an enumeration loop because the While loop can be used to implement one by having the condition based on counting how many times the instruction has been obeyed, see the next chapter to find out how. These simple control statements, the default, the If..Then and the While are all you need to write any list of instructions and therefore to get any job done. You can see the flow of control graphs for the three below: The first is the default, the second is a conditional If..Then..Else and the final one is a While loop. Notice that the conditional is a choice between two alternatives, i.e. it really is an If..Then..Else, but you can leave either the left or the right branch empty to implement an If..Then or, far less common, an If..Else. The While flow of control graph also implicitly includes jumping back to repeat the evaluation of the condition and a jump out of the loop if the condition is false. It really should be drawn as:
Where the backward pointing arrow is the jump back to the start of the loop and the forward pointing arrow is the jump out of the loop when the condition evaluates to false. This may be accurate but few think of it like this. You can add additional statements to make things simpler or more efficient, but these are all you need as everything else can be built from them. Notice that there isn’t a Goto in sight. |
|||||
| Last Updated ( Wednesday, 24 December 2025 ) |
