So you heard a long time ago about some programming language called QBasic. This language isn't used much anymore except in its new incarnation as visual basic. If you learn QBasic, vb is quite simple. So what am I going to show you today? How to write a simple program in QBasic. First of all Basic stands for "Quick Beginner's All-purpose Symbolic Instruction Code" and was developed in the 1960's as a simple way to learn programming. This first simple program will use multiplication to find the area of a room.

QBasic is a very simple language, and for those programmers who are reading this that have never dealt with QBasic, this is an unstructured programming language meaning that you can use the GOSUB command to jump around to any place in the program at any time. Many experienced programmers do not like this feature, but when I took a class for this, it saved my life while writing programs. Also in this language there is no need to declare variables. Now I will explain the program line by line.

CLS
This command tells QBasic to clear the screen. Often times this is needed because old data from the same program doesnt automatically disappear.

INPUT "Please enter the length "; Length
This tells Basic to accept input from the user, and displays a message with the text in it. This user input is then assigned to the numeric variable "Length".

INPUT "Please enter the width "; Wdth
This is the same as the "Length" variable except one thing. If you havent noticed Ive been misspelling the variable "Wdth". This is because the word "Width is a word that is reserved.

LET Area = Length * Wdth
This is the mathematical function in the program. Once again since the variables don't have to be declared, it is easy to use "Area".

PRINT "The area is"; Area
Shows on the screen the sentence followed by the results of your calculation.

END
After you are finished you must tell QBasic that the program is finished. This is accomplished using the "END" command.

A program of a similar nature written in java, or C++ would have taken a longer to write and more code. There is a lot more to QBasic, but this should get you started on your way. I feel that QBasic still has use as a teaching aid and that it is important to learn. If you need a QBasic compiler, Microsoft has a small one known as QBasic. There isnt anywhere free to find it except inside a textbook at the public library. If there are any mistakes in my post feel free to correct them.

December 14th, 2001, 01:01 PM

proactive

About the GOSUB: The reason programmers don't use this is that it results in something I call 'spaghetti-code', it's impossible to see the structure of the code. What happens is that when you get many lines of code, you just can't figure out how things work. And then, when you have to go back and change something, you're left with no chance at all.

Good tut, though!

December 14th, 2001, 01:49 PM

oblio

QBasic is available at qbasic.com

December 14th, 2001, 03:40 PM

tH3#cL3aN3r

regards to ThePreacher,
Reminds me of learning basic on the old amstrad 464.
I still have the book for learning basic & the games you can type out yourself like BOMBER which is only about 70 lines long if memory serves me.
I'll be going home over xmas & i'll see if I can root it out,
you won't mind Preacher if I type some stuff out from it for your Basic tut.

December 14th, 2001, 07:32 PM

ThePreacher

More QBasic Commands

In addition to those QBasic commands that are shown above I decided to add that a string variable(words or letters) can be identified by the "$" sign located at the end of it. An input string statement would look like this:

INPUT "Enter your name"; Name$

Next I will go over the goto command for QBasic, its called "GOSUB". This command enables you to go to a subroutine which is similar to a function in C++, and a method in Java. I will use the previous program to demonstrate how to use the subroutine. First the complete program.

GOSUB MainRoutine
This tells QBasic to jump to the part of the program with the name that is indicated.

MainRoutine:
This tells QBasic that the subroutine begins here.

RETURN
This tells QBasic to return to the main section. Unlike other programming languages, the data from within the subroutine is automatically available for use by other routines and the main program.

December 14th, 2001, 08:49 PM

larryjs

I think thats Qbasic you're talking about there Preach, old basic used line numbering.

December 14th, 2001, 09:03 PM

ThePreacher

I went ahead and changed everything to QBasic so there would be no confusion.

December 14th, 2001, 09:28 PM

Tedob1

firstbas from powerbasic is also available at qbasic.com. its a shareware program that never expires, which is their way to make you want their power basic program.

with it you can write stand alone programs(compiles and links).
the variables x$ = string var, x% = int. var
you can make a string var equal any amount of charecters, thats a pretty cool feature.

some useful commands:

shell "some.exe" //runs a program and returns when its finished

delay(3) // pauses the program for 3 seconds

start "some.txt" //will open an existing text file with the app your system normally uses for that type of file.

//you can create a batch file and run it as part of your program:

open "batch.bat" for output as #1 'creat a file if it dosn't exist
print #1, "pause" 'writes the dos command pause
close 'closes the file so you can use it
shell "batch.bat" 'runs the batch file and waits for it to finish

create very small command line exe's

can anyone tell me how to loop through a comma delimited file?

December 14th, 2001, 11:33 PM

shkuey

Quote:

A program of a similar nature written in java, or C++ would have taken a longer to write and more code.

Six lines compared to nine, and this c++ code could be compressed further, but for the sake of simplicity I left it as so.

I'm not trying to dispute your claim of more code, for this example, but if you were going to write a more complex program, qbasic would quickly become inferior to java and c++. I'm sure you know this, but I thought I'd ******** for somebody starting out that basic isn't going to be used for anything but a learning tool.

December 15th, 2001, 05:30 AM

Tedob1

This is a tutorial on qbasic, not camparitive religion. and how many lines of code are used from the include file your not counting.

All programing languages are fun and although limited in what it can do, basic is a very high level language and therefor easy to learn.
Its a great way to see what programming it about and strengthens the creative process, which is (imho) important when your working with more advanced languages, like c++, and java.

Here's an example of bad coding, the print command is used instead of pixel positioning. A lot of spagetti code but still fairly understandable, but most important it works. i wrote the same thing using c, which i like better because you can pass arguments at the command line, but this is about basic.
I'm not really sure if this was the final version, but here goes:
==================================
color 12
CLS
BEGIN:
print" "
print" "
print" "
print" "
print" "
print" "
print" **********************
print" ***** Bug Buster *****"
print" **********************"
PRINT ""
print " What do you want to do?"
print ""
print " 1) Block a site"
print ""
print " 2) See if a site has been blocked"
print ""
print " 3) Edit hosts file"
print ""
print " 4) Changed my mind. Exit"