![]() |
Menu |
QBasic Tutorial #4 Last time I left you with this little program. ' Author:- Nick ' Date:- Now ' Description:- Hello program ' Comments:- Pretty simple eh? ' Start of program CLS DIM MyInput$ DIM MyLCaseInput$ PRINT "Please enter yes or no (y/n): "; MyInput$ = INPUT$(1) MyLCaseInput$ = LCASE$(MyInput$) IF MyLCaseInput$ = "y" THEN PRINT "You entered yes" ELSE PRINT "You entered no" END IF END ' End of program Did you remember to use the template? No more reminders from now on. This particular tutorial may need a little more concentration as we are to embark on the tortuous subject of loops. It took me many months to get my head around loops and mulitiple loops still give me a headache. Take a look at this. DIM i FOR i = 1 to 5 PRINT "Variable i is now set to "; i NEXT i This is known as the FOR loop.Why FOR? Think of it as a bit of quaint english as in 'For surely this is our winter of discontent'. The variable i although not very descriptive is an index which is added to each time the loop goes round. Here's how it works. i is set to be the value 1 and a message is PRINTed showing that. The NEXT tells the computer to go back to the FOR and add 1 to the value (the STEP command has been omitted for clarity) then go through the loop again except that it displays the number 2 in the message box and so on until the value of 5 is reached and the loop ends which in this case means the end of the program. I can see you're a bit baffled. I'll put it another way. i starts off as 1 because that is the initial setting for the FOR loop. The value of i is then printed. The NEXT tells the program to add 1 to the value of the index and go back to the beginning of the loop although this time i is now set to the value of 2 which again is printed. This goes all the way until i is set to 5 and is finally printed and it drops out of the loop to the next command which in this case is END. The loop can be any set to run using any valid numbers although the bigger the second value, the longer the loop will run which may temporarily hang QBasic. It could even be set to run from 6 to 1 if required and can skip values using the form: FOR i = 1 TO 6 STEP 2 or go backwards: FOR i = 6 TO 1 STEP -2 so that in this case only every other value is used. Take a while to digest this if you are new to programming. DO Loops These are slightly different and look like this: DIM i i = 1 DO i = i + 1 PRINT "Variable i is now set to "; i LOOP UNTIL i = 5 The DO LOOP differs in that some of the work involved must be done by the programmer, particularly when it comes to incrementing the index variable. Generally, this kind of loop would not be used for this kind of application as a FOR loop would obviously be better but I've used it so that you can compare the two pieces of code which give precisely the same results. So the variable i is set to one (QBasic would normally set it to zero but it is good practice to give it a value even if it is going to be zero). The loop starts and we add one to the index value (increment it). This means that it is actually now set to 2 thus the PRINT statement first displays the value as being 2 and NOT 1. Where you put the incrementing code (i = i + 1) is important. In this case, starting i at zero would have produced the result we were expecting. Remember, the computer will not do it for you as in the above example. The message box is displayed then the value of i is tested to see if it has reached 5. The loop will continue until this condition is met. Note that LOOP is a keyword and cannot be used as a variable name which some BASIC's use to simulate this kind of structure. The computer goes back to the DO (NOT the i = 1) and goes through the code again, adding 1 (could be more if required), displays the value of i and tests in case it has reached 5. Eventually it will and the loop will end. Be aware that if a mistake is made and the loop never ends then the program will lock up the QBasic and you will need to shut it down from Task Manager. Don't be afraid to experiment and make mistakes - you'll learn much more from your mistakes than from your successes. I think that's probably enough brain bending code for this session. |
| Written by Nick Cheesman. Last updated: 01/04/2004 Please eMail me at: nickjc@nickjc.co.uk |