![]() |
Menu |
QBasic Tutorial #3 Last time I left you with this little program. ' Author:- Nick In this tutorial, I'll show how the computer can appear to be a bit more intelligent, able to make decisions based on input and modify it's output. This is achieved using the IF statement. I won't be repeating the entire text file as in previous tutorials so you will need to read them first. Here then is a simple IF script:- DIM MyInput$ INPUT "Enter your name: "; MyInput$ IF MyInput$ = "nick" THEN PRINT "You entered: "; MyInput$ END IF As before, this code goes between the CLS and END in your template (remember to use your template each time and rename it to something else which in this case could be qbif.bas but you can use any name provided it conforms to the DOS 8.3 convention). A variable is created (or DIMensioned) ready for use and is of type string (as denoted by the dollar sign). We use the INPUT statement as before to get input from the user and store it in MyInput$. It's important to use variable names that are meaningful. You will also notice that the name is mixed upper and lower case to highlight that there are two words glued together. This is called Hungarian notation and tries to make program listings more readable (spaces are not allowed in variable names). Note that the PRINT statement is indented again as an aid to readability. This becomes more important when we get to using loops in another tutorial. The variable MyInput$ is then used by the IF statement to decide whether or not to display the input. As it stands, this is not terribly useful as only the exact user input (in this case the word nick in lower case) will allow the message to be displayed. It would be more useful if the program could point out to the user the kind of input required if it is wrong. This is where the else statement makes an appearance. DIM MyInput$ INPUT "Enter your name: "; MyInput$ IF MyInput$ = "nick" THEN PRINT "You entered: "; MyInput$ ELSE PRINT "You didn't enter nick" END IF Now a message will be displayed no matter what is input. Note that an error condition will still occur even if the word Nick or NIck (or any other upper/lower case mixture) is input. To get around this, use the command LCase. Replace the following line:- DIM MyInput$ INPUT "Enter your name: "; MyInput$ IF LCASE$(MyInput$) = "nick" THEN PRINT "You entered: "; LCASE$(MyInput$) ELSE PRINT "You didn't enter nick" END IF This will convert the output from the Input command (in this case a name) and convert it to lower case. Note that the variable MyInput$ isn't actually changed but is modifed by LCASE$ (LowerCASE$) before it is printed. This could be improved by introducing a second variable to hold the modified value. DIM MyInput$ DIM MyLCaseInput$ INPUT "Enter your name: "; MyInput$ MyLCaseInput$ = LCASE$(MyInput$) IF MyLCaseInput$ = "nick" THEN PRINT "You entered: "; MyLCaseInput$ ELSE PRINT "You didn't enter nick" END IF So now, it will be possible to ask the user whether to carry out some operation or not (like overwriting an existing file for example). However, the pressing of the RETURN key is a bit messy. It would be nice to be able to respond with a single keypress. This is where INPUT$ comes into play. 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 This assumes that pressing any key other than y for yes will be taken to be n for no. This forms the basis of menu's which we will get to in due course. For the moment, hHave a play with these programs and see if you can add your own variations. The next tutorial will be a little more taxing than this one as we'll be investigating loops. |
| Written by Nick Cheesman. Last updated: 01/04/2004 Please eMail me at: nickjc@nickjc.co.uk |