eca00000ff23fe0080666001f70 ^1|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| ^2 B E G I N N E R S ' C O R N E R - ^2 B A C K T O B A S I C S . ^1|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| ^7 In the last Issue of Totally Amos, we took a look at variables, this ^7time we'll have a look at loops with the help of The Beginner's ^7Programming Handbook, (By Usbourne Books) and the Easy Amos manual ^7(because it's easier for a non programmer like me to understand!) ^6 Loops are a way of gettong the computer to do things over and over ^6again without you having to tell it each time. ^7 There are a few types of loops in Amos, let's start with one that ^7pops up everywhere - the FOR NEXT loop. ^6 An example is usually the best method of explaining something, so we ^6have decided that we want to print the words TOTALLY AMOS on the ^6screen 10 times. ^6 We could use the following routine:- ^2Print"TOTALLY AMOS" ^2Print"TOTALLY AMOS" ^2Print"TOTALLY AMOS" ^2Print"TOTALLY AMOS" ^2Print"TOTALLY AMOS" ^2Print"TOTALLY AMOS" ^2Print"TOTALLY AMOS" ^2Print"TOTALLY AMOS" ^2Print"TOTALLY AMOS" ^2Print"TOTALLY AMOS" ^6 But that is very repetative, and imagine if you needed to do ^6something 1000 times! ^7 In a FOR NEXT Loop, we can set a counter which will detirmine the ^7number of times that an instruction is carried out, this is set as:- ^2For T = 1 To 10 ^6----- T acts as a counter, it tells the computer ^6to do something 10 times, the computer is ^6told that this counter is to count from 1 ^6to 10. Every time the program comes back ^6here, 1 is added to T, the program stops ^6when the counter reaches 10. ^7 Now we want to tell the computer what we want printed. ^2A$ = "TOTALLY AMOS" ^6----- A$ is a string variable which we are using ^6to store the words "TOTALLY AMOS". By ^6storing it in a variable, we would be able ^6to use it later in the program just by ^6calling A$. ^7Here we tell the program to print. ^2Print A$ ^6---- We are telling the computer to print the ^6words stored in A$. This is what we are ^6telling the computer to do 10 times. ^7Now that's been done, what's next? ^2Next T ^6---- This tells the computer to go back to the ^6first line and carry out the instruction ^6again. ^7 A more complicated FOR NEXT loop is a Nested Loop. This simply ^7means that one FOR NEXT loop is tucked inside another FOR NEXT loop. ^7This means that you can do 2 things at once. ^7Let's look at this example:- ^2 o - For I = 2 To 12 ^2 u | ^2 t | For J = 1 To 12 _ ^2 e | | ^2 r | Print J;"Times ";I;"=";J*I | Inner Loop ^2 | | ^2 l | Next J _| ^2 o | ^2 o |_ Next I ^2 p ^7 The counter in the outer loop, I, is counting from 2 to 12. The ^7inner counter, J, counts from 1 to 12. ^7 This means that each time I counts 1, J counts from 1 to 12 before I ^7goes on to count another one. ^2 ie I = 2 J = 1 ^2 2 ^2 3 ^2 4 and so on to 12 ^2 I = 3 J = 1 ^2 2 ^2 3 ^2 4 and so on ^2 Print J;"Times ";I;"=";J*I ^7 This line tells the computer to print the current value of J, ^7followed by the word "Times" (NB inverts ") then the current value of ^7I, followed by the character `=' and finally the result of J ^7multiplied by I. The semi-colon tells the computer that there is ^7something more to print before going on to the next part of the ^7program. ^6 Have you guessed what this program will print? ^6 It will, in fact print out the times tables from x2 up to x12. ^6 Broken down this is done as follows:- ^6 I = the number of the times table ^6 J = the numbers from 1 to 12 ^7 The tables are printed in order as a long list, ^2 1 Times 2 = 2 ^2 2 Times 2 = 4 ^2 3 Times 2 = 6 ^2 4 Times 2 = 8 etc ^2 Then 1 Times 3 = 3 ^2 2 Times 3 = 6 ^2 3 Times 3 = 9 ^2 4 Times 3 = 12 etc ^2 All the way up to 12 Times 12 = 144 ^6 Can you see how the outer loop (I) only increments after the inner ^6loop (J) has run from 1 to 12? ^7 One rule to remember is that both parts of the inner loop MUST be ^7INSIDE the outer loop, if not, you'll get an error message. ^2 ie For I.... ^2 For J.... ^2 Next J ^2 Next I ^1|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| ^4 If you have a Basic Blind Spot, please write and let us know so that ^4we can try and help. We hope that you can see from this series of ^4Basic Basic, that nothing is too trivial, it is all too easy to ^4assume that `everyone must know that!' The beginner's bits in the ^4last 2 issues of TA were inspired by Amos users who suffered such ^4blind spots. Let's hope that these articles have helped. ^1|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| \