Register The Beginners Basic Helpfile

The best choice you could make with registering the help file.  If you are really serious about programming and dont want to read books which just jump to quickly into the language, the registered help file is for you.  As you have seen over the last 8 tutorials, nothing is too mind boggling, its written in plain english(except for the grammer)for everyone to understand.

When writing The Beginners Basic Helpfile I thought of how you need to understand the language.  I can remember reading books which were just too complicated for readers to understand.  Books were just jumping to fast.  Make yourself understand from what you have read so far, think about registering.

When you register you will receive all this for just a small sum of money:

1.	All the tutorials from 1 - 16.

2.	Tutorials 1-8 will be revised editions.

3.	ASIC Shareware Basic Compiler.(Saves Download Time).

4.	Extra Source code to play around with.

5.	All missed out topics in this helpfile will be added.

6.	Email Support (When Available).

7.	Your Helpfile will be registered to YOU!

All this for just £5.00 uk, $7.75 equivalant everywhere else.  You will need to include £1.25 uk P&P and $3.00 equivalant everywhere else, but its still cheap.   All you need to do is include a postal order for the total amount above including your full details.  Please remember to include your email address if you are on the internet.  Send it to:

Steven Salmon - Registration

Gables Farm

Windmill Lane

Bursledon

Southampton

Hampshire

England

SO31 8BE.

Thank you very much.


The Beginners Basic Helpfile

The Beginners Basic Helpfile Unregistered Version

Welcome to the Beginners Basic Helpfile.  This helpfile has been written in conjunction with The Beginners Basic Homepage.  You can find this site on the internet http://www.aladdin.co.uk/u/basic. To find out more information about this site please link here.

From now on you are never going to want to refer to anything else other than this help file.  Due to the constant demand on the internet for Basic programming tutorials, I have constructed many which will be made available to you for nothing.  To kick off this opening help file heres what you will expect for this issue.  Click on a topic.

 Qbasic Tutorial Introduction

     Qbasic Getting Started

        Qbasic Tutorial 1

        Qbasic Tutorial 2

        Qbasic Tutorial 3

        Qbasic Tutorial 4

        Qbasic Tutorial 5

        Qbasic Tutorial 6

        Qbasic Tutorial 7

        Qbasic Tutorial 8

  Qbasic Frequently Asked Questions  - unavailable in Shareware Version

    Shareware Programming Reviews - unavailable in Shareware Version

 Source Code Extra - unavailable in Shareware Version

            About The Beginners Basic Homepage

                                                               Register Now!


About the Beginners Basic Homepage

The Beginners Basic Homepage was developed and created to help new commers to Basic Programming a breeze.   Within the pages were a number of ongoing tutorials for people wishing to learn the basic programming language.  The tutorials were mostly aimed at the teaching of Qbasic.  Qbasic is a interpretered language which comes shipped with MSDOS 5.0+.  More about Qbasic can be found by clicking here.

I started The Beginners Basic Homepage on November 9th 1995.  3 Months on and the pages were getting a incredible hit rate, sometimes crashed the server. My service provider was not to happy because I was not paying for the web space.  I was a little sneaky because they only allocated 1 free page to every subscriber.  I thought of a way that I could have more than 1.  Anyway I eventually got caught and they decided to put a stop to it.  They emailed me and said that you must either stop or rent web space.  The point of the site was to teach people how to program in Basic, as there were a lack of it on the internet.  I was making no money and I am still not making no money.

What I had to do is develope a way in which I can still attract my readers, but still keep the costs down.  I cant afford to rent web space when I am not making money out of my hard work.  This is how the Basic programming helpfiles got together.  What I now intend to do is transfer all my knowledge into a easy to read helpfile.  I want the helpfiles to be made available all over the internet and on every shareware cdrom that is created.

I hope you enjoy the tutorials and the helpfiles.  Remember that the helpfiles are shareware.  All I ask is that you send me £5.00pounds or $7.75dollas for my hard work.  I would like to continue to develope the tutorials.  If you register the tutorials I will send you this:

 All the tutorials and another 8 more(1-16) Revised!

 ASIC Shareware compiler v5.0

 Source code for you to use in your own programs

 The thought that you have registered

You can find more information about registering by linking here.

Regards,

Steven Salmon  - Contact me by Email at stevens@aladdin.co.uk


QBasic Tutorial Introduction

So welcome to the Introduction part of this long ongoing tutorial which hopefully will make you understand the concepts of basic programming.  If you are a intermediate "C" programmer and wish to learn basic, this would be a good place to start.  What I do advise though for new basic programmers is that you don't skip the tutorial unless you are perfectly clear on what it happening.  If there is a programming example which you know how it works, I still feel that you should type it in.  Reason for this is you need to understand programming layout more at first than you do of the heart of programming.  Layout can be a very important factor to programs, you will understand this later in the tutorial.

So the first thing you need to do is load up QBasic.  If you don't know how to it would be best to skip to the QBasic getting started topic of this helpfile.  Remember to return here though.

Once QBasic is loaded your screen should look something like a text editor.  You will observe a blue background with a window which display's relevant information about QBasic.  Press "ESCAPE" to clear this window.  You should now notice the cursor is flashing in the top left corner.

QBasic has a very helpful help system built in.  Let's take a look at what is available in Help.

Follow these next steps.

Press SHIFT first then F1

You will now see the help file.  I suggest you read the information displayed because you will find that it will be very useful.

If you are using a mouse, which is recommended, you can enter the help by clicking the leftmouse button on the word help which can be found in the titlebar at the top.  If you are using Windows 95 you might find that there is a problem with the mouse within QBasic.  Quite simply there is no pointer which means your mouse is not actually being used.  The simple way to get around this problem is this:

1. Print out this Document.

2. Exit Windows 95 to MS-DOS.

3. Load the mouse driver.

4. Load QBasic.

The problem is that Windows 95 does not allow external Mouse Drivers to be loaded.  If you are pretty fluent with how your PC works, it might be a good idea to create a Bootable disk which will load everything up in one go.  Failing that you could reset your machine.  Before "Loading Windows 95" message appears press F8, which will allow you to boot in all these different wonderful ways.

These programming examples do not really require you to use the mouse anyway.  It will just take a little longer to learn the hotkeys which will allow you to do various tasks that the mouse would of done.

Anyway back to programming.  If you still have the Help file displayed, press "ESCAPE" to clear it from your screen.  The way your screen is laid out is something which you will pickup a little later.  It is not important at the moment, but what is important is the drop down menus.  To access the drop down menus, press ALT followed by beginning letter of each menu heading.  For example to access the FILE menu we would press this:

Press ALT first then F

You can now jump different menu's by pressing the right cursor arrow, and of course to go back the other way you would press the left arrow key.  To select an item you would use the up and down cursor arrow keys.  The arrow key's are usually located to the right of your main keyboard but to the left of your mumeric keypad.

Have a browse through what options are availble, but more will be explained later.

Important Info

There are still a few things to remember when programming.  To be safe always save your work at regular intervals.  If a powercut decides to happen you know that you have not lost too much work.  It has happened to me, so don't say I have not warned you.

When saving your work make sure you call it a meaningful file name.  It's no good calling a calculator program "BERTY.BAS".  Remember that file names can only be 8 characters long.  All QBasic programs will have the .BAS extension.  If you call a file "PROGRAM", QBasic will replace it with "PROGRAM.BAS".  I will metion more important points later when they spring to mind but for now lets get our fingers dirty with some very basic QBasic programming.

The Next Topic is Getting Started with QBasic

Back to Contents Page


Getting Started with QBasic

These tutorials are designed for you to work at your own pace.  You can use any version of Qbasic and you will find that if, you have MS-DOS version 5 and above, you can use QBasic by just typing either "qbasic" or "qb" to load it up.  If you upgraded to Windows 95 from Windows 3.1 you can access QBasic straight from the Start button by going to:

START----->---Programs------>-----Applications----->----Microsoft QBasic.

Note: Please remember that if you purchased the full version of Windows 95, you will probably not have Qbasic on your system.  The best way to get around the problem is to either buy Microsofts QuickBasic or reinstall DOS into another directory on your computer.

If QBasic does not have a ICON within Windows, you will have to access the MS-DOS prompt and type either "qbasic" or "qb".

Troubleshooting

If you still can't access QBasic you can check to see if you have it by selecting the MS-DOS prompt and looking in your DOS directory.  You do this as follows.

C:>cd DOS

C:>dir q*.*

This will list all of the filename's in the DOS directory begining with "q".  If you still feel that QBasic is somewhere on your hard disk, you could try a search.  Type the following at the MS-DOS prompt.

C:>dir qbasic*.* /s

If you get no response, you could try this.

C:>dir qb*.* /s

If you still have problems then you will have to mail me with information and I will try my very best to help you out.

Shareware and Freeware Basic Interpreters

If your unlucky and don't have Microsoft's QBasic, you could always try a Shareware or Freeware Basic interpreter.  Here are some FTP sites which should contain this type of software:

ftp://ftp.cdrom.com/pub/simtel/

ftp://src.doc.ic.ac.uk/

If you still can't find any then mail me and I will try and help you out.  QBasic is a powerful basic programming language.  If you like it you could always buy the original version which is available from many software retails stores around the world.  You get far more with the original package than you do with just the bundled version which comes with MS-DOS v5 and above.  I believe that it is best to use the MS-DOS version for now.

Please remember that this Helpfile is being updated all the time, not all the tutorials are complete.  So come back after your first visit because hopefully new pages would of been added.  Time to do some programming with QBasic.

Next Topic is the QBasic Tutorial 1

Back to Contents Page


QBasic Tutorial 1

Load up QBasic and then press "ESCAPE" to clear the info dialog box.  You should see a empty blue screen with the cursor flashing in the top left corner.  The first program we are going to write will be very simple.  It will display a message on the screen.  Type the following:

PRINT "Hello World"

Once you have typed the very small program into the main editor you are ready to run the program.  In QBasic you can run a program in a number of ways.  The easiest method is by pressing F5.  Do that now.  You will find that the screen clears to black and prints the word "Hello World" in the top left corner.

Sometimes if you load QBasic from DOS you will find that the remains of the commands are left in the background.  For example C:>QBasic would be left in the background if you loaded it from DOS.  You don't need to worry about this as the CLS command will sort your problems out.  CLS command will Clear the Screen for you before doing the next operation.  For example type the following in and press F5.

CLS

PRINT "Hello World"

Once you run the program you will find that access information left behind will be cleared away and you will be left with a black screen with "Hello World" printed in the corner.  "PRINT" and "CLS" command's are available in my Reference section.

With the "PRINT" statement you can have as many as you like in a program.  For example type the following:

CLS

PRINT "Hello World"

PRINT "Hello Again"

PRINT "Goodbye!"

This will print all three lines on the screen one after the other.  So far you should of not had any error's.  If you had it might of been one of the following:

PRINT ""Hello World""

You cannot have no more than two inverted commas(") in any Print statement.

CL S

This command should read CLS not CL S.

CLS

PRINT "Hello World

Don't miss the inverted comma from the end or the beginning of the print statement.

Here is a handy tip.  Move the cursor up to the end of the "PRINT" statement.  It does not matter which "PRINT" statement.  Press F1.  A Help file will be displayed containing information of that particular statement.  Very helpful if you get stuck on a particular command.

So far you have learned two brand new commands.  Being "PRINT" and "CLS".  Now I will introduce you to two further commands.  These are called "FOR" and "NEXT".  These commands are very useful.  To clear the previous program hold down ALT and then press F.  Follow on screen instructions until you start with a fresh page.

Now type the following program exactly how it is shown below.

CLS

FOR i=1 to 10

PRINT "Hello World"

NEXT i

Press F5 to run the program and see what happens.  You will find that the word "Hello World" is printed 10 times down the screen.  Now let's break down the program and see how it works.

CLS

Clears the screen of any leftover rubbish which might be around.

FOR i=1 to 10

This is the interesting bit.  "i" is a variable which will store a value.  To start with the variable will hold the value of 1.  Everytime the "NEXT i" statement is reached the program will check to see if the variable "i" is 10.  If it is not 10, then the program will continue and jump back to where the "FOR" command was.  It is like a loop, everytime the "i" variable is not 10 it will jump back and increase the number by 1.  So if you set the "FOR" loop to 20, "NEXT" will check to see if "i" has reached 20.  For example:

FOR i=1 to 10

1,2,3,4,5,6,7,8,9,10 STOP!!!!

or

FOR i=1 to 20

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 STOP!!!!

Everytime the program loops over it will do everything contained between the "FOR" and "NEXT".  If you were to write the program like this:

CLS

FOR i=1 to 10

NEXT i

The program will still do the same thing but not do anything between the "FOR" and "NEXT".  The reason for this is because there is nothing there in the first place.  Picture it as a 10 second clock.  The clock will go 1,2,3,4,5,6,7,8,9,10 then re-start over again.  The same principle is in the above example except we are not starting over again.

In the above example the program is adding 1 to the "i" variable everytime it hits the "NEXT" statement.  We could change the program so it will add 2 to the "i" variable.  See if you can work out what the next program will do before you run it by pressing F5.

CLS

FOR i=1 to 10 STEP 2

PRINT "Hello World"

NEXT i

Above we have introduced another command called "STEP".  This command specifies how much to increase the counter on every loop from the "NEXT" command.  The "FOR" and "NEXT" loop is like a counter.  It's like you and me just counting how many fingers we have.  In the above example it will increase the counter to 2 on every loop from the "NEXT" statement.  Here's a example:

2,4,6,8,10 STOP!!!!

We understand what the "PRINT" statement does.  It will print "Hello World" 10 times down the screen unless you change any value's contained within the "FOR" statement.

The "NEXT" statement is easy and was really explained above.  Try changing the "NEXT i" statement to "NEXT" and and see what happens.  Basically you are just taking the "i" off the end of the row. If you do eliminate the "i" variable on the "NEXT" statement, the program will still work.  But what I do suggest is keep to what I explained above for now because in a large program it is nice to see what "NEXT" does what.  I will go into the "NEXT" statement in more depth later.

So the program will print "Hello World" five times on the screen because we are increasing the counter by double.  It may seem complicated to start but with like any language everything will fall into place.  Best thing to do is try changing the "STEP" value in the above example to see what kind of results you get.  Further to that try changing the "i" value and the "to" value to something else.  Have a good play that's what I say.

So in part one of this Tutorial you have learned five new commands.  These are:

PRINT,

CLS,

FOR,

NEXT,

STEP,

Later I will explain what else you can do with these commands, but try having a look at the help file.  You need to understand about variable's and string's and how they are used.  In the next chapter I will introduce you to this.

Next Topic is QBasic Tutorial 2

Back to Contents Page


QBasic Tutorial 2

Variable's are very important in all programs in some form or the other.  With variable's you can store relevant numeric information which can be operated on later.  For example you could have 5 different numbers which you may want to add together.  This can be down very easy in basic.  You had seen in Tutorial 1 a variable being used in the "FOR" and "NEXT" statements.  We gave the "i" variable a value of 1 to start with then we added 1 to it everytime we looped using the "FOR" and "NEXT" statement.  So to make things simple here is a proper description:

A variable is simply a name you give an area in memory in which a data value is stored.  When you need to retrieve that piece of data, or modify its value, you can refer to the memory location by the variable's name.  For example in a telephone program the variable age might hold the person's current age.  As the program runs, the value stored in name might change several times over a couple of years.

Naming Variables

You are free, in any program, to give variable's a meaningful, descriptive name.  When you come to write a program think about what each and every variable name should be.  In any program try to make variable name's apply to whatever the variable is storing.  For example the variable name telephone will oviously store a telephone number of some sort.  So if it is a telephone number you want to store, it's best not to call it something like eggs.  Believe me, it will make life far easier later when you come to write long programs.

Variable's come in all shapes and sizes.  Here is a common variable list:

1.  The first character must always be a letter(i.e A,G,D,F,z,e,t, or y)

2.  After the very first letter in a variable name you can have letters, digits or underscores.

3.  The final character can either be %,&,!,#,$ or nothing.  VBasic users should really        keep to the final character being the above and not leaving it out.

4.  Remember variable name's cannot be reserved words.  For example in QBasic there is a statement called "PRINT", you must not use this or any other statement name for a variable name.

In QBasic it does not matter if your variable names are either uppercase or lowercase.  For example if you type a variable name as COST and then later in the program you type the variable name as cost, you will find that the variable name will match any other in the program.  If you a program already typed in please clear it by using NEW from the file menu at the top of the screen.  Here is program that I would like you to type in exactly as show below.

CLS

cost=123456

PRINT cost

COST=789101

PRINT cost

     As you can see there are a number of things happening when you type the program into QBasic.  Firstly notice how the statements change from lowercase to uppercase.  You can see this only if you type in the program as it is displayed above.  The next thing to be noticed is how the variable name's change from lowercase to uppercase.  As mentioned before everything will change as you type in a program, but the operation will always be the same.

If you have typed in the program from above, press F5 now.  This will run the program.  The screen should clear and the information should be displayed.  What the program is doing is storing 2 different value's into the variable name "COST" or "cost"(depending on how it was writtin in.)  Firstly it will display the first value and then after changing during the program, it will display the new value.  So program's can change variable information in no time at all and as many time's as you like.

Picture a variable as a box.  You first put a number into a box, for example 1.  You then replace the number to 5.  This operation is the same as taking the number 1 out of the box and putting the number 5 into it.  You must remember that QBasic can only handle variable name lenght's upto 40 characters.  So we have now talked about numeric variable's.  Below we are going to talk about string's.

Strings

String's are something which will be talked about in QBasic tutorial 2, but for now here is a brief description.

A string is a way in basic where you can store information in a variable.  For example you could store a user's name or address in a string.  Here is a little code to show you how it works:

CLS

name1$="Freddy Bloggs"

name2$="Freddy Bloggs Sister"

PRINT name1$

PRINT name2$

This is a very simple program which stores information in a string variable.  If you typed this program in and press F5 to run, it will display what is in the variable name's line by line.  String variable name's don't have to have numeric value's in them.  In the example above I have used two variable names which have both "1" and "2" on the end.  I have just done this to seperate the different pieces of text up.  You could have this:

CLS

nameone$="Freddy Bloggs"

nametwo$="Freddy Bloggs Sister"

PRINT nameone$

PRINT nametwo$

What you must have though is the dollar sign($) on the end of the variable name.  This let's QBasic understand that it is a string variable(string only).  VBasic can handle string's and variable's in different ways, these are called Varients.  But for now it's best to stick to the easy method which QBasic can understand.  QBasic can handle upto 32,767 character's in anyone string.  The only time you might exceed this is if you are writing a text editor.  There are methods where you can get around this problem.  If I remember I will explain much later.

In the next tutorial you will learn how to take data input from the keyboard and use it in a few different way's.  So let's continue and move onto tutorial number 3.

Next Topic is the QBasic Tutorial 3

Back to Contents Page


QBasic Tutorial 3

If you read the last tutorial you would of seen briefly how to handle string's in QBasic or virtually any other platform of basic.  If you have not had a look I suggest you have a look now.

Below is a short program which allow's you to print some information up on the screen and in the meantime it allow's you to store variable information into memory.  Type the program out exactly as show and press F5 to run it.

CLS

name1$="Freddy Bloggs"

money1=500

name2$="Mr Blob"

money2=350

PRINT name1$

PRINT money1

PRINT name2$

PRINT money2

Once run you will find that the program will print all the information, which is contained in the variable's, to screen.  This is a very easy method of printing variable's.  For example you could rewrite the program to change the information stored in the variable's, but by asking the user for input from the keyboard.  We are now going to do this, so clear the program you have just entered and type the next one in.

CLS

INPUT "Your Name:"; name$

INPUT "Your Age:"; age

PRINT "Hello ";name$;" Your Age is ";age

This is a very good example of how to recieve input from a keyboard.  Firstly the program will clear the screen.  Next the program will ask a for a name to be typed from the keyboard.  Next it will ask for a age, then it will print the results out to the screen.

Let's take it a step at a time and have a closer look at the "INPUT" statement.  The "INPUT" statement allows you to read characters from the keyboard and from files.  We will be looking at files later.  The "INPUT" statement can be used in a variety of ways, here a few for now.

INPUT "Your Name:", name$

Notice that the ";" has been replaced by a ",".  This will stop a "?" sign from appearing at the end of the input statement once the program is running.  Change the program which you had typed in to the line which is shown above.

INPUT name$

The above example will not prompt you for anything and wait for the user to enter something.  Once return is pressed it will store the information in the "name$" variable.  If you try the above example notice how there is a "?" on the screen.  You can clear this by doing the following:

INPUT "", name$

This will display nothing on the screen and just wait for a input from the keyboard.  Once the return button is pressed it will move onto the next statement(if there is one).  So all you have got to remember is when a string is a string and a value is a value.  You know that a string will have a "$" on the end of the variable name, and a value variable will just have the standard name and nothing else.

So lets recap on the second program in this tutorial.

CLS

INPUT "Your Name:"; name$

INPUT "Your Age:"; age

PRINT "Hello ";name$;" Your Age is ";age

We are doing the same sort of thing in the next "INPUT" statement.  Following this is the "PRINT" statement.  In the above program we are linking the variable's together and printing the one line on the screen.  It may look complex at first but lets put it into plane English.  Dont type this next part into QBasic.

PRINT HELLO THEN A SPACE AND THEN PUT THE NAME$ AFTER IT.  AFTER THIS PRINT "YOUR AGE IS" AND THEN AGE, WHICH WAS ENTERED IN BEFORE.

It's not that difficult to be quite honest.  Now I have given you the hard start, let's make the program above do the same thing but make it look much simple.

CLS

INPUT "Your Name:"; name$

INPUT "Your Age:"; age

PRINT "Hello "+name$+" Your Age is "; age

Notice in the above example that the "PRINT" statement line has now changed.  I have replaced some of the ";" character's with "+" character's.  This make's it look more understandable and work's more effecient than before.  The snag is that using the "+" symbol is that it only works with string variable's.  This is why I could not put it on the end of the line to join up the end of text to the variable "age".  It's a shame, but the good side again is that this method is used alot more in Visual Basic.  You will see over the next comming tutorial's this method being used.

TASK 1

1.1 Write a program that will allow you to enter a person's information and print it on the screen.  The program should print the person's address on just one line but with 4 string variable's holding each part of the address.  Remember that variable string's can contain numeric characters.  The answer is at the end of this part of the Tutorial.

IF.....THEN.....ELSE Statement

In any program you write you are most sure to use the "IF" statement.  The "IF" executes a statement or statement block depending on specified conditions.  If that sound's complicated then take a look at the following piece of code.  Don't give up if things look complicated, just look over and over the code until it clicks, It will click trust me.

CLS

INPUT "What is your AGE:", age

IF age

Wow look at this.  A very interesting program which will check your input from the keyboard and then do a operation on it.  What the program is doing is checking to see if the value you entered is above or below 18.  It will then print the outcome on what the value was you entered.  Best thing to do is type it in exactly as show and then run it by pressing F5.  I have used the TAB key to make the spaces for the "PRINT" statement.  This is a very good thing to start to do.  Program layout is very important.

Before we have a look at this program in more detail, let's go over a few conditions which are quite important when writing computer programs.

            =   -------> Is Equal To

           <>  -------> Is Not Equal To

            >   -------> Is Greater Than

            <   -------> Is Less Than

            >= -------> Is Greater Than or Equal To

            <= -------> Is Less Than or Equal To

In the above example we were using the "Is Less Than" condition.  Let's put our "IF" statement into a little plain English.  All we were doing is:

If age is less than 18 then print "you are underage".  If 18 or higher print "your fine!!!!"

When the above operators are talking about "Equal", we are talking about the same or that value.  For example "IF age  18 THEN END" would mean if age is not 18 then end.  It just takes a little while to think about it, once you've done that your are well on your way with the "IF", "THEN" and "ELSE" statements.

INPUT "1 or 2? ", i%

IF i% = 1 OR i% = 2 THEN

    PRINT "OK"

ELSE

    PRINT "Out of range"

END IF

Above is a example taken from QBasic.  Observe what is happening.  Firstly the program is asking for a number.  You can either enter 1 or 2.  The program runs the "IF" statement to see if the number which was entered is 1 or 2.  If the number is 1 or 2 it will print "OK", if it's not 1 or 2 it will print "Out of range".  After this it will just do a standard "END IF" finish.

By just looking at some of the program's which have been written, you can see how easy they are doing the job.  More complicated information of the "IF", "THEN" and "ELSE" statement can be found in the QBasic Help file.  You can access this by typing "IF" and pressing "F1".

Task 1 Answer

CLS

INPUT "Name:", name$

INPUT "Address1:", address1$

INPUT "Address2:", address2$

INPUT "Address3:", address3$

INPUT "Address4:", address4$

INPUT "Telephone:", telephone$

PRINT name$

PRINT address1$ + ", " + address2$ + ", " + address3$ + ", " + address4$

PRINT telephone$

So you have learned the "IF", "ELSE", "INPUT" and briefly the "THEN" statement.  We will be looking at the "THEN" statement more in a tutorial or two soon.  In the next tutorial I will be introducing you to more new commands and another task.  Also you will learn how to use loops to make your programs do something exciting.  Keep with it and good luck.

Next Topic is the QBasic Tutorial 4

Back to Contents Page


QBasic Tutorial 4

When writing a QBasic program, your screen layout is one of the most important features of the program.  You must make the program's screen clear to read and not to have bit's and bob's that don't really need to be there.  In previous example's we have used a basic black screen with just a white colour text.  In the following example we are going to change all of this to make your programs stand out.  Type the following in exactly and press "F5" to run.

SCREEN 12

COLOR 10

PRINT "We are using a Light Green Text With a Black Background"

PRINT "Screen resolution is 640x480 with 256 Colours!"

FOR i = 1 TO 60000

NEXT i

SCREEN 9

COLOR 10, 8

PRINT "We are using a Light Green Text With a Blue Background"

PRINT "Screen resolution is 640x350 with 64 Colours!"

So from the above code example I have introduced two new commands.  These commands are "SCREEN" and "COLOR".  Let's have a closer look at the "SCREEN" statement.

QBasic can handle many different screen modes.  When I say screen modes I am talking about different sizes and colours.  Here is a run down of different screen modes in QBasic.

SCREEN 0: Text mode only

SCREEN 1: 320 x 200 graphics

SCREEN 2: 640 x 200 graphics

SCREEN 4: 640 x 480 graphics

SCREEN 7: 320 x 200 graphics

SCREEN 8: 640 x 200 graphics

SCREEN 9: 640 x 350 graphics

SCREEN 10: 640 x 350 graphics, monochrome monitor only

SCREEN 11: 640 x 480 graphics

SCREEN 12: 640 x 480 graphics

SCREEN 13: 320 x 200 graphics

The screen modes above are available to all QBasic users.  Screen modes are very important to programs.  For example if you wanted to draw lines, boxes and circle's on the screen with any kind of graphics method you would require a screen mode which allow's you to draw graphics.  Most of the screen modes above works with fairly recent PC's.  A few years ago some modes would not work because you would need a certain graphics adaptor. Thats why recent PC's are designed to handle older types of screen modes such as CGA,EGA,VGA and now SVGA.

O.k so what benefit do you get from different screen modes.  Well, some screen modes above allow you to use more colours than others.  In the first program in this tutorial, I gave you a program which will use two screen modes.  One has a maxium of 256 colours and the other only allow's you to use 64 colours.  When writing a program think about how many colours you are really going to need and at what resoultion are you going to have the display screen.  640 x 480 is a very nice resolution which is screen mode 11.  Try changing the screen modes in the first tutorial program and see what you get.  Remember that some screen modes may not work, so don't worry about this.  For more information on screen modes, type "SCREEN" into QBasic and press F1.

Using Colours with Screen's

So the first program used two different screen modes and some different sets of colours.  Let's look closely at the "COLOR" command.

Firstly the "COLOR" statement allows the user to select specific background and forground colours for the program.  It even allows you to select a border colour.  The border is the area which surrounds the screen.  The "COLOR" statement can be placed anywhere in a program just like any of the commands mentioned so far.  At the end of this chapter there is a brief description of the commands you have learnt so far.  So to put the command in pratice let's write a interesting program.  I am not going to tell you what it does yet, so for now, just type it in and run it by pressing "F5".

SCREEN 12

CLS

FOR i = 1 TO 15

COLOR i

PRINT "Testing Testing 1.2.3"

NEXT i

So here we a go, a simple program.  Can you guess what it is going to do from what we have learnt so far?  Well let's take it command by command to see how it operates.

SCREEN 12

We are now going to select Screen Mode 12.  This will allow us to use, from a pallete of 256 colours, 16 colours.  More information in the QBasic Help file.

CLS

We know that this will clear any garble left behind on the screen from before.

FOR i = 1 TO 15

We are setting the variable "i" a intital value of 1, which will be incremeted(increased)by 1 later.

COLOR i

We are giving the "COLOR" statement a starting value of 1 which came from the "i" variable in the "FOR" statement.

PRINT "Testing Testing 1.2.3"

We are now to print the words above on the screen.

NEXT i

When the program runs it will repeat the "FOR" and "NEXT" loop until the "i" variable reaches 15, which is defined in the "FOR" statement above.

So when the program runs it will display 15 pieces of text which are each a different colour.  Notice how there are only 15 pieces of text when we should be displaying all 16 which we have got available.  To do this we should change the "FOR" statement to this.

FOR i = 0 TO 15

Note how we changed the value of "i" to zero first.  This is because the colour 0 is black.  If we changed it from 1 TO 16 we would get a error because the "COLOR" statement in screen mode 12 can only handle 16 colours at anyone time.  Have a little play with the "FOR" and "NEXT" loop.

This is how the "COLOR" statement works.

COLOR [foreground][,[background][,border]] --- Screen mode   0

COLOR [background][,palette]                      --- Screen mode   1

COLOR [foreground][,background]                --- Screen modes  7-10

COLOR [foreground]                                     --- Screen modes 11-13

As you can see from the first program in this tutorial, we can define not just the foreground text colour but also the background colour.  The "COLOR" statement works quite different in other screen modes.  To see what does work and what does not work, look at the help file under "COLOR". This will be a ideal reference for you to use, when selecting colours for various parts of your programs.  The "COLOR" statement is explained in more detail in a couple of tutorials from now.  Oh! Also the "COLOR" statement is not used by all Basic programming languages.  You will need to check this up by looking at your user guide.

Task 1.2

Write a colourfull input program which will ask for a user's name and age.  When you output it to the screen make sure the writing is of a different colour.  Hint: Use screen mode 12.  The answer is at the end of the tutorial.

Review of Commands Learnt

PRINT

FOR

NEXT

CLS

SCREEN

COLOR

INPUT

STEP

IF

ELSE

THEN

ENDIF

Quite a few of these commands will be mentioned in more detail later in tutorial, but you are not

doing to bad.  The main thing is experiment with what you have learnt, and you will pick it up nice and easy.

Task 1.2 Answer

SCREEN 12

CLS

COLOR 3

INPUT "Name:", name$

COLOR 5

INPUT "Age:", age

COLOR 9

PRINT "Hello "; name$

COLOR 10

PRINT "You are"; age

The code is nice and simple and gives a good result.  You program should look something like this, but your colours will probarly be different than mine.  Try typing my program in and see if you get the same result as me.  If you are close then well done, if not then don't worry!

So you have now learned a little about screen modes and colors in QBasic.  Remember the screen mode and any colours can be set at anytime in a QBasic program.  Remember that you don't need to keep using the "CLS" command all the time, the screen should clear automatically when a new screen mode is selected.  Keep going and well done.

In the next chapter we are going to learn about screen layout and introduce you to some very useful command's such as "LOCATE" and "DATE$".  Also you will learn how to make the computer respond back to you by using a useful warning command such as "BEEP".  The "IF" and "ELSE" statement is mentioned and used in more detail than so far. See ya soon!

Next Topic is the QBasic Tutorial 5

Back to Contents Page


QBasic Tutorial 5

When you write a program in QBasic it is nice to place your text somewhere on the screen.  Within previous examples so far we have just had the text printed in the top left side of the screen.  We can use a specific command which will place text anywhere on the screen, this command is called "LOCATE".  Below is a simple syntax.

LOCATE row, column, cursor

row

The number of a row on the screen; row is a numeric expression

and must be a integer value. If a row value is not specified,

then the line (row) does not change.

column

The number of a column on the screen; column is a numeric

expression and must be a integer value. If a column value is not

specified, then the column location does not change.

cursor

A value indicating whether the cursor is visible

or not. A value of 0 (zero) indicates cursor off; a value

of 1 indicates cursor on.

Before we go any further let me just briefly explain what a integer is.  A interger is a whole number ranging -32,768 to +32,767.  This means that a value of 21566 is a integer, and a value of 2343992 is a long integer.  Please refer to the datatypes section for more details on this.  But from now on integer variables are going to be used like below in example's in this tutorial.

money%=599

O.k here is a little code to show you how "LOCATE" command works.

SCREEN 12

CLS

LOCATE 5,10

PRINT "Hello World we are at 5,10"

LOCATE 10,15

PRINT "Hello World we are at 10,15"

If you type this program in and press "F5" you will find that the words "Hello World we are at y,x" are placed in various locations on the screen.  The y,x in the last sentence is whatever is placed into the program oringinally.  Try changing the "LOCATE" statements in the above program to something different.  Press "F5" to run.  Here is a chart to show how the row,column cordinates work.

|

|

|

|

row

|

|

|

|

|--------column-------

The first "LOCATE" statement in the program we just typed in moved the following "PRINT" statement 5 lines(rows)down and 10 Characters(columns)across.  What you must remember is that whatever "PRINT" statement follows the "LOCATE" it will only do the operation on the following statement.

The locate statement does not have to have a value representing the row or column all the time.  For example we could have:

LOCATE 5

This will simply move the "PRINT" statements output to 5 rows down the screen.  We can also have this:

LOCATE , 5

This will simply move the "PRINT" statements output to 5 column across the screen.  So again you dont have to specify the arguments all the time.  Arguments are the information which is required for a statement to work.  For example:

LOCATE argument1, argument2

In which case argument1 would be row and argument2 would be column.  Have a little play with the "LOCATE" command and see what you come up with.

Sounding a BEEP!!!

Within QBasic you can make your computer do a simple sound effect which might represent a warning or a showing of information.  This command is called "BEEP".  Its very very easy to use.  To try it enter the following 1 line program and press "F5" to run.

BEEP

Simple!.  It will generate a high pitch sound.  You can place the "BEEP" command anywhere within a program.  In the following program the "BEEP" command is used together with some other bits you have learnt so far.  Type the following in and press "F5" to run.  Remember to delete the program which you typed in last.

SCREEN 12

CLS

checkpass$ = "fred"

LOCATE 5, 5

INPUT "Please Enter Password:", password$

IF password$ = checkpass$ THEN

        CLS

        LOCATE 5, 5

        PRINT "Login Correct"

ELSE

        CLS

        LOCATE 5, 5

        PRINT "Login Incorrect!!!"

        BEEP

END IF

The program above may look a little complicated at first but really it's very easy.  Firstly the program will change screen mode to 12.  Following this it will clear the screen to get rid of any bits left behind by other programs.  The next statement will tell the variable "checkpass$" to have the word "fred" stored within it.  We then setup the screen to ask for a password.  Once the password has been entered it will store the information within the variable called "password$".  The "IF" statement following will check to see if the variable "password$" is the same as the variable "checkpass$", which if it is then it will print "Login Correct" upon the screen.  If the password is not correct it will jump to the "ELSE" statement and print "Login Incorrect!!!". Also a loud beep will come from the computer's internal speaker.

The we go a simple inputting and checking program.  You can also check for invalid numbers.  Here is a program which allow's you to do this.

SCREEN 12

CLS

checknumber% = 50

LOCATE 5, 5

INPUT "Please Enter a number(0-100):", number%

IF number% >= checknumber% THEN

        CLS

        LOCATE 5, 5

        PRINT "50 or Above!!!"

ELSE

        CLS

        LOCATE 5, 5

        PRINT "49 or Below"

END IF

 Again another simple program which checks to see if a number entered is higher or lower than the one which is stored into the variable called "checknumber%".  Firstly the "checknumber%" variable is holding the value of 50, which is checked by the "IF" and "ELSE" statement, just like the password program before.  The "IF" statement is checking to see if the value you typed in is greater than or equal to the value stored in the variable called "checknumber%".  For example if you typed in 49 you would get a result of "49 or Below" printed on the screen.  However if you type 50 or more into the program you will get a result of "50 or Above!!!" printed on the screen.

Date and Time

QBasic allows the user to find out what the system date and time is.  To do this you can use two simple commands such as "DATE$" and "TIME$".  You can also set the time and date from within a QBasic program, but this will be explained much later.

Type the following program in and press "F5" to run.  Remember to clear the previous program from within memory.

CLS

PRINT "Todays Date is:", DATE$

PRINT "Todays Time is:", TIME$

Press "F5" to run and you should see the date and time displayed on your screen.  There is not much else I can tell you about the "DATE$" and "TIME$" command until later.  You could try writing a program which will display the date and time at the top of the screen and ask a user for a password.

You have now learnt a further 4 QBasic commands.  Remember that you can get online help by just pressing the "F1" by any command on the screen.  In the next chapter I will introduce some basic looping techniques which will continue a operation until something happens such as a user pressing the keyboard or until something makes the loop stop.  Thanks for reading!

Next Topic is the QBasic Tutorial 6

Back to Contents Page


QBasic Tutorial 6

Within QBasic you can create a program which will do something for either a period of time or until a user makes the computer do something else.  In this part of the tutorial I am going to introduce a few techniques which allow you to do this.  We have already looked at one kind of loop which is the "FOR" and "NEXT" loop.  This loop operates in a completely different way for previous examples.

I am now going to introduce you to the "DO" and "LOOP" loop commands.  Here is a Basic program which will just loop over and over again until the user stops it by pressing the keys CTRL & BREAK.  Type it and run it by pressing "F5".

CLS

DO

PRINT "HELLO WORLD"

LOOP

From this example you can see the words "HELLO WORLD" print forever down the screen.  It might not be clear at this point, but the program is actually do something.  You can ofter notice the flicker(if any) at the bottom of the screen.

As you can see the "DO" and "LOOP" commands are quite effective even at this point.  Let's break the commands down and see what they actually do.

DO

This command with set the start of the loop which executed everything in between the "DO" and the "LOOP" commands.

LOOP

The "LOOP" command is executed only if "DO" is present, such as the previous examples.  Everytime a "DO" command is in your program it expects to find a "LOOP" command.

There is more to "DO" and "LOOP" than just looping forever.  Let's build onto the commands by getting some kind of response back from the computer.  I am now going to introduce you to a further command called "WHILE".  This command is quite effective but sometimes complicated to understand.  Here is a simple example which need to be typed in and run.

CLS

DO WHILE I < 10

    I = I + 1

    PRINT I

LOOP

The program may look complicated but really it is not.  When you run the program it will simply print 1 to 10 down the screen.  The program is actually checking to see if the variable I = 10 and if it does then the program will finish the loop.

Firstly we are starting the loop and saying while I is not 10 then print the value to screen.  In which case the program will jump straight back to the beginning and check the variable to see if it has reached 10.  It is simple, but make sure you follow it through.  Try changing the first line to a value of 20 and see what it does?  The line should now read:

DO WHILE I < 20

Strange may it seem but if you now run the program it will print 1 to 20 down the screen.  To take things a step futher we can now give you a choice on where to put the "WHILE" command.  Before I give you another programming example let me just briefly explain a little more about the "WHILE" command.

WHILE

There is only a little which I can tell you about the "WHILE" command because it is best explained in co-operation with another command called "WEND".  The "WHILE" command allows you to check a condition of something to give results.  As show in a previous example it is classed as a checking command.

The "WHILE" command can be placed elsewhere in the programming example I gave you before and give the same results.  Here is the code which does the same as before.

CLS

DO

    I=I+1

    PRINT I

LOOP WHILE I < 10

So as you can see from the program above, it will give the same result if you run it by pressing "F5".  So the "DO" and "LOOP" loop give's you some kind of flexibility when deciding when to place code into a program.  This allows more control over program layout and design, keep this in mind.  Loop's are explained and user in more detail later in the tutorials.

Understanding User Input

User input is a vital part of any program.  For example I have already covered how to handle variables and strings.  Also I have covered how to use the "PRINT" command in a little detail.  We are now going to put our built up knowledge into pratice and write a more complex program.  Don't worry if the program look's a little complicated and clever, it is easy, just follow it through bit by bit and everything will come clear.

CLS

        Password$ = "APPLE"

DO

        INPUT "What is your Password:", Pass$

        IF Pass$ = Password$ THEN Enter = 1

LOOP WHILE Enter  1

CLS

PRINT "You have Entered!"

After you type in the program and press "F5" the screen will clear and ask you to enter a password.  If you enter anything but "APPLE" it will ask you again and again until it have been entered correctly.  Remember that the program is checking to see if you are typing the program in lowercase and uppercase.  This program will only except the word apple to be written "APPLE", not "appple".  Later in the tutorials I will explain how you change words that are in lowercase to uppercase without pressing the CAPS LOCK or SHIFT keys.

A Easy look at WHILE

Below is the source code for a more simple program which will demostrate the "WHILE" command.  Type the following into QBasic and press "F5" to run.

DO

    I = I + 1

    PRINT I

LOOP WHILE I = 1000

The above program makes it more clear to understand the "DO LOOP WHILE" commands.  If you type the program in a run it, it will count from 1 to 1000.  This is easy to understand if you look at how the program is doing it.  Firstly we are telling the computer to start a loop by using the command "DO".  Next we are asking the computer to start a variable called "I".  The next statement will print the value of I onto the screen, in which case the first number would be 0.  The computer has started the variable at a value of 0.  The next part of the program will check to see if the value in the "I" variable is 1000, if not it will jump back to the start of loop, in which case it is the "DO" statement.  The computer will then tell the variable to add one to the previous value of the "I" variable, in this case it would make "I" = 1 then 2 then 3 etc.

With the above program you could change the "I = I + 1" to "I = I + 2".  This would cause the computer to add 2 to the variable "I" until the computer reaches the final statement which is the "LOOP WHILE" for checking to see if "I" has reached 1000.

CLS

        oldname$ = "FRED"

DO

        INPUT "What is your name:", name$

        IF name$ = oldname$ THEN entrance = 1

LOOP WHILE entrance  1

Again above we are using a similar kind of program, like the password program, which allow's user input to check a variable against each other.  The above program will ask you to enter a name.  If the information you typed in is the same as what is contained in the "oldname$" variable then program will exit, if nothing is the same then the program will continue.

Have a little pratice at using variable's with the "DO LOOP" kind of loops and see what you can come up with.  In this chapter you have learnt the basic's of loop control with data input from the keyboard.  In the next chapter I am going to introduce a simple menu program which uses a few new commands such as the "SELECT", "CASE" and "IS" commands.  I shall warn you now that the next chapter might loose you a little, but hey it's part of the learning process.  If you do get lost then please don't give up, just read through the chapter again until everything becomes clear.  Very soon I shall be writing a program which uses virtually every command you have learnt so far.  This program will be ideal for reference and just give you a general feel of how the commands put together can make some good programs.

Until the next chapter keep going.

Next Topic is the QBasic Tutorial 7

Back to Main Menu


QBasic Tutorial 7

In this chapter we are going to write a simple menu program which can be used in any of your

own programs by just cutting and pasting in.  You will need to change a few things, such as the

text which is written on the screen.  Before I go any further here is the code which is needed to be typed in.

CLS

SCREEN 12

        COLOR 12

                LOCATE 3, 35

                PRINT "Main User Menu"

                LOCATE 4, 35

                PRINT "--------------"

        COLOR 10

                LOCATE 10, 35

                PRINT "1. Add"

                LOCATE 12, 35

                PRINT "2. Subtract"

                LOCATE 14, 35

                PRINT "3. Divide"

                LOCATE 16, 35

                PRINT "4. Multiply"

        COLOR 6

                LOCATE 25, 32

                INPUT "Enter Number (1-4):", number

SELECT CASE number

        CASE 1

        PRINT "You Selected number 1"

        CASE 2

        PRINT "You Selected number 2"

        CASE 3

        PRINT "You Selected number 3"

        CASE 4

        PRINT "You Selected number 4"

        CASE ELSE

        PRINT "Number typed was not 1 to 4"

END SELECT

Oh Yes, things are really starting to heat up.  Just take a minute and look through the code.  As you can see there is quite alot which you have learnt so far.  The only bit you have not learnt is this:

SELECT CASE number

        CASE 1

        PRINT "You Selected number 1"

        CASE 2

        PRINT "You Selected number 2"

        CASE 3

        PRINT "You Selected number 3"

        CASE 4

        PRINT "You Selected number 4"

        CASE ELSE

        PRINT "Number typed was not 1 to 4"

END SELECT

Trust me, this bit is very easy.  Firstly lets give you a breif run down of what the "SELECT

CASE" statments mean.

The "SELECT CASE" statement allows you to execute 1 of many statement blocks.  For example a statement block is this:

CASE 2

        PRINT "You Selected number 2"

In the above main program we have got 5 statement blocks which are "CASE 1", "CASE 2", "CASE 3", "CASE 4" and "CASE ELSE".  Within the "SELECT CASE" part of the program above, the first line said:

SELECT CASE number

This command is asking to execute a statement block on what is held in the variable called "number".  If you look just above this part of the program you will see we have a "INPUT" command which is asking the user for a number.  The "SELECT CASE number" command will use whatever statement block is asked for.  So if we type in "2", within the program, the "SELECT CASE" statement will use "CASE 2", and then execute whatever is contained in the program after that, and before the word's "CASE 3" have appeared.

The "CASE ELSE" command is used just incase the user types anything other than 1 to 4.  IF the "SELECT CASE number" command can't find a case on what the user has typed in, it will refer to the "CASE ELSE" statement to give a error message or do whatever the programmer has asked it to.

Advanced "SELECT CASE"

Let's move onto something which is a little more advanced than the first "SELECT CASE" program.  Within the next program we are going to use a command called "TO".  You will see what it does later, but first type in the program which is shown below and run it by pressing "F5".

CLS

        SCREEN 12

        COLOR 12

                LOCATE 3, 35

                PRINT "Main User Menu"

                LOCATE 4, 35

                PRINT "--------------"

        COLOR 10

                LOCATE 10, 35

                PRINT "1-2. Add"

                LOCATE 12, 35

                PRINT "3-4. Subtract"

                LOCATE 14, 35

                PRINT "5-6. Divide"

                LOCATE 16, 35

                PRINT "7-8. Multiply"

        COLOR 6

                LOCATE 25, 32

                INPUT "Enter Number (1-8):", number

SELECT CASE number

        CASE 1 TO 2

        PRINT "You Selected number 1 or 2"

        CASE 3 TO 4

        PRINT "You Selected number 3 or 4"

        CASE 5 TO 6

        PRINT "You Selected number 5 or 6"

        CASE 7 TO 8

        PRINT "You Selected number 7 or 8"

        CASE ELSE

        PRINT "Number typed was not 1 to 8"

END SELECT

At first glance it may not look any different than the first program, but notice the "TO" statement within the "SELECT CASE", "END SELECT" part of the program.  The "TO" statement allows you to set a range of something within either a "SELECT CASE" statement or even the "FOR" and "NEXT" loops.  Remember that you have already used "TO" within the "FOR" and "NEXT" statements in previous chapters.

The actual program will allow you to respond to not just 1 number but to 2 numbers within a range.  So to execute the 4th "CASE" statement you would need to type 7 or 8 within the program when it is running.

This command is down to common sense and can be understood if you think about what the program is doing.  In the next chapter I am going to introduce a way in which to handle numbers within a program.  I have briefly explained the usage of

            =   ------->  Is Equal To

            <>  -------> Is Not Equal To

            >   ------->  Is Greater Than

            <   ------->  Is Less Than

            >=  -------> Is Greater Than or Equal To

            <=  -------> Is Less Than or Equal To

in previous tutorials.  We are going to look at this a little more in depth.  Thanks for reading so far.

Next Topic is the QBasic Tutorial 8

Back to Contents Page


QBasic Tutorial 8

Using numbers in QBasic is probably one of the most important things you will need to know when writing a QBasic program.  For example if you are writing a game you would probably need to store the players score and any bonuses he/she might get.  A simple example of adding a bonus score onto the main score would probably be this.

bonus = 100

score = 500

score = score - bonus

PRINT score

 The program above tells QBasic to store the value of 100 into the "bonus" variable and 500 into the "score" variable.  The following line will add both "score" and the "bonus" value's together and assign a new value to the "score" variable.  This example just shows a way you can add to variable values to equal a new result.

You could make things just a little more complicated.  What if you have 4 variable values that you wanted adding together?  Well you could do something like the following.

bonus = 100

score = 500

levelbonus = 200

luckscore = 100

score = score - bonus - levelbonus - luckscore

PRINT score

Remember that you can add variable numbers which ever way but just making sure that they are not string's you are adding together just yet.  The following examples shows other ways which numbers could be handled in any program.

age = 21

new = 3

age = age - new

PRINT age

The above program is using the "-" operator to do the required job.  In our language it is called subtraction.  The next program allows you to see how working on numbers can effect the final result in a program.

CLS

        apples = 2

        plums = 5

        oranges = 3

LOCATE 2, 1

PRINT "In Stock we have: "; apples; " Apples, "; plums; " Plums, "; oranges; " Oranges"

LOCATE 6, 8

PRINT "We have now sold 2 Plums and 1 Orange"

        plums = plums - 2

        oranges = oranges - 1

LOCATE 10, 1

PRINT "In Stock we have: "; apples; " Apples, "; plums; " Plums, "; oranges; " Oranges"

 I have already explained briefly in previous tutorials how to use some of these operators.

            =   ------->  Is Equal To

            <>  -------> Is Not Equal To

            >   ------->  Is Greater Than

            <   ------->  Is Less Than

            >=  -------> Is Greater Than or Equal To

            <=  -------> Is Less Than or Equal To

During any long program you will probably always use one or more of the above.  With the above operators you can do operations on different numbers.  For example you could check to see if something is the same or higher than.  I don't know if you know but in the UK there is a tv show called "Play your Cards Right".  In the game show they have to say if the next playing card is higher than the proceding one.  For example say the first card is 6.  The contestent has to say if the next card is higher or lower than the next one.  If they say higher then 7 upto a ACE would win on that occasion.  If 5 or lower came up after the contender had said higher then they lose.  Games like this would be easy to write, and you would use many of the above to simulate this.  Below is a simple guessing game.

CLS

RANDOMIZE TIMER

        number = INT(RND * 10)

DO

        INPUT "Guess the number: ", guess

        IF guess > number THEN PRINT "To High"

        IF guess < number THEN PRINT "To Low"

LOOP UNTIL guess = number

PRINT "You Win"

Oh yes, sticking you in it here.  I have now just introduced you to two new commands.  The first one is "RANDOMIZE TIMER" and the other is "INT(RND)".  Don't worry it may look a little complicated but actually it is very easy.  Let me explain the two commands to you.

"RANDOMIZE TIMER" is a way of generating really random numbers.  "RANDOMIZE" on it's own would allow you to juggle numbers up, but when you place "TIMER" into the command then it juggles them much more.  You probarly will not use "RANDOMIZE" much unless you are writing a number game or something of that nature.

However "INT(RND)" is probably something you will use alot.  The "INT" part explains that it is looking for a integer.  I have already explain integer's before in the tutorial.  To briefly explain more, a integer is a whole number and not some decimal place number like 4.3454.  Integer's in variable's are sometimes a good thing because they make sure you are getting a whole number.  If you were to obmit the "INT(" part of the program above you will find that the "RND * 10" statement will generate some decimal value between 1 and 10, like 5.3.  This is where it brings me to my next point.  The "(RND * 10)" part will generate a random number between 0 and 10 for me.  If you were to put "(RND * 20)" you will get a random number between 0 and 20 generated for you.  Remember that the generate random number will be placed in the number variable in the program above.

The number checking routine is this part.

        IF guess > number THEN PRINT "To High"

        IF guess < number THEN PRINT "To Low"

The first line of this is checking to see if the guess from the user is higher than the random generated number.  If it is too high then the program will print "To High".  The next line of the above is the complete reverse.  It is checking to see if the guess from the user is lower than the random generated number.

In the next couple of tutorials you will see many of the operators in use, especially some which have not been explained yet.  In the next tutorial I will be introducing you to some graphic commands.  Also you will learn how to snazz up your own programs to look appealing to the user.  Thanks for reading so far.

Next Section is Registration

Back to Contents Page


QBasic Source Code