Chapter 2
e-me|links|files|help|contests|main|projects|aboutme|guestbook|survey|forum|chat|frames
Welcome! here is my second tutorial for my help section. I'm actually working
on it (I know it's hard to believe, but I promise I'll finish all my chapters,
some day.) Well, here's what you'll learn this time.
1. What else can I do with variables?
2. If Then statement
3. Loops
4. for-next (loop)
5. do-while, loop-until (loop)
6. goto (loop)
7. gosub (loop)
8. while-wend (loop)
9. on-gosub, on-goto (loop)
10. select case (loop)
11. inkey$
tip: if you get into an endless loop press ctrl and break (usually
break is also the pause button)
______________________________________________________________________________
What else can I do with variables?
Well, last chapter we taught you how to make a variable, but what do we do
with it once we have it?
Tons of stuff, almost the whole language is based on variables, because most
programs will do something depending on what the variables are.
for example if we had a racing game depending on what direction the car was
facing, what button we pressed, what car we are using, and other stuff then we
would move a certain way.
Well, this has many variables
what direction we're facing
what button was pressed
what car was being used
and whatever other variables we wanted to use to determine how it moved
So in this chapter you will learn how to tell the computer what to do when a
variable equals a certain thing.
______________________________________________________________________________
If-Then statement
One way to make the computer do something when a variable equals a certain
thing is an If-Then statement
here's how it works:
If a$ = "devin" THEN PRINT "hi, devin"
this can work in many other ways also, it can be any type of variable and
after the THEN it can do anything we want (ex. locate, input, print, color,
cls, or whatever.)
______________________________________________________________________________
Loops
What if I want to do the same code over and over again (do I have to keep
typing it?) No, use loops.
There are tons of kinds of loops and here they are:
for-next (loop)
do while (loop)
loop until (loop)
goto (loop)
gosub (loop)
while-wend (loop)
on-gosub (loop)
on-goto (loop)
select case (loop)
And that's all I can think of, but if I remember any other important ones I'll
add them to other chapters.
______________________________________________________________________________
For-Next (Loop)
Here is the easiest type of loop:
FOR a = 1 TO 10
PRINT "hi"
NEXT a
The numbers 1 to 10 can be changed to anything you want, and print "hi" can
be any other statement, or a bunch of statements if you want. What this loop
would do is print "hi" and then make 1 to 10 change to 2 to 10 and
print "hi" again and it would eventually stop when it gets to 10 to 10
another ex.
FOR a = 23 TO 50
INPUT "what is your name"; name$
PRINT "hi"; name$
NEXT a
Now what if we wanted to go backward?
FOR a = 50 TO 1 STEP -1
INPUT "what is your name"; name$
PRINT "hi"; name$
NEXT a
In this example a will start at 50, and it will be subtracted by one until it
reaches one. You can change the step to any number you want, but it must be
a subtraction sign in this case because 50 will never get to 1 by adding.
You can also do it the other way.
FOR a = 1 TO 500 STEP +5
INPUT "what is your name"; name$
PRINT "hi"; name$
NEXT a
this time instead of 1 getting 1 higher every time it would get 5 higher every
time so it would get to 500 in 100 loops instead of 500.
______________________________________________________________________________
Do While, Loop Until (loop)
here's another type of loop:
x = 3
DO WHILE x = 3
PRINT "hi"
IF INKEY$ = CHR$(27) THEN x = 4
LOOP
This time instead of going a certain number of times, it goes until a certain
condition is met. It will keep printing "hi" until they press escape and makes
x = 4 instead of 3.
here's another kind:
DO UNTIL x > 3
PRINT "hi"
x = x + 1
LOOP
This time it keeps printing "hi", and keeps making x 1 higher until x is
bigger than 3.
You can also put the until x > 3, and while x = 3 after the loop.
ex.
DO
PRINT "hi"
x = x + 1
LOOP UNTIL x > 3
This is almost the exact same thing. The only difference is where it checks
if x > 3. It usually doesn't really matter which one you use unless x must be
checked after the loop is done instead of before the next loop.
Also instead of x > 3 you can use anything.
ex.
DO UNTIL INKEY$ = CHR$(27)
LOOP
this will keep looping until the press escape.
______________________________________________________________________________
Goto
goto is another very simple word.
here's how you use it.
1 PRINT "hi"
GOTO 1
in this case the computer would never quit printing "hi" because it would go
to the number 1 and print "hi" and keep going, so this isn't a very helpful
loop, but what if we did this
1 INPUT "what is your name"; name$
IF name$ = "devin" THEN GOTO 2
IF name$ = "garner" THEN GOTO 3
CLS
GOTO 1
2 PRINT "nice name"
END
3 PRINT "cool name"
END
See how I mixed up all of our words from earlier into one little program,
here's what it would do:
ask my name
if I typed devin it would print nice name
if I typed garner it would print cool name
and if I didn't type either one it would clear the screen and ask again.
Now this isn't a great program, because it's really stupid, but you could do
something like this to make many things, like ask them what game they wanted
to play and then take them to the line number where that specific game starts.
also I showed you another word END (it's obvious isn't it.) it ends the
program. (wow!) if you don't type end at the very end of the program it will
end by itself, but if they were playing and they died you would type end and
it would end it even if it wasn't the last line in the program. Now this end
command will take them right back to the editor where you typed your code (not very good
unless you are testing to make it work) but we don't want whoever is playing
to get frieked out and be able to see your code, we want it to take them right
back to dos. Well, I'll tell you. Use SYSTEM instead of END, but in order for
it to work you must start the game right from dos. So type this at the command
line.
c:/>qbasic/run program.bas
and program will be whatever you named it. Or you can make a batch file so it
will do it for them, just like a compiled exe.
ex.
c:/>edit
this will take you to the dos editor. then you type this
@qbasic/run program.bas
and save the file as program.bat where program is the name of your normal
qbasic file. This would make it so all they have to do is type program(whate-
ver the program is called)and it would start them right in the program, and take
them back to dos when it ended.
______________________________________________________________________________
Gosub
gosub is almost exactly like a goto statement except it can go back to where
it came from.
ex.
GOSUB 1
END
1 PRINT "hi"
RETURN
what this would do is goto 1 and print hi then return would make it go back to
where it came from, but it goes one line after it, so it would goto the end
and the program would stop. No your never going to want to do this thing, but
there are tons of ways to use it
in a racing game:
if they hit a certain key goto a spot where it moves them, and then go back to
where they can press a key again.
also for your line numbers it can use names too.
like this:
name:
it must have a : at the end of the name, but it works exactly like a line
number but if you give it a name, you can describe where it takes you like
movemyguy: so you know later when your making changes what exactly is going to
be done.
also if you want to remind yourself what something is going to do you press '
ex.
IF INKEY$ <> "" THEN GOTO movemyguy ' this part will wait tell I press something, and goto movemyguy
this way qbasic won't look at whatever is after the ' so you can type anything
you want without qbasic realizing it's there.
Also when you goto a line name you don't have to have the : but you can, but
when you are making the line name you do.
ex.
goto moveme
moveme: end
-or-
goto moveme:
moveme: end
It works the same either way.
______________________________________________________________________________
While-Wend
ex.
WHILE x > 3
PRINT "hi"
x = x + 1
WEND
this is exactly like the do-while, loop-until stuff, I don't know why
they made so many, but they did
while = do while
wend = loop
see same thing, even try switching them around it does the same exact thing.
______________________________________________________________________________
Now they get a little harder
On gosub, On goto
here's how they work:
FOR a = 1 TO 3
ON a GOSUB 1, 2, 3
NEXT a
1 PRINT 1
RETURN
2 PRINT 2
RETURN
3 PRINT 3
RETURN
so the on gosub will go to line 1 if a equals 1, line 2 if it equals 2 and so
on, and you can have whatever line numbers you want because the first number
always is if a equals 1, and second is 2, and so on. and you can have any
variable it doesn't have to be a and it doesn't have to be a for-next loop.
on-goto works the same.
a = 3
ON a GOTO 1, 2, 3
1 PRINT "hi"
END
2 PRINT "hello"
END
3 PRINT "whatever"
END
It would goto 3 because a = 3 and I put end in there because if a = 1 then it
would go to line 1 but since goto doesn't return it, would go to line 2 and 3
afterward.
______________________________________________________________________________
Now it's even harder, but still not too bad
Select case
here's how it works:
a = 3
SELECT CASE a
CASE 1 PRINT "hi"
CASE 2 TO 3 PRINT "hello"
CASE 4 PRINT "how are you"
CASE IS => 5 PRINT "whatever"
END SELECT
so it would check and see if a was 1 for case 1, 2 through 3 for case 2 to 3,
and if a is bigger or equal to 5 it would go to case => 5, or you could have
> 5 or = 5 or >< 5 to say bigger than 5, or equal to 5, or different than 5.
______________________________________________________________________________
INKEY$
I wasn't going to explain this, but since I used it in a few examples today
I thought you might want to know how it worked.
ex.
b$ = INKEY$
IF b$ = CHR$(27) THEN END
IF b$ = CHR$(13) THEN PRINT "enter key"
So you use chr$(number) for any ascii number, or you could use it
like this.
b$ = INKEY$
IF b$ = "a" THEN PRINT "a"
Also if you don't know what the ascii numbers are, or what they mean just type
in this program to tell you ALMOST every ascii number. If you still can't find
a certain keys number then email me, and I will tell it to you.
There is also an ascii chart in qbasic's help section, but it's kind of hard
to understand.
1 PRINT INKEY$
GOTO 1
so all you would have to do is press a key and it would print it's number.
______________________________________________________________________________
Yes, I know it's tons of loops for you to remember, but most people
get farmiliar with just their favorites, because they all work okay. Plus it
may be awhile before I do any more chapters so you'll have plenty of time to
learn how to use them.
Don't understand this? email me
my Email: speige@hotmail.com
my URL: http://www.geocities.com/timessquare/Labyrinth/2690