Great another tutorial. This is my 4th one! In this tutorial we cover more about loops, IF...THEN...ELSE and SELECT CASE. Lets begin with some loops stuff. You should already now about using GOTO, GOSUB and FOR..NEXT satements but what about DO..LOOP or WHILE...WEND? I don't often use the WHILE..WEND statements but I'll teach you them anyway. The WHILE..WEND statements are similar to the FOR..NEXT loop but with a few differences. Take a look at this: '-------------------- WHILE N < 100 PRINT N N=N+1 WEND '-------------------- The program above will loop around 'while' N < 100 once the variable reaches the set number it will stop looping and continue as normal. That's all there is to know about WHILE...WEND, here is a different type of loop, very much like the one just shown. DO...LOOP --------- These are very much similar as to the ones above but have better controll with them. look... '-------------------- DO PRINT N N=N+1 LOOP UNTIL N = 100 '-------------------- The program above will do exactly the same thing as the previous one it has just been set out differently. The DO...LOOP statements can not only use the UNTIL command but also the WHILE. '-------------------- DO PRINT N N=N+1 LOOP WHILE N < 100 '-------------------- Notice that on the bottom line the same syntax has been rewritten to do exactly the same thing! It usually desn't matter which one you use. With the DO...LOOP method you don't have to but the test on the bottom line (e.g. the line with the word LOOP) you could put it on the the DO line. watch: '-------------------- DO PRINT N N=N+1 LOOP UNTIL N = 100 '-------------------- '-------------------- DO UNTIL N = 10 PRINT N N=N+1 LOOP '-------------------- One other thing about loops is that if you don't have a test then the loop will continue forever! Well now you're a master of loops! Practice using what you know to make your own programs too. IF..THEN..ELSE -------------- An IF statemnt or 'statement block' can be used to evaluate variables or other things and execute code based on the result. Look at this IF statement and try to guess how it works: '-------------------- CLS LET N = 5 IF N = 5 THEN PRINT "N IS 5" IF N = 10 THEN PRINT "N IS 10" '-------------------- The program starts off by making the variable N the value of 5 The following two statements are IF's they perform tests on the variable and execute a command as required. The output from this would be 'N IS 5' you can see why, I won't bother explaining. IF's can also make more complex lines: '-------------------- CLS LET N = 5 IF N = 5 THEN PRINT "N IS 5" ELSE PRINT "N IS NOT 5" '-------------------- This program is similar to the one above but on the line with the IF an extra piece has been added on 'ELSE PRINT "N IS NOT 5"' This piece is executed if and only if N doesn't equal 5. Take a look at this example: '-------------------- CLS LET NAME$ = "DAVID" IF NAME$ = "JANE" THEN PRINT "HI JANE" ELSE PRINT "NICE DAY?!" '-------------------- When you run this program the output is 'NICE DAY?!' the 'HI JANE' part is not shown because NAME$ is not equal to 'JANE'. Now onto statement blocks. These do exactly the same as what you've just seen except they can execute more than just one statement: '-------------------- CLS LET NAME$ = "DAVID" IF NAME$ = "DAVID" THEN PRINT "HI THERE" PRINT "DAVID" END IF '-------------------- Notice the END IF? This tells the computer that all the code enclosed within the IF and the END IF is to be executed when the test is TRUE and that it should not be executed otherwise. You can also add the ELSE command into a statement block: '-------------------- CLS LET NAME$ = "DAVID" IF NAME$ = "DAVID" THEN PRINT "HI THERE" PRINT "DAVID" ELSE PRINT "NICE DAY?!" END IF '-------------------- Using what you have learnt already you should be able to see how this works. Here's a hint the final output is: HI THERE DAVID Practise using IF statements and statement blocks, they can be very powerfull. Also in larger programs try to use tabs they can help in decoding and for later review. SELECT CASE ----------- This is another way of testing things. SELECT CASE statement blocks can be used for branching and an be used to replace IF's when a single variable can have many different results. Take a look at another example: '-------------------- CLS FOR N = 1 TO 5 SELECT CASE N CASE 1 PRINT "MONDAY" CASE 2 PRINT "TUESDAY" CASE 3 PRINT "WEDNESDAY" CASE 4 PRINT "THURSDAY" CASE 5 PRINT "FRIDAY" END SELECT NEXT N '-------------------- You should already know what the FOR and CLS do so I'll go straight onto the SELECT CASE. Some of you may already be able to see what it does, but I'll tell you anyway. The first line SELECT CASE N tells the computer to take out a notepad and get ready for some expreiments on the variable N. The next line CASE 1 can be translated into an IF statement so.. IF N = 1 THEN ..... Yeah? Anyway, the line(s) directly below that are what are executed if this is true. This is the same for the other 'CASE ' lines. The END SELECT line means stop testing. Simple! A different type of CASE line can be added, this is CASE ELSE. It is the same as an IF's ELSE statement. If we put this in the program above we have: '-------------------- CLS FOR N = 1 TO 7 SELECT CASE N CASE 1 PRINT "MONDAY" CASE 2 PRINT "TUESDAY" CASE 3 PRINT "WEDNESDAY" CASE 4 PRINT "THURSDAY" CASE 5 PRINT "FRIDAY" CASE ELSE PRINT "SATURDAY - SUNDAY" END SELECT NEXT N '-------------------- Branching like what you've just been shown can be extreamly useful in replacing loads of IF's! That's not to say that you can just use SELECT CASE instead of IF. Well hope you've learnt something new in this tutorial. See ya soon.