tutorial 4 for begginers

Yes i'm afraid, even though you can print, input, CLS and even create simple gosub commands, 
you are still a begginer.

This fourth installment shows how to loop a program, an invaluable feature.

LOOPS

Okay, a loop, well it speaks for it's self really, a loop in an electrical circuit, 
a loop in a racing track, a loop in a stuck record, a loop in a computer program, 
they all mean the same thing.  A loop is something that when told, the computer will take you back to that point and start again, a little badly put, but i can show you an example using knowledge that you know already.

Back:PRINT "Hello"
PRINT "Hello again"
PRINT "And again"
GOTO back

WHAT EVER YOU DO, DO NOT RUN THIS PROGRAM!!!  Want to know why...

As you can work out the computer will print the three lines and then start again and 
print the same three line over again.  this will happen at the speed of your computer 
and as you know, it's very fast.  That means that the standered Ctrl C won't work to stop 
the program and you'll have to re-boot your computer and lose all unsaved information. 
You can try it to see the effect, but expect to get stuck and have to restart.

Okay, well that is your first looping program, but how can we make it practical, 
here is a 'practical' example of the use of using the Gosub command to create a loop.

Restart:CLS
PRINT "H=Hello Q=Quit R=Restart"
INPUT "Would you like to say hello, quit or restart" a$
IF a$ = "H" THEN GOSUB hello
IF a$ = "Q" THEN GOSUB quit
IF a$ = "R" THEN GOSUB restart
gosub Restart             'Incase non are selected

hello:cls
print "Hello, i've cleared the screen"
input "press a key and then enter to quit" leave$
quit:END



See how simple it is, infact i even got a little stuck doing that off the top of my head 
as it seemed too simple, but let's take a closer look and pick out the short cuts i took.
I have used the IF command again with the THEN to get good results, i hope you still 
understand that, i have put gosub restart after all of the IF statments incase the user has
 half a brain and types in Z or something and if that were the case, the computer would
 realise that A$ does not = H,Q or R and carry on as it does not need to gosub to any of 
the THEN's and then would read the gosub restart.
The only actual label that contains anything is the hello label, that is because the restart 
gosubs straight to the restart instantly as soon as R is pressed, the Quit just follows on 
with END.
And another shortcut kind of, is were i did not end the hello section with a gosub Quit as 

that made no sence as the quit followed it anyway, so as soon as a person pressed a key and 
then enter after being said hello to, the computer would read end anyway and terminate the 
program.
I had to design it that way, becuase if i, lets say put the quit:end above the hello label,
 then obviously the hello label would need to have a gosub quit to go back up to the quit to
 end the program.  Get it?, oh and to just complicate it, you would'nt need to do that for 
this particular example because as soon as the computer ran out of lines it would stop anyway, 

but you get the picture i hope.

Anyway, thats just manipulating the program to make it loop, here are the more common and 
more useful types:

DO WHILE
DO UNTIL
FOR, NEXT

Okay, we'll go through each one quickly and you'll have to try them out in a couple of examples.

DO WHILE LOOP

DO WHILE is the command that you start the line with to begin a loop, this means that the 
computer will 'do' i.e. loop to a point (The point it loops to in the program is labeled loop), 
WHILE, so loop while something is either true or false.
I'll explain, you see everything about programming really boils down to whether something 
is true or false. so DO WHILE a$ = "j" means that the computer will keep on looping while 
the person keeps on typing J, or better put, until a$ = j is false.  When a person presses 
j on the keyboard for example a$ = j is true and the loop continues.  Of course in practical 
terms there would have to be an input after the DO WHILE command to actually input the letter 
in the first place for the computer to read.  But when the person presses k, the computer finds 
a$ to be false in the do while loop and leaves the loop and continues the program:

CLS
DO WHILE A$ = "J"
INPUT "Press J to do it again, anything else to quit" A$
LOOP


So here you see we have created a loop without having to manipulate the GOTO or GOSUB commands. 
The LOOP at the end tells the computer to resturn to the co-insiding DO WHILE.  
It should be noted that i am explaining this in the most basic terms and terminology for 
your easy understanding.  If you were taking an exam for example, you would not use the frase 
'Co-insiding DO WHILE', you say something like the 'linked LOOP procedure of the function a$' 
or something complicated like that.

So WHILE, loops while something specified is true or false, if it is true, i.e matches the 
variable (thats the a$), then it loops again and again until it does'nt, so the next loop 
command comes easily, the UNTIL Command.

DO UNTIL command

So instead of While something is something, its do until something is something, and this 
will be used far more frequantly then the WHILE.

I will not write a program, as it would be good to exercise you ability to think logically 
about the situation.

quick e.g

DO UNTIL a$ = "yes"
INPUT a$
LOOP

The program will continue to loop until the user types in "yes"

The next loop statment is the for and next.

for and next allow extreme customisation of your projects loops, this is because:

FOR a% = (1 to 100)
NEXT a%

Here you see how the NEXT acts as the LOOP command at the bottom, so if you can't work it out,
 the computer will basically count to 100 and then stop the loop and carry on to what evers
 next (Makes a good time delay in programs with intro's and stuff), and notice the brackets that allow the computer to recognise that whats inside it is the peramitors of the variable a% (You'll come across a similar idea as this with Arrays)

That's pretty simple and time is getting on, so experiment with the 
FOR (VARIABLE) = (something to something)
NEXT (VARIABLE)
and create some time delays.

There are countless uses for the above, and i would go into them, but at this stage it's
 best to keep it simple.


Keep trying with the basics and make some programs with text and time delays and see what
 else you can do with loops.

If you as always need any help contact me.

Written by Jamie for anyone



  


    Source: geocities.com/jamo20010