Tutorial 2

If you're reading this, you're already a programmer, or you're a person that would like to become one. This is the first tutorial in a continuing series. If you can read and understand this tutorial, you're well on your way to becoming an expert programmer!

This tutorial explains the following commands:

REM

CLS

PRINT

INPUT

INT

SQR

LET

IF...THEN...[ELSE]

FOR...NEXT

GOTO

RND

END

RANDOMIZE TIMER

And...

Variables

String Variables

Integer Variables

Floating Point Variables

Line Numbers

Math Operands

This will be the longest tutorial I make (I hope). It teaches you all of the ingredients you'll need to begin making a game of your own! When you type in these lines, you can type them in all caps, or in lower case. It doesn't matter. Press Enter after each line. Type them in exactly as they are shown (except for the Caps rule I already told you.)

The first game program is short, simple, and easy to type in, but it will teach you a lot.

### Guess The Number:

Here it is:

REM ***Guess The Number***

CLS

PRINT "What is your name";:INPUT nm\$

110 RANDOMIZE TIMER

N=INT(RND*19)+1

PRINT

PRINT nm\$;", I'm thinking of a"

PRINT "number between 1 and 20."

138 PRINT

PRINT "What is my number";:input g

IF g<>n THEN 300

PRINT

PRINT "Hurray, ";nm\$;"!"

PRINT "You Guessed my number!"

FOR t=1 to 10000:NEXT t

200 PRINT

PRINT nm\$;", Do you want to"

PRINT "play again";:INPUT a\$

IF a\$="Y" OR a\$="y" or a\$="YES" or a\$="yes" THEN 110

IF a\$<>"N" or a\$<>"n" or a\$<>"NO" or a\$<>"no" THEN 200

PRINT:END

300 PRINT

IF g>n THEN 350

PRINT "Sorry, ";NM\$;". Too Small!"

GOTO 138

350 PRINT "Sorry, ";nm\$;". Too Big!"

GOTO 138

'This is the end of the program!

### Sample Game:

For this example, we'll say your name is Marvin.

You: Shift + F5

You: Marvin

Computer: I'm thinking of a number between 1 and 20. What is my number?

You: 10

Computer: Sorry, Marvin. Too small! What is my number?

You: 17

Computer: Sorry, Marvin. Too big! What is my number?

You: 15

Computer: Hurray, Marvin! You guessed my number!

---Slight Pause---

Computer: Do you want to play again?

You: No

Computer: Press any key to continue.

## Command Definitions and Use:

### REM:

REM stands for "remark." The computer ignores the REM command. Like a string tied to your finger, it is there to remind you what your program is or what it is supposed to do. For example, programmers frequently use the REM command at the beginning of a program to give the program a name, and tell instructions for the program.

Notice on the last line of the program, that there is a '. The apostrophe stands for the REM command, but looks better, and is more common on QBASIC programs. Most programs will be documented with apostrophes rather than REM commands.

Remember: Anything following REM, or ' the computer will ignore. It stops reading the line at that command.

### CLS:

CLS is a command used to clear the screen. It makes the screen completely black. (Notice how the first letters of "clear" and "screen" are in the command.) This command is not required for text, because text will just put text on the top of the screen off and put the new text on the bottom. However, when you run your program without clearing the screen before it starts, it shows the remains of the program run previously. If you clear the screen at the beginning of the program, (and in longer programs, during it) the program looks much neater.

### Variables:

Notice the INPUT nm\$ line directly following the PRINT "What is your name" statement on the third line of the program? Before I explain what print does (which is pretty self explanatory), I have to tell you what INPUT nm\$ means. Before I tell you that, you'll have to understand what variables are. Here you go:

Computers have thousands of little cubby holes in which you can store commands and information. Before you can store commands in a cubbyhole, however, you need to give the cubbyhole a name. Variables are these cubbyholes. In math class, you learned that variables are unknowns. You can't do anything else with them. For example, in math class, x * x = x squared, and x + x = 2x. You don't know what they are.

Computer variables always have a definite value. If you don't have anything in a cubbyhole, what's in it? Nothing. Nothing is equal to zero. Therefore, before you tell the computer what the variable equals, it always is equal to zero. After you tell the computer what a variable is equal to, (or before) you can use it in math problems, display it on the screen, and many other things. Variables look like this: a, a\$, a%, a! Those are the four types of variables that QBASIC can handle. I'll tell you what each type means later.

