QBasic Simple and complex Kevin B. Kohler Chapter II User input + if then else In this chapter we will go over the commands input, if, then, else, randomize(timer), rnd, and something called common shared(we are only using common shared because we don’t know how to pass variables yet). First open Qbasic and type in this short sample program. Declare sub UserInput() cls common shared Name$ Print “Hello what is your name?” ‘prints out a statement to the screen UserInput ‘calls a sub called UserInput Print “Hello ” Name$ create the sub UserInput and put inside it the following Sub UserInput() input ; “” , Name$ print END SUB The first command mentioned, and one we wont take much time discussing is common shared. Its use is pretty straight forward, it automatically lets you use a variable in all parts of you program. Just take apart the name, Common means its easy to find, shared means that every one gets to use it some of the time. The next new command we will take a while discussing. It lets the user input either numeric or string variables depending on the variable you put after it. For instance, if you put a numeric variable it wont take characters for input, but if you put a string variable after it you cant expect it to use operations correctly. This is one of the reasons you should plan out your program before hacking out lines of code. You should always take the time to figure out what kind of variable your going to need. If you don’t do this you may end up with problems later. Now that, I hope, you understand the basics of the input statement let me teach you the many different things you can do with it. First off the empty quotes can be used like a print command. So let us change the program by taking out the question what is your name, and instead of putting it into the main program put it into the input sub inside the quotes and then run the program. You will notice there is no question mark. You can add a question mark directly to the quotes in the input, or you can simply put a ; instead of a comma after the quotes. You can also take the quotes & “;” out completely and have the statement look like this “ input ; Name$ “(note “input Name$” will work too with the same effect). This will simply print a question mark I don’t know why, but it doesn’t work with a comma. The next commands we will learn will be if then else, Randomize(timer), and rnd. We will show how to use these by creating a number guessing program. It will create a random number between 1 and 10, and you have to try and guess it. Type in the following program Declare sub CreateRandomNumber() Declare sub UserGuess() Declare sub CheckGuess() common shared RandomNumber ‘allows the variable to be used in all subs common shared PlayersGuess ‘same as above cls Randomize(Timer) ‘sets rnd statement to be randomized using the computer clock CreateRandomNumber ‘calls a sub to create a random number UserGuess ‘calls a sub to take users input CheckGuess ‘calls a sub to check the input end You should create all of the 3 above subs and put into them the following ‘This sub creates a random number between 1 and 10 Sub CreateRandomNumber() RandomNumber = int(rnd*10)+1 END SUB ‘This sub gets user input Sub UserGuess() input ;”guess a number between 1 and 10 ”,PlayerGuess end sub ‘This sub checks to see if you have guessed correctly SUB CheckGuess If PlayerGuess = RandomNumber then ‘checks to see if you guess is correct Print “YOU GUESSED CORRECTLY” else ‘tells it what to do if it isn’t Print “You guessed wrong, the number was ”;RandomNumber end if ‘ends the if statement END SUB The Randomize(Timer) command tells the computer to take a random number based by the timer every time the rnd statement is used. The Rnd statement simply takes a random number between 0 and 1. By multiplying this number by another number such as 10 we end up with a number between 1 and 10, for example .5* 10 = 5 or .3258 *10 = 3.258. The function int in front of the statement tells the computer to chop of the decimal so instead of 3.258 you would have a random number of 3. The plus 1 simply makes it so it wont equal 0 and will sometimes equal 10. If then statements however, are more complex. In a if then statement the first element is the if. It says if *blank* is true then do *blank* *blank* *blank* ect. end then it terminates the if statement with an “end if” statement. If there is only one *blank* after the “then” you can put it on the same line and take out the end if so it would look like this. If PlayerGuess = RandomNumber then Print “YOU GUESSED CORRECTLY” This is shorter, and I don’t see any reason why you shouldn’t do it. Another thing you can do with if then statement is put in else statements. Else statements say that if the if statement isn’t true then run this instead. After the statement you should have the same kind of structure. You put in the statements to run in under it. There are 6 different commands you can use in if statements. The first is = saying if a number (or string variable)is equal to another number(or string variable/string) then run the following. The others are <>, >, >=, <, and <=. <> means not equal to, > means greater than, >= means greater than or equal to, < means less than, and <= means less than or equal to. For our perposes we will say these 4 don’t doesn’t work on string variables. It is possible to say - “if PlayerGuess then print “YOU GUESSED CORRECTLY””, but this would be pointless because it won’t actually test for anything. Also you can use multiple statements in if then statements to determine things. You can do this by stringing them together like - “If PlayerGuess = RandomNumber and Playerguess > 0 and < 11 then”. This will make it so it only put you into the sub if all of the following are true. If one of them is true but not the others then it will not go into the if then statement(and in our case it will go to the else). If you want to go into the if statement when only one of them is true you would have to change it to “If PlayerGuess = RandomNumber or PlayerGuess > 0 or PlayerGuess <11” This would greatly screw up our program, but it would succeed in what it was supposed to show(that being how the or statement works). There is one other similar statement called xor but I will not go into that now. You have reached the end of the second chapter. Can’t you all ready feel the power of being able to tell your computer what to do. Its an ego rush isn’t it! Hints and Tips About chapter II and a little beyond 1) The more complex your program gets, the more comments you should include. 2) If you don’t know weather or not to put the statements of the if then statements on another line because you might have to write more, put em on another line and put an end if after em 3) if in doubt, use parenthesis. 4) Proper indentation is very important in programming. All statements inside a if statement should be indented two spaces. The same goes for loops, subs, and case statements. Copyright 1998 Kevin B. Kohler