>
 
QBasic Tutorial 1
QBASIC TUTORIAL

Introduction:

Welcome to Basic QBasic, a grouping of lessons on how to use the QBasic programming language. First, you may be wondering what QBasic exactly is.  Well, there is a simple answer - QBasic is a programming language written for computers back in 1975, by Bill Gates and Paul Allen, and has been the accepted standard ever since.  Why?  Because of its ease of use, its English-like commands, and its power.  

QBasic stands for Beginner's All-Purpose Symbolic Instruction Code.  Several of its commands are pure English (e.g., PRINT, INPUT, LET,etc.).  It has a simple structure for its programs, and its lines are executed in order.  But why should you use QBasic?  What's in it for you?  Many things are in it for you.  When you learn QBasic, you also learn many of the fundamentals of other programming languages (e.g., HTML, Hyper Logo, C, Java, etc.).  You also can create programs easily.  Once you get into it, you'll find that the fun in creating programs is worth coming back to.   By the end of this unit, you should know many things about the QBasic programming language, and you will be able to write excellent programs that are useful to you and your friends.

--Using QBasic--

QBasic is very easy to use.  To the run the program, click on start, programs, room233 where you will find the QBasic icon to click on. You can also download a copy of the program to your computer at home from my web site (chatt.hdsb.ca/~asada). Unzip the file at home in a folder, you may call it, QB45 on your hard drive, and once your are done, run the program by double clicking on the application file QB.exe.

        The general layout of the Qbasic screen is shown below:
9803_113436_0.png
Immediate Mode


Before we get right into the programming stuff, it is important to understand how calculations are performed, and how the PRINT statement can be used to display output on the computer screen.  The Immediate Window in QBASIC allows the programmer to operate the computer much like a calculator by performing individual calculations.  Advanced programmers often use the Immediate Window to test segments of code, or variable values, before incorporating them into their full scale program.

Exercise 1:
Type each of the following lines of code, one at a time, into the immediate window, remembering to hit the ENTER key after each line. In your notebook, record EXACTLY what appears on the screen:

PRINT 75-38
PRINT 8*9
PRINT 85/4
PRINT 5*6+2
PRINT 5*(6+2)
PRINT –5*(6+2)
PRINT 11-7
PRINT 3^2
PRINT “Nelson”
PRINT “NELSON”
PRINT “N”;”E”;”L”;”S”;”O”;”N”
PRINT “N”,”E”,”L”,”S”,”O”,”N”   REM why is the output different for this than the previous statement?
PRINT “5+2”
PRINT “5+2=”; 5+2
PRINT “NELSON”;”HIGH SCHOOL”
PRINT “NELSON”,”HIGH SCHOOL”
PRINT "NELSON ";
PRINT "HIGH SCHOOL"             REM ensure that you understand the reasons for the differences in the last 3 statements

An Important Note: Upon completion of this exercise, you should be able to identify what operations each of the following symbols perform in a QBASIC line of program code.
        e.g.,  ( )  ^  /  *  +  -  “  ,  ;  :  

Exercise 2:
Each of the following lines of code contains an error. Try to predict what the error is prior to typing it into the immediate window, and then write the correct code segment in your notebook.

PRINT (5+7) (4+6)
PRINT 5(6+3)
PRINT "NELSON’
PRINT ‘RULES’
PRINT 5*(5+7))

Now that you have had some practice working with the computer in immediate mode, the remaining sections will deal with writing an actual program in the PROGRAM area.

Program Mode

In this section we will cover the easiest statements QBasic can offer. These statements include: PRINT, CLS, END, SLEEP, COLOR, SCREEN, and LOCATE. To get started load Qbasic and get prepared to write your first program. Unlike Immediate Mode where you were restricted to entering single lines of code and executing them immediately on the screen, in Program Mode you will enter several lines of code (your program) and then execute them in sequence.

Ok let's get to our first program (type the following line into the Program Area Window):

PRINT "Hi people!!"

Guess what. You just made your first program. Not much happened though. Press Shift-F5 and the program will run. Just to prove a point, run your program again. You should have noticed that it PRINTed Hi people!!, twice on the screen. You may also have noticed a bunch of other text and junk cluttering up your screen. To clean up your output screen, you need to clear the screen. That's where the CLS statement comes in. Try editing your program by adding this:

CLS
PRINT "Hi people!!"

The next statement we will learn is END. I think you can probably figure out what it does. If not, it ENDs your program. Most programming languages should have a statement that marks the physical end, that is, the last line of a program, in HTML we use the </HTML> tag to mark the end of our code. In QBASIC we use the END statement to do the same thing.  Let's take the above program and add END:

CLS
PRINT "Hi people!!!"
END

Now run this program to see the results on the output screen.

SLEEP statement:
Now we're going to learn the SLEEP statement. This statement is rather easy. When you use SLEEP, the computer will pause for a particular number of seconds (as specified by the programmer) then continue with the program. SLEEP with no number, will hold until a key is hit. Modify your existing program to reflect these new changes, then run your program to determine what the changes do:

CLS
PRINT "Hi people!!!"
SLEEP 1
PRINT "Bye people!!!"
END

Bonus Challenge:
Modify this program so that the Hi people/Bye people message appears to flash/blink on the screen.

COLOR statement:
Now we will learn about the COLOR command (note the Americanized spelling of this statement…just like HTML code). This statement is self-explanatory, it allows the programmer to enhance their screen output by adding COLOR to the screen text. Each of the following numbers, when used with the COLOR statement, will display the corresponding color on the screen.

The COLOR list is as follows:
0 = Black
1 = Blue
2 = Green
3 = Cyan
4 = Red
5 = Purple
6 = Brown
7 = Grey
8 = Dark Grey
9 = Light Blue
10 = Light Green
11 = Light Cyan
12 = Pink
13 = Light Purple
14 = Yellow
15 = White

These are all the colors for certain SCREENs (QBASIC supports several different graphic/screen modes). Different screens also support different numbers of colours. Try using color in the program we've made so far by changing your existing program code to this:

CLS
PRINT "Hi people!!"
COLOR 1
PRINT "Hi people!!"
END



SCREEN statement:
As already stated, there is also a QBASIC statement called: SCREEN.  This statement allows the programmer to take advantage of the different resolutions and color types available in the QBASIC programming environment. The different SCREENs are:

SCREEN 0 = No graphics add 16 to any color to make it flash. (this mode also supports a color background option
SCREEN 7 = 320 x 200 resolution. Graphics and 15 colors, no colored background
SCREEN 12 = 640 x 480 resolution. Graphics and 15 colors, no colored background
SCREEN 13 = 320 x 200 resolution. Graphics and 256 colors, no colored background
There are other SCREEN modes, but we will only use the 4 that were mentioned on the previous page. Experiment with them and learn how different screen modes effect screen output. Type in the following program and run it:

SCREEN 0
CLS
COLOR 9
PRINT "Hi people."
COLOR 21
PRINT “This should be flashing purple because we added 16 to the regular colour of purple”
PRINT “which is just the number 5!!!”
END

To COLOR a background using the COLOR statement, adding two numbers (separated by a comma) allows you to control both foreground and background colours on those screen modes which support background colours.  Try typing in the following program sample:

SCREEN 0
COLOR 1, 4   
PRINT "I'm blue on red."
SLEEP 1
PRINT "Cool."
END


LOCATE statement:
Next we learn how to position text on the screen using the LOCATE statement. In QBASIC the standard screen is divided up into rows and columns, generally with 25 rows and 80 columns. Using the LOCATE statement, the programmer can position the cursor anywhere on the screen by referencing a row and column reference.  Type in the following program segment and run it to see what happens:

SCREEN 12
CLS
COLOR 14
LOCATE 2, 2
PRINT "Hello. I hope you're understanding this tutorial!!!"
END

The LOCATE statement positions the cursor based on a column and row designation. Experiment with different values to see what effect it will have on your output.


Exercise 3:
- Write a program that will horizontally centre the following statement:  NelHS Lords!!!
- Your program should have the display text in yellow, blinking on a blue background.

Input, Assignment and Screen Formatting Statements

In this next section, we will cover the following three statements: LET, INPUT and TAB().  These first two statements allow the programmer to store data in variable storage locations for use within the program, the TAB() statement is actually an addition to the PRINT statement which allows the programmer to position information on the screen similar to the method describe in the last section using LOCATE.

In the previous section, you learned how to use the PRINT and CLS commands.  In this section, you will learn about variables and the INPUT command.  What are variables?  Variables are "boxes" in the computer's memory to store a value - be it a number, name, decimal, dollar amount, etc.  When using variables, the programmer must first define them by providing a name for this storage “box”. Generally a good rule of thumb is to give the variable storage location a name which is descriptive of the kind of data which will be stored in it (e.g., Sum, Total, Name$, etc.)  For the programmer there are two main types of variables which can be used:

Numeric Variables – as the name suggests, this type of variable is used to store numbers (including: integers, decimals, etc.)  When naming a numeric variable the programmer needs only to pick a name which best describes the data. (e.g., sum, area, amount, total, etc.)

String Variables -  "strings," are also called text variables. That is, this storage location is used to store text and character based information. When naming a string variable the same rules apply as above, except the variable name must end with a $. (e.g., name$, address$, school$, etc.)



ASSIGNMENT statement:
You still may be in the dark as to how variables are used.  Variables are assigned a value using the LET command.  For example:

LET number = 123  

Or you could simply use the following line:

number = 123

This line of code would assign a value of 123 to the variable "number. Try inputting and running this segment in the program window to see what happens:

CLS
number1 = 4
number2 = 12
product = number1 * number2
PRINT product
END

The previous program should output to the screen the value assigned to "product."   If you want to include text before the number, making your program more descriptive and user friendly, try changing your program to reflect the following changes:

CLS
number1 = 4
number2 = 12
product = number1 * number2
PRINT “If you multiply “; number1 ; “ by “; number2
PRINT “The result will be “;product
END

INPUT statement:
While the assignment statement is an effective way of loading data/information/values into a variable storage location, it isn’t terribly user friendly. You might now be asking, "How do I ask the user at the keyboard for information/data that could be entered at the keyboard?"  You do this by using the INPUTstatement.  INPUT basically uses the same format as PRINT. Try running this:

CLS
INPUT “Please enter a number ”;number1
INPUT “Please enter another number “;number2
sum = number1 + number2
PRINT "The sum of the two numbers is"; sum
END

This would give the user a prompt for a couple of numbers, and then calculate their sum and assign that calculated value to the variable "sum". This information would then be printed out.  You can also supply a prompt for INPUT to get text information - like this:

INPUT "What is your name"; username$
PRINT "Hello, "; username$; "."

TAB() statement:
Suppose now, as a programmer, you wanted to further dress up your program and its on-screen formatting. By using the TAB() statement, along with the PRINT statement, your output can move to a new horizontal position – out from the left hand side of the screen (similar to using the LOCATE statement. Knowing that the screen is divided up into 80 columns, if you wanted to place your text at the 30th and 60th columns, you would simply type the following:

CLS
INPUT “Enter your name:”; name$
PRINT
PRINT
PRINT TAB(30);Name$;TAB(60);”how about this!!!”
END

**What effect does having the two PRINT statements in lines three and four have on screen output?

Exercise 4:

Type in the following program segments into the Program Area, then before you run them, try to predict what the screen output will look like:

CLS
num1 = 5
num2 = 8
num3 = 45
num4 = -30
name1$ = “Nelson”
name2$ = “High”
name3$ = “School”
PRINT num1, num2; num3, num4
PRINT num1 + num2
PRINT num4/num1
PRINT num5*num2
PRINT name1$ , name2$ ; name3$
END

Type in the following program, and again prior to running it, predict what the outcome would be on the screen:

CLS
INPUT “How many hours were worked?”, hours
INPUT “What is the rate of pay per hour?”, rate
totalpay = hours * rate
PRINT “The total pay earned is:”;totalpay;” dollars”
END

Exercise:
1. Write the correct basic code that would perform the following mathematical calculations:

5(6+3)

3(9+3)2

3+4
2+4

2. Write a program to input the user's name and age and output them to the screen.


3. Write a program that will accept as input from the user the length and width of a rectangular backyard. The program should then calculate the perimeter and area of the backyard, and display the appropriate information on the screen.

Top of Page