TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Peter Shimmin
from: Paul Edwards
date: 1996-04-04 23:50:28
subject: basic tutorial

Everything There is to Know about very basic BASIC

Version 001 released 1994.01.23

Please see EVEVR*.* for background information, freqable from 
3:711/934{at}fidonet.

Contributors:

Paul Edwards   3:711/934{at}fidonet


INTRODUCTION

This document is meant to tell you everything there is to know about
a very basic introduction to BASIC.  The target audience is people who
have NO prior programming knowledge.  ie, this document is meant to be
able to be used by 9-year-olds, and if it is too complicated for 
9-year-olds, then it needs to be modified.  ie, if you don't understand
it, it isn't your fault - it's mine.  Please send your modifications
to me so that everyone can benefit.  The idea is that after reading this,
you can start reading one of the other books readily available about
BASIC.  This document is released to the public domain.


THE DOCUMENT ITSELF

A "program" is something where you tell the computer what you want it
to do.  BASIC is a language, just like English, except that it is a
language that the computer understands.  Computers do not understand
English, as it is far too difficult for them.  So if you want to speak
to the computer, you need to use a language that it understands.  One
of these languages is BASIC, and it is the best one to learn first.

First of all you need to tell the computer that you want to talk to it
in BASIC.  The way you do this changes depending on the computer you are
using.  There is a good chance that if you type in "qbasic", that it will
work on your computer.  If it doesn't, you will need to ask someone else.

Now you can start writing a program in BASIC, the language that your
computer understands.  So type in the following:

PRINT "hello"

This is your first program.  The computer doesn't know very many words,
but one of the words that it does know is "PRINT". 
"PRINT" tells the
computer that you want it to put something on the screen.  But you also
need to tell the computer WHAT you want it to put on the screen.  Now
we want it to print the word "hello" on the screen.  But we can't just
go:

PRINT hello

We need to put the word "hello" in double-quotes.  This is just the way
the computer works.  So don't forget to put the double-quotes on whatever
it is you want to print.

Now that you have finished writing your first program, you need to run it.
Running a program basically means "Do what I just told you to do".  The
way you run your program depends on which computer you have.  You will have
to ask someone else how to run your program.

When you run your program, it should print the word "hello" on your
screen, which is exactly what you told the computer to do.

Now say you want the computer to print "goodbye" instead of
"hello".
What do you think you would do?  You just have to go and change the
word "hello" to "goodbye" in your program.  So now your
program should
look like this:

PRINT "goodbye"

Run your program, to make sure it does what you expect it to do. 
Depending on what computer you are using, you might still have the word
"hello" written on the screen, followed by "goodbye". 
This is because
some computers don't clear the screen before running your program, so it
is left over from the last one.

You can run your program as many times as you like.  Try running it again,
to make sure it still works.

Now what if you want the computer to print BOTH "hello" and
"goodbye"?
Well you can do that by telling the computer to do two things, instead of
just one.  You have one line telling the computer to print "hello", and
another telling it to print "goodbye".  This is what the program should
look like to do this:

PRINT "hello"
PRINT "goodbye"

Type in this program, and run it, to make sure it does what you expect it
to do.

Because some computers don't clear the screen before running your program,
you should always tell the computer to clear the screen first.  You can
do this by making the first line in your program "CLS" which means
"clear the screen".  So type in your program like this:

CLS
PRINT "hello"
PRINT "goodbye"

Run your program to make sure it works.  Now what do you do if you want
to print two words on the same line.  Like instead of saying "hello" you
want it to say "hello there".  Well it is not much different at all.
Here is the program to do that:

CLS
PRINT "hello there"

You should type this program in and run it, to make sure it works.

Now say you want to print your lucky number on the screen.  My lucky
number is 13.  So here is how to print that:

CLS
PRINT "13"

But there is another way of doing this.  You see, when you print something
in double-quotes, it means to print it EXACTLY like you say.  However,
the computer is smart enough that it knows about numbers.  It knows how
to count.  It even knows how to add two numbers together!  So you don't
need a calculator if you have a computer, you just need to write a program
to make your computer add up the numbers for you.  So there's no need to
put your lucky number in double-quotes.  Try typing in the following
program:

CLS
PRINT 13

If you run your program, it should print 13 on the screen, just like the
previous program.  You might wonder why the computer lets you print
numbers two different ways.  Well to see why that is the case, type in
the following program, and see what it does:

CLS
PRINT 3+4

Did this program print "3+4" on the screen?  NO!  The computer thought
that you wanted to add the two numbers up, and print the result on the
screen.  It added 3 to 4 and got 7.  It then printed 7 on the screen.
So why don't you write this program down on a piece of paper.  Next time
you can't find your calculator, you can type in this program again, and
find out the answer that way.

Now let's see what would happen if we put the double-quotes around the
calculation.  Type in the following program:

CLS
PRINT "3+4"

What happens when you run this?  Did you get "7" printed on the screen?
NO!  The double-quotes mean "print this on the screen - don't do any
calculations - don't do anything - I know what I'm doing - I know what
I want - just print it EXACTLY like I told you to".

The next thing you should know about is what a "variable" is.  Say someone
tells you a number, and says "DON'T FORGET THIS NUMBER".  It might be a
long number, like 7341.  Now you don't want to forget this number, but it
is so easy to forget it.  If you don't write it down, and the person asks
you for the number again, you might make a mistake and say "7431".  That
is why you would write it down on a piece of paper.  But the computer
doesn't have any arms, and it doesn't have any paper.  So it can't write
down any numbers to make sure it doesn't forget.

Fortunately, the computer does have a way that it can store numbers so that
it doesn't forget it later.  This is what variables are for.  A variable
is basically like a piece of paper.  You can write something on it.  A
variable is a special piece of paper though - it can only fit one number
in it.  If you want the computer to remember two numbers, you need to give
it two bits of paper.  The computer has lots of pieces of paper, so that
it can remember lots of different numbers.  

How do you tell the computer which piece of paper to use?  You need to be
able to tell the computer which one to use, so that later on you can say
"tell me what you have got written down on piece of paper number 7".  
The computer doesn't use numbers to specify which piece of paper you want.
It uses letters instead.  So there is "piece of paper A",
"piece of paper B",
and so on, up to "piece of paper Z".  And when you have used up
all of them,
there are actually a whole lot more you can use, but we'll worry about that
later.  Let's start by looking at the following program:

CLS
A = 5
PRINT A

The first line, as you know, clears the screen.  But what does the second
line say?  What it says is "I want you to remember the number 5.  Put it
in piece of paper A so that you will remember it when I ask for it".
Once you have put the number 5 into "piece of paper A", it will remember it
forever (almost).

Now the third line says "put on the screen whatever you have written down
on piece of paper A".  So it prints "5" on the screen.  Make
sure you type
this program in and run it, to make sure that what I'm saying is true!

Instead of calling these "pieces of paper" "pieces of
paper", let's call
them by their proper name, which is "variable".  So "piece
of paper A" is
actually "variable A".  Usually this is abbreviated to just
"A".  So if I
say "A is set to 5", that means "variable A is set to
5", and that means
"5 is written down on piece of paper A".  "set" means
"write down" or
"put into".

Now let's have a closer look at the 3rd line in that program.  Notice how
the program says:

PRINT A

instead of

PRINT "A".

Try changing the program the other way and see what happens.  The second
way, "A" gets printed on the screen, instead of "5". 
That is because the
second way, "A" is just like "hello".  It means
"print EXACTLY what I tell
you to print".  Whereas the first way, it means "look up variable
A and tell
me what is in there".

So basically there is three different things that you can print.  You can
print a word:

PRINT "hello"

You can print a number:

PRINT 13             OR
PRINT 3+4

You can print a variable:

PRINT A

Now let's write a program that uses 2 variables instead of 1.  

CLS
A = 5
B = 6
PRINT A

Run this program, and it should print "5" on the screen.  Now change the
program to this:

CLS
A = 5
B = 6
PRINT B

Run this program, and it should print "6" on the screen.  Notice that the
computer did not get confused because you used two variables.  The computer
NEVER gets confused.  And it NEVER forgets.  And it NEVER loses a piece of
paper or variable.  This is one of the best things about computers.  
Another good thing is that when they add up numbers, they NEVER make a
mistake!

Now remember how when we went

PRINT 3+4

it printed out 7?  Well try out this program:

CLS
A = 5
B = 6
PRINT A+B

Run this program, and it should print "11" on the screen.  That is because
when it saw the "A+B", the computer knew that what you meant was
"get the
number in variable A, then get the number in variable B, then add them
together, then print out the answer".

You might think this is a waste of time.  Why go to all that trouble, when
you could easily have written:

PRINT 5+6

and got the same answer.  Well that is a good question.  The reason is that
the other way gives us more flexibility.  We will find out about that later.

The variables A to Z only allow us to store numbers in them.  What if you
wanted the computer to remember what your name was, so that it could print
it out on the screen 10 times?  Well fortunately the computer has some more
variables that you can use to store words in.  They are called A$, B$, ...
up to Z$.  And when you've used all of them up, there are actually more
available.  So let's try out this program:

CLS
A$ = "Paul"
PRINT A$

Run this program, and it should print "Paul" on the screen. 
Change "Paul"
to whatever your name is, and run the program again.  So now you know how
to make the computer remember numbers and words.  Now let us look at 
another program:

CLS
A$ = "Paul"
A$ = "John"
PRINT A$

Will this program print "Paul" or will it print "John"
or will it print
something else?  Run the program to find out.  It prints "John".  The
reason for this is that when you tell the computer:

A$ = "John"

what that actually means is "forget what is in A$ at the moment, use a
rubber to rub it out, I've got something new to put in there, and it is
'John'".  

Now instead of having to put your name in the program all the time, you
can actually tell the computer that you want to type in your name after
the program has started.  That way other people can use your program!
Type in the following program:

CLS
PRINT "PLEASE ENTER YOUR NAME"
INPUT A$
PRINT A$
PRINT A$
PRINT A$

If you run this program, it first of all prints "PLEASE ENTER YOUR NAME"
on the screen.  The computer then gets to the line "INPUT A$".  This means
"ask the user for a word".  Some computers will print a question
mark ("?")
on the screen, to let you know that they want you to type in something.
So type in your name.  Your name is then put into variable A$ and the next
3 lines of the program tell the computer to print your name 3 times.  Run
the program a few times, changing the name of the person.

What if you would like the computer to print your name 3 times, but you want
the three names to be printed all on the one line?  Here is the same program,
with that single change:

CLS
PRINT "PLEASE ENTER YOUR NAME"
INPUT A$
PRINT A$;
PRINT A$;
PRINT A$

You will notice that the only changes that have been made are to put
a semicolon (";") on the end of lines 4 and 5.  What the semicolon means
is "after printing this variable, DON'T go to a new line".

There is an even better way of writing this same program.  Try this one:

CLS
PRINT "PLEASE ENTER YOUR NAME"
INPUT A$
PRINT A$; A$; A$

This program also prints your name 3 times.  The semicolon, as before,
just means "after printing this, don't go to a new line".

What if you want to print "Hello Paul" instead of just
"Paul"?  Well the
following program does that:

CLS
PRINT "PLEASE ENTER YOUR NAME"
INPUT A$
PRINT "Hello"; A$

First of all the computer will print "Hello".  The semicolon says to stay
on the same line, and then whatever is in A$ (which is your name) gets 
printed.  If you wanted to print "Hello Paul" 3 times, you could use the
following program:

CLS
PRINT "PLEASE ENTER YOUR NAME"
INPUT A$
PRINT "Hello"; A$
PRINT "Hello"; A$
PRINT "Hello"; A$

Now remember when we were getting the computer to add up 2 numbers, and
I said that putting the numbers in variables was more flexible?  Well this
is the reason why.  Look at the following program:

CLS
PRINT "Please enter the first number"
INPUT A
PRINT "Please enter the second number"
INPUT B
PRINT "Adding these two numbers gives"; A+B

If you run this program, you will be asked to enter a number.  Enter the
number 3, for example.  Then the computer will ask you to enter another
number.  Enter 5, for example.  The computer will then print out "8".
So now you have your very own, very useful calculator.  Whenever you want
to add up two numbers, you don't need to go and write a new program, you
can simply run this one, as many times as you want.  And the best thing
about it is that the computer never makes a mistake, and it never gets
bored!  Because the computer is so nice, it is a good idea to write lots
of programs for the computer, because if you don't, you are just wasting
such a useful friend.

By now you should have an understanding of some of the fundamental things
about programs.  Before you go any further, you should make sure you
fully understand this document.  You should also use your knowledge about
writing programs so far, to write some of your own programs.  You can
experiment with different things that you have learnt.  Then you can
move on to a more advanced book.  There is a whole world waiting out there 
for you to conquer.  Good luck.  And please, if you write some useful
programs, consider releasing them to the Public Domain.  Unless you're
planning on selling them (which you're probably not), then you can do
something useful to the whole world by letting everyone use your program
instead of keeping it to yourself.  Make sure you have an explicit
"Released to the Public Domain" notice on it, otherwise it is of no use
to anyone.
@EOT:

---
* Origin: X (3:711/934.9)

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.