Sunday, July 29, 2018

QBasic Tutorial

QBasic Tutorial 1


Download Qbasic64bit
Download Qbasic32bit

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, variables are talked about in tutorial 2, the variable 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:

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.


No comments:

Post a Comment

How to Format a Computer with Windows 7/8/10

How to Format a Computer with Windows 7/8/10 Note - Formatting hard drive information does not permanently erase the data. Even after a ...