I guess I should tell you what the commands mean in order, so keep this thought in your head while you read the next section.

### Math Operands:

Math is very important to QBASIC programs. You must be able to use math in programs you write to make any half decent programs. Well... you can make some half decent programs, but not many. So let's get to it!

Multiply: *

Subtract: -

Divide: /

Exponents: ^

Square Root: SQR

Make an Integer: INT

Order of Operations: ( )

There are more of them, but these are the most important. Use them the same way that you would in math class. There's only one more catch. You have to say that a variable is equal to a math problem,. or to PRINT the math problem, or assign it to do something else. You can't just leave a math problem without telling the computer what to do with it.

I'll tell you how to use the last four mat operands mean, because they're not so obvious. The ^ for exponents makes the number(s) directly following the carat the exponents of the number proceeding it. You must have a number before and after the exponent sign. The SQR for square root finds the square root for number in parenthesis directly following it. For example, SQR(16) finds the square root of 16, which is 4. Integers are whole numbers. They can be positive or negative. What INT does is make the expression in parenthesis directly following the INT command into an integer. The INT function rounds the number to the nearest whole number. Order of Operations is very easy, but is a tough concept to understand. Say you wanted to multiply X by what the difference of Y and Z. How would you do it? Well, first, you would subtract Z from Y, and then multiply the difference by X. You write this as X(Y-Z), but in QBASIC, you would have to add an asterisk between the X and the first parenthesis. So it would look like this: X*(Y-Z). What the computer does is solve this problem from the inside out. It starts in the middle of the parenthesis, combining Y and negative Z, then multiplying this by X. If you don't understand this, read about it in your math book.

### PRINT:

PRINT puts text on the screen. Anything that you want PRINTed you must put in quotes, or be a variable. The Syntax of PRINT is: PRINT [expression list] [{,;}]. If you just type in "PRINT", a blank line will be PRINTed. Anything that is PRINTed must be put in quotes (""), or be in variable form. If you wanted to print Ahoy, there! on the screen, the statement would look like this: PRINT "Ahoy, there!". All ASCII characters can be PRINTed on the screen except for quotes ("). The answer to math problems can also be PRINTed by just writing the math problem after a PRINT statement. Also, if you put a variable after a PRINT statement, the variable's contents will be PRINTed on the screen. (Not the Variable's name!)

All this is fine except for the fact that you want to PRINT variables and regular text in one line. You do this by putting semicolons between separate things you want. If you wanted to PRINT "There are [The value of X] people in this class, and if you add ten people to the total number of people, there would be [X+10] people."? The code for it would look like: PRINT "There are";X;"people in this class, and if you add ten people to the total number of people, there would be";X+10;"people." Does that make sense? Just break everything up into separate parts. First, you want to write "There are" on the screen. So put your PRINT statement there, and type in "There are". Put your semicolon there to tell the computer that you are writing the next thing to be PRINTed. Now put X down because you want that to be PRINTed next. Now add another semicolon because you want to write another thing on the screen. Now type in the next set of text and your semicolon. Now you want to write the value of X+10 on the screen, so you write it followed by a semicolon. Now write "people." and you're done with that line of code! (A lot of work, eh? Don't worry, you'll get used to it...)

You could also do this by typing PRINT, and one expression followed by a semi colon, and it would display the same thing on the screen. For example, on one line, you type PRINT Hello, "; and on the next line, you type PRINT abc\$. The following will be displayed: Hello, [value of abc\$]. This brings us to another thing. Notice how after the Hello in the PRINT "Hello "; line I left a space? When you PRINT variables that keep track of numbers or math problems on the screen, there is a space put on both the left and the write of the character(s). When you PRINT string variables (variables that store text) on the screen, no spaces are put around the contents of the variable. To make it look nicer and easier to understand, you have to PRINT spaces on both sides of the variable. So you put a space after the end of Hello so that you don't have "wall to wall letters". If you want to print two string variables in a row, you must PRINT " " in between them to keep them separated. It would look something like this: "PRINT a\$;" ";z\$ ".

Recap:

• If you type PRINT without any variables or text after it, it will PRINT a blank line.
• Text that you want PRINTed must be in quotes. (")
• Variables can be PRINTed by adding the variables title after the PRINT command.
• Math problems can also be PRINTed.
• More than one thing can be PRINTed on a line by adding semicolons between separate things you want PRINTed.
• You must PRINT spaces between string variables and other things you want PRINTed.
• If a semicolon follows a PRINT statement, the next thing PRINTed will be on the same line.

NOTE: The PRINT Command can be used to write to sequential files, and "PRINT USING" can be used to PRINT formatted text. These are not important for you to know now.

### INPUT:

INPUT is a command that takes information from the user of the program. The person types in information, and it is assigned to a variable. You INPUT like this: INPUT [Variable name]. This PRINTs a question mark (?) and puts a cursor on the screen where you should begin to type. After all desired information is INPUTted, press enter, and it is saved in the array specified by the INPUT command. String and numeric variables can be written with this command, but if you type characters other than numbers for numeric variables, the computer will not accept the information, and will write "Redo from start", and you will be given a second chance to INPUT the information.

Now you're wondering how to get rid of that darn question mark aren't you? Well, if you make your PRINT and INPUT statements one, you don't have to look at that stupid question marks. This is how you do it: Say you wanted to write "Type in your birthday:", and right after the colon, have the user type in their birthday. The code for that would look like this: INPUT "Type in your birthday", bday\$ .

Recap:

• INPUT can write either string or numeric variables.
• When you type INPUT, a question mark is displayed to tell the user to type in information.
• If you don't want the question mark shown, make the INPUT statement into an INPUT/PRINT statement like this: INPUT "Write Number Here:", A\$
• When you write characters other than numbers onto a numeric variable, a "Redo from start" message will appear, and you can retype the variables data.

### LET:

LET actually isn't a QBASIC command. It's a command left over from BASIC that Microsoft decided to get rid of in QBASIC. Why do you tell me about, you ask? Well, because what it does, or used to, is very, very important. LET allows the computer to change variable values without direct input from the user. For example, if you wanted to create a program that counts the number of times you've shot a cannon, you wouldn't want to have to have the player type in how many times they have shot. They might cheat, and it would get EXTREMELY annoying! So, you can have the computer change numeric variables value.

You do this by saying the a variable is equal to something. It would look like this: ZUM=A+(34*56.8). This makes the variable ZUM equal to the value of A plus 34*56.8 . String variables are exactly the same, but what they're equal to must be another variable, or something in quotes. Understand? It's quite easy, and very important!

Recap:

• The LET command is not required. Don't use it! It's a waste of time and of three good bytes!
• With this command, you assign the values of numeric and string variables.
• String variables can be equal to numeric variables, but numeric variables can't be equal to strings.
• If you say that a string variable is equal to something, it must be enclosed in quotes. ("")

### Line Numbers:

Line numbers are a way to name certain portions of your program. These are essential for the computer to change positions in the program using GOTO or GOSUB statements.

What they are are numbers or words that are located at the beginning of a line or section of code. Numeric line numbers are sort of generic. They aren't customizable. Text line labels can be whatever you want, but they must be followed by a colon (:). Line numbers were required in older version of BASIC on each line of code, but now they're optional. Put them in front important sections of code so that you can send the computer to read that part of the program. In a program, they look like this: 126 PRINT "Hello, world!". The 126 on that line was the line number. Here's another example. goldfish: IF g\$="gold" AND s\$="fish" THEN PRINT "I like fish!" The goldfish: was the line label.

One more thing about word line labels: the line's name is not the word followed by a colon. It is just the word. This prevents QBASIC from thinking that a line number is actually a CALL statement. You don' need to know about these yet, just remember that the name of a line does not include the colon. It only needs it if it is the actual line number at the actual point in the program that is named by that line number.

Recap:

• Line numbers can be words or numbers.
• Word line numbers require a colon after them. The colon is not part of the name.

### GOTO:

GOTO is a command that tells the computer to go to another place in the program, and continue executing the statements. GOTO tells the computer to find a line number or label, and start reading from there. It's pretty simple. All you have to do is type in GOTO and a line number or label, and the computer will resume the program there. It looks like this: GOTO funfunsillywilly . This tells the computer to find the line "funfunsillywilly", and continue reading from there.

Recap:

• GOTO tell the computer to find another line in the program, and continue reading from there.
• The Syntax is: GOTO [line label or number]

### IF...THEN

IF...THEN asks computers yes or no questions only. You ask the computer if a variable is equal to what you asked about, it does what is after the THEN command. If the answer to the question is no, the computer ignores anything beyond the THEN command. The syntax for IF...THEN is IF [variable] [=, <, >, <>] [variable or expression] THEN [do whatever is beyond this point]. You can ask if an expression is equal to, less than, greater than, or not equal to.

The first IF...THEN statement in the program was on the 11th line, which is IF g<>n THEN 300. It asks the computer if your guess, g is not equal to the number, n. If your number is not equal to the computer's number, the computer goes down to line 300. If they are equal, the computer continues reading on the next line, ignoring the THEN command.

Pretty easy, right? Well, there are still a couple more things you should know about this command. You can use the AND, OR and the ELSE command. You can probably figure out how to use them - they're not hard. However, I'll show you how just because I'm nice.

AND is a command that you put in the IF part of the command. You put this there to ask two questions, and if both are true (have yes for an answer), the command(s) after the THEN are executed. The commands after the THEN are only executed if both are true. Here's what it would look like: IF x=18 and m\$="Hello, Dude!" THEN PRINT "Hello!".

OR works almost the same way as AND. It does what is after the THEN statement if any of the statements are true. It would look like this: IF a\$="abcdefg" OR qwert=8 * (88 + 9.2) OR B\$="Up, up and away!" THEN PRINT "!Ay Carumba!".

ELSE is a command that follows the THEN command. If the statement is false, the computer skips what is after the THEN command and does what is after the ELSE command. It looks like this: IF hi\$="Here comes RICHARD SIMMONS!" THEN 100 ELSE 200.

When you ask questions about string (text) variables, you must write the expression that the variable will equal if the question is true in quotes.

One more thing: after THEN and ELSE commands, if you want to use a GOTO command, just write the line number. The GOTO command is not required, so skip it :)

Recap:

• IF...THEN asks only yes or no questions. It's either true or false.
• If the statement is true, the command(s) after the THEN are executed.
• If the statement is false, the command(s) after the THEN command are skipped.
• AND and OR can make IF...THEN commands more powerful by making the command ask more questions.
• The ELSE command follows the THEN command, and is executed if the IF...THEN question is false.

### FOR...NEXT

FOR...NEXT is your first loop. Loops are bits of a program that tell the computer to repeat itself - to do a section of a program over again. Loops were invented because programmers are extremely lazy, and instead of wasting time and space writing PRINT "hello!" ten times, they just write FOR t=1 TO 10 : PRINT "hello!" : NEXT t . I'll explain what the FOR...NEXT loop means (and does) now.

What FOR does is name a variable and set it equal to the number before the TO, and tell the computer to repeat the part of the program up to the NEXT that many times. Simple, right? Well, there's more. You can also set the STEP for a FOR...NEXT loop. The STEP is the number that the variable adds to itself everytime it goes by the FOR command. The STEP is not required, and is by default set at one. The NEXT command does not have to have a variable following it, but it's nice to have one there so that you know what FOR loop you're returning to. The computer figures that when it hits a NEXT, it should return to the most recent FOR.

Recap:

• The FOR...NEXT loop repeats a part of a program a specified number of times.
• The syntax of this loop is: FOR [numeric variable (name of a variable)] = [numeric value (the value of the numeric variable)] TO [numeric value (The number that the loop should end on.)] (STEP [number (the number to be added to the variable each time around]) NEXT can be left without a variable or with one.
• Anything you want can be put in between FOR...NEXT loops, even other FOR...NEXT loops.
• STEP is not required, and is automatically set at one if omitted.

### END:

The END command tells the computer that the program is over, and it should stop reading lines. It's as simple as that. When the computer reads the end command, it stops reading lines, and PRINTs "Press any Key to Continue." It returns power to the system. The syntax for END is - you guessed it - END.

The END command is not required to end the program unless there is more of the program listed below where you want it to END. If the program runs out of lines to read, it assumes the program is over, and stops it.

Recap:

• The END statement tells the computer to stop reading and executing the program.
• The END command's syntax is: END.

### RND:

RND chooses a random number between zero and one. Not very impressive, eh? Well, it isn't, but if you multiply that really small number by a big one, it becomes a big, random number!

Notice the fifth line down in the program? It has the following code: N=INT(RND*19)+1 . You might understand it, but it might look Greek to you. I'll explain it step by step. First of all, this is a LET command, without the LET. (You know why LET is omitted.) This line makes the variable N a random integer between 1 and 20. Now the hard part - the math problem. Remember how computers solve math problems backwards with order of operations? Well, this problem is no different. The computer starts out with the RND command. The computer sees this and picks a random number between one and zero. Next, it multiplies the random number by 19. Now the parenthesis are gone. Next, the computer adds one to the number, so that the number is between one and twenty. You see, RND can choose a number so small that if you multiply it by twenty, still is less than one. So, to make the number that's less than one equal to it, you add one. However, then, if you multiplied the number by 20, the number would be between 1 and 21. Well, that's not right, so you have to multiply the RND by 19. Finally, the INT command rounds the number to the nearest whole number. It may be a complicated way to get a number, but you'll get used to it.

There's one more problem with QBASIC and RND's, however. IT ALWAYS PICKS THE SAME ONES! They're not random, which is why you use RANDOMIZE TIMER in front of your RND statements if you want them to be truly random. Put a apostrophe in front of the RANDOMIZE TIMER line to REM it out, and then run the program. Play the game until you win once, and then exit.

I bet you that the answer was 15. I don't know why RND doesn't work right, but the RANDOMIZE TIMER statement fixes that problem. You don't need to know what it means - just remember to put it in front of your RND statements. When you're more advanced you can learn those commands, but don't worry about it now.

Recap:

• The RND command picks a random number between zero and one.
• Multiply RNDs to get larger random numbers.
• Always put RANDOMIZE TIMER statements above RND statements.

### Variable Types:

There are two basic types of variables - numeric and string. String variables are ones that can hold all ASCII characters. They can not be used in math problems. When asking questions about them, or changing their content, the expressions must be in quotes. String variables are letters and numbers followed by a dollar sign. (\$) String names must have a letter as the first character, but everything else is up to you.

There are three types of numeric variables. The first type is called floating point. They are regular number variables. They can be any number, it doesn't matter. Variables with a percent sign on the end are integers. They automatically round themselves off to the nearest whole number. I have no idea what the ones with exclamation points are. As far as I can tell, they do the same thing as floating point variables. I've only seen them used once or twice.

Recap:

• Variables with dollar signs on the end are called strings, and can hold any ASCII character.
• Variables without any sign on the end are floating point variables. They hold numbers.
• Variables with a percent sign are integers. They automatically round to the nearest whole number.

### Challenges:

1. Make a program that PRINTs this pattern of Xs:

X

X X

X X X

X X X X

X X X X X

2. Make a simple survey program that asks you questions and responds to them. There should be ten questions.

3. Make the player of Guess The Number able to choose the difficulty of the game, and then implement it. There should be five difficulty levels. If you want to make it even more complicated, give the person a limited amount of tries.

4. Make a very basic text adventure. There should be at least ten rooms, and two enemies. You should be able to get three items, one a key, the second a weapon, and the third some object to win the game. Give the person at least three choices at each stop. They should be numbered. There should be at least four different endings.

### Final Thoughts:

Wow, that's was a LOT of typing! Thank god this is the longest tutorial. Now you should be able to write some simple text-based programs. Pretty soon you'll be able to make nicer, graphical programs. I know that you'll probably be starting an RPG tonight, but you really need more experience! You'll get a lot of flaming if you go and tell everyone that you're starting another RPG, and want somebody to teach you how to draw a circle on the screen. Just wait for a while, and after you know a whole lot of commands, and have had a whole lot of experience, make one, but not yet!

The next tutorial, with the answers to the assignment challenges will come out in less than two weeks. It'll start you out with another game, and explain all of the new commands and concepts. Until you read the next tutorial, practice with these commands. Have fun! :)

-Pete

Sunday, December 6, 1998