| Table of Contents | | Previous | | Next |
In this course we will be using QBasic 4.5.
Q - Quick B - Beginners A - All-purpose S - Symbolic I - Instruction C - Code
Each line may contain up to 255 characters. Each line may have more than one command provided that they are separated by colons.
Keywords. Keywords are words that have a special meaning in QBasic and indicate an action to be preformed.
BEEP, CALL, CHAIN, CHDIR, CIRCLE, CLEAR, CLOSE, CLS, COLOR, COM, COMMON, CONST, DATA, DATE, DECLARE, DEF, DIM, DO, DO UNTIL, DO WHILE, DRAW, END, ERASE, ERROR, EXIT, FIELD, FILES, FOR, FUNCTION, GET, GOTO, IF, ELSE, END IF, INPUT, KEY, KILL, LET, LINE, LOCATE, LOCK, LOOP, LPRINT, LSET, MID, MKDIR, NAME, NEXT, ON COM, ON, ON KEY, ON PEN, ON PEN GOSUB, ON PLAY GOSUB, ON STRIG GOSUB, ON TIMER GOSUB, ON EVENT GOSUB, OPEN, OPTION BASE, OUT, PAINT, PALETTE, PCOPY, PEN, PLAY, POKE, PRESET, PRINT, PSET, PUT, RANDOMIZE, READ, REDIM, RESET, RESTORE, RESUME, RETURN, RMDIR, RESET, RUN, SCREEN, SEEK, SELECT CASE, CASE, END CASE, SHARED, SHELL, SLEEP, SOUND, STATIC, STOP, STRIG, SUB, SWAP, SYSTEM, TIME, TROFF, TRON, TYPE, UEVENT, UNLOCK, VIEW, WEND, WHILE, WIDTH, WINDOW, WRITE |
Variables. A location in storage whose value can change as the program is
executed. Keywords may not be used as variable names. QBasic is not
case sensitive. COUNT, Count and count are all the same variable.
Variables can be numeric or string. Variables can be declared to be of a
specific type by appending with a special character ($ - String
% - Short Integer & - Long Integer
! - Single Precision # - Double Precision).
Some programmers like to use periods to separate words. For example
Employee.Name$, Employee.Address$ and Employee.Salary.
Constants. A location in storage whose value does not change as
the program is executed. The numeric constants in QBasic can be integer,
fixed point or in exponential form (174, -35.1, 62E-23). String constants
are enclosed in double quotes.
Assignment statements. The equals sign in a line of computer
code has a different meaning than the equals sign in mathematics. In
computer science the equals sign means that the information on the right is
being assigned to a storage location on the left.
Arithmetic operators + addition
- subtraction
* multiplication
/ division ^ exponentiation
Comments. Documentation makes programs easier to debug and
maintain. Internal comments may be included using the following
syntax (i) REM comments or (ii) ' comments. These comments are ignored by
the interpreter/compiler.
Computers cannot directly execute programs written in a high level language such as QBasic. Computers must first translate the QBasic statements into machine language instructions that are understood by the computer. Compilers and interpreters are two types of software programs that can perform this translation. Compilers translate the entire program into machine language and create an executable file. Interpreters translate each statement into equivalent machine language instructions. The machine level instructions are executed and the results are obtained without producing an intermediate machine language program.
The INPUT statement is used for entering small amounts of data or to process data as it occurs. The READ, DATA and RESTORE statements are for processing large amounts of data especially when the data is internal to the operation of the program. DATA statements can only appear in the main program, they cannot be placed in subprograms. In QBasic DATA statements are non-executable and are frequently placed at the end of the program. The READ statement must be place before any assignment statements requiring the use of that data. The PRINT statement is used to print information to the screen. Commas used in PRINT statements have the effect of moving you from one print zone to the next and are convenient for placing output in a tabular format. Semicolons used in PRINT statements allow you to display information in a packed or compressed format. The PRINT USING statement may be used to format the output. The LOCATE statement allows you to control the position of the cursor on the screen. The CLS statement can be used to clear the screen. If this statement is not used, the screen will show the results of previous program runs.
INPUT | INPUT "Enter an ordered pair x,y" , x, y |
READ DATA RESTORE |
READ x1, x2, x3 sum = x1 + x2 + x3 RESTORE 'Reset pointer to first data item READ x1, x2, x3 product = x1*x2*x3 PRINT x1, x2, x3, sum PRINT x1, x2, x3 product DATA 1, 3, 9 |
PRINT "Distance = ", rate*time PRINT "Income "; TAB(10); "Tax rate"; SPC(5); "Tax" |
|
PRINT USING | PRINT USING "$$######.##"; income |
LOCATE | LOCATE 10, 20: INPUT "Enter x", x |
In structured programming all program logic is a combination of the following basic three control structures. In this section we add the decision making commands.
IF |
SELECT CASE |
IF tax >= 0 THEN switch$ = "y" PRINT gross.pay ELSE switch$ = "n" END IF |
SELECT CASE gpa CASE IS >= 90 Letter.Grade$ = "A" CASE IS >= 80 Letter.Grade$ = "B" CASE IS >= 70 Letter.Grade$ = "C" CASE IS >= 60 Letter.Grade$ = "D" CASE IS >= 0, IS < 60 Letter.Grade$ = "F" CASE ELSE PRINT "Negative letter grade." END SELECT |
Loops are used for repetitive calculations such as counters and accumulators and for inputting or outputting large amounts of data. A do loop begins with a DO statement and ends with a LOOP statement. There are four types of do loops available.
DO LOOPS | FOR loops | ||
Execute while a condition is true |
Execute until a condition is true |
||
Terminate at the top |
i=1 DO WHILE i < 5 PRINT i i = i + 1 LOOP |
i=1 DO UNTIL i > 5 PRINT i i = i + 1 LOOP |
|
sum = 0 FOR count = 1 TO 100 STEP 1 sum = sum + count NEXT count PRINT "The sum is "; sum |
|||
Terminate at the bottom |
i=1 DO PRINT i i = i + 1 LOOP WHILE i < 5 |
i=1 DO PRINT i i = i + 1 LOOP UNTIL i > 5 |
FOR i = 1 TO 4 PRINT "Outer loop ", i FOR j = 1 TO 3 PRINT "Inner loop ", i,j NEXT j NEXT i |
The table below contains a partial list of the functions built into QBasic.
ABS, COS, DATE$, EOF, EXP, INT, LCASE$, LEFT$, LEN$, LOG, POINT, POS, RND, SGN, SIN, SPC, SQR, TAB, TAN, TIMER, TIME$, UCASE$ |
An array is a string or numeric storage variable that allocated a specific number of storage locations. In other words a programmer can store more than one value under the same variable name. This is much more convenient when the data is related. Arrays are commonly used for sorting and other table processing applications. Some examples of array declarations and manipulation are shown below.
1 |
DIM Month(1 TO 12) FOR i = 1 TO 12 READ Month(i) NEXT i FOR i = 1 TO 12 PRINT Month(i) NEXT i DATA 12, 23, 34, 45, 43, 34 DATA 34, 56, 76, 87, 34, 56 |
2 |
DIM Table(1 TO 5, 1 TO 5) FOR i = 1 TO 5 FOR j = 1 TO 5 Table(i,j) = 0 NEXT j NEXT i |
Subroutines are a convenient method for dividing a large program into smaller sections that each carry out a specific task. If you want the routines to be more independent use subprograms instead. If the specific task requires several numbers and produces only one number, a function may be more appropriate.
Subroutines | Functions | Subprograms |
GOSUB Accept.Data GOSUB Add.Data GOSUB Display END Accept.Data: Add.Data: Display: |
p = 500 r = 0.08/12 c = 2 PRINT "FV is"; FV(p,r,c) END FUNCTION FV(pp, rr, nn)
|
CALL Ps(4.5, 5*6, "Fun") END SUB Ps(x, y, s$)
|
When using large amount of data, it is preferable to have the data stored in files to reduce the amount of data entry needed. QBasic provides two types of file organization - sequential and random. A sequential file is limited to sequential processing. The records can be processed only in the order they are placed in the file. In a random the processing sequence is not related to the sequence in which the records are stored. If the tenth record is needed, it can be accessed directly without processing the previous nine.
Sequential file processing |
|
Read from file | OPEN "INVENT.DAT" FOR INPUT AS #1 DO WHILE NOT EOF(1) INPUT #1, Stock$, Location$, Cost, Price, Quantity LOOP |
Write to a file | OPEN "INVENT.DAT" FOR OUTPUT AS #1 WRITE #1, Stock$, Location$, Cost, Price, Quantity CLOSE #1 |
The SCREEN command can be used to select a graphics mode.
SCREEN 0 | Text Mode (25 rows and 80 columns) |
SCREEN 1 | Medium Resolution Graphics (200 rows and 320 columns) |
SCREEN 2 | High Resolution Graphics (200 rows and 640 columns) |
You can use the usual letters and numbers on the keyboard or you can extend the character set using the CHR$(code) command where code is an integer between 128 and 255. The string command STRING$(n, code) is useful for repeating characters. The COLOR foreground, background command may be used to set foreground and background colors for the screen (0-black, 1-blue, 2-green, 3-cyan, 4-red, 5-magenta, 6-brown, 7-white, 8-grey). As an example consider the following program which draws a border.
Program for displaying a border in text-mode. |
SCREEN 0 COLOR 7, 4 LOCATE 8,9: PRINT CHR$(201); STRING$(20,205); CHR$(187) FOR I = 9 TO 17 LOCATE I, 9: PRINT CHR$(186) LOCATE I, 30: PRINT CHR$(186) NEXT I LOCATE 18, 9: PRINT CHR$(200); STRING$(20,205); CHR$(188) END |
COLOR background, palette
Choose a background color using the numbering scheme presented in text graphics mode above. The choices of palettes for the foreground are shown in the table below. Select palette number 0 or 1. Graphics statements following the color statement may then select a color from the chosen palette.
Palette 0 | Palette 1 | Number |
Background color | Background color | 0 |
Green | Cyan | 1 |
Red | Magenta | 2 |
Brown | White | 3 |
PSET, PRESET
The point set and point reset statements may be used to set a point on the screen to one of the four colors from the active palette.
SCREEN 1 COLOR 5, 0 FOR J = 1 TO 100 PSET (50, J) NEXT J END |
LINE
LINE (x1,y1) - (x2, y2) , color, box | Draws a line connecting the two points. Color selects a color from the active palette. Box is B for box or BF for a box filled with color. |
LINE -(x,y) | Draws a line from the last point referenced. |
CIRCLE
CIRCLE (x,y), radius, color, start, end, shape | (x,y) is the radius. color selects the color from the active palette. start and end are the angles as measured in radians (negative values cause a wedge to be drawn). Shape is the ratio of the radius in the y-direction to the radius in the x-direction and can be used to draw ellipses. |
PAINT
PAINT (x,y), paint, boundary | (x,y) are the coordinates of a point within the region to be filled with color. Paint is a numeric or string expression. The numeric expression determines the color. If a string expression is used, it determines the tiling pattern. | Pi=3.1415926 SCREEN 1: CLS COLOR 1, 0 CIRCLE (40,40), 20, 1 PAINT (40, 40), 3, 1 END |
DRAW
The DRAW command instructs the computer to draw an object an object defined by a string expression. The string expression contains codes for drawing lines, points, setting colors, and performing other operations all within a single command.
' Draw a sailboat SCREEN 1: CLS COLOR 9, 1 DRAW "C2 L10 F10 R20 E10 L30 BU1 C3 U40 F30 L30" PAINT (160, 105), 2, 2 PAINT (165, 85), 3, 2 END |
(1) C2 selects the color magenta for the boat (2) L10 moves and draws to the left 10 (3) F10 move and draw diagonally down and right 10 (4) R20 move and draw to the right 20 (5) E10 move and draw diagonally up and right 10 (6) L30 move and draw to the left 30 (7) BU1 move up one row (8) C3 select the color white for the sail (9) U40 move and draw up 40 (10) F30 move and draw diagonally down and right 30 (11) L30 move and draw to the left 30. |
WINDOW
The WINDOW statement redefines the coordinates of the screen. In medium and high resolution the upper left hand corner is the origin (0,0) and the coordinate extends down and to the right. After using the window statement the coordinate system is the same as the one used in mathematics. When using the command WINDOW (x1,y1) - (x2,y2), the first point is the upper left hand corner and the second point is the lower right hand corner.
'Plot y=x^2 SCREEN 1: CLS COLOR 9, 7 WINDOW (-10,10)-(10,-10) FOR I = -10 TO 10 STEP 0.1 PSET (I,I*I) NEXT I END |
GET and PUT
The GET and PUT commands are used for creating animations. Use the GET statement to store a figure and use PUT statement to move the figure from one location to another.
Write programs to compute area of rectangles, triangles and circles.
Write a program that inputs a name, converts to uppercase letters and then outputs. This program should concatenate the first and last names.
Write a program that either computes the future value of an annuity or compute payment size needed to amortize a loan.
Statistics functions such as mean, standard deviation, skewness and kurtosis.
Discrete logistic equation (time series and final state diagram).
Julia sets and other fractals.
| Table of Contents | | Previous | | Next |