-------------------------------------------------------------------
Copyright 1998 Richard J. Gaydos.
-------------------------------------------------------------------
A QBASIC Primer
-------------------------------------------------------------------
Beginning Material
-------------------------------------------------------------------
From C: enter qbasic to start up QBASIC
Type your program in the top portion of the screen
The bottom portion is for commands, like run
-------------------------------------------------------------------
Ctrl + Home gets you to the beginning of your program
Ctrl + End gets you to the end
-------------------------------------------------------------------
The last statement in each program is the END statement
-------------------------------------------------------------------
REM is used for a comment
Use this comment box:
REM------------------------------------------------------------REM
REM REM
REM REM
REM REM
REM------------------------------------------------------------REM
-------------------------------------------------------------------
Variables
-------------------------------------------------------------------
Variable Names can be up to 40 characters long
Do NOT use QBASIC reserved words, like REM
Use A-Z, 0-9, and period. NO SPACES !!
Start with a letter
Capitalize the first letter of each word
Examples
TestScore
StudentCounter
-------------------------------------------------------------------
Variable Suffixes
-------------------------------------------------------------------
! Single Precision Floating Point (the default)
% Single Precision Integer
# Double Precision Floating Point
& Double Precision Integer
$ Character Data
Examples
GPA
GPA!
TestScore%
AccurateResult#
BigNumber&
StudentName$
-------------------------------------------------------------------
but these extra characters can take away from code readability, so ...
DIM variable AS typename
DIM Name$ AS STRING * 15
DIM Number AS INTEGER
DIM GPA AS SINGLE
-------------------------------------------------------------------
Assignments
-------------------------------------------------------------------
The CONST Statement
-------------------------------------------------------------------
CONST variable = expression
Examples
CONST CityTaxRate = .015
CONST NumberOfLabs = 7
-------------------------------------------------------------------
The Assignment Statement
-------------------------------------------------------------------
destination = expression
Examples
Number = 16
Average = 87.9
Name$ = "Rick"
Total = First + Second
Count = Count + 1
Total = Total + Score
-------------------------------------------------------------------
Note: Integers ROUND on assignment
Example
Number% = 6.9
PRINT Number% produces 7
Exception: x.5 rounds to nearest EVEN integer
Examples: 5.5 rounds to 6, and
6.5 rounds to 6 TOO
-------------------------------------------------------------------
The Swap Statement
-------------------------------------------------------------------
SWAP variable1, variable2
Example
Place = 27
Value = 14
SWAP Place, Value
Before After
Place holds 27 Place holds 14
Value holds 14 Value holds 27
-------------------------------------------------------------------
Arithmetic
-------------------------------------------------------------------
Hierarchy of Operations - Similar to Algebra
1. ( ) Do work in parentheses first
2. ^ Exponentiation
3. * / Multiplication and division
4. \ Integer division
5. MOD Remainder after integer division
6. + - Addition and subtraction
Examples
Place = 7
Value = 5
Location = 2
Formula Answer is
Answer = (Place + Value) / Location 6
Answer = Place + Value / Location 9.5
Answer = Place * Value / Location 17.5
Answer = Value ^ Location 25
Answer = Value \ Location 2
Answer = Value MOD Location 1
Answer = 4.5 4 if Answer is Integer
-------------------------------------------------------------------
Output
-------------------------------------------------------------------
PRINT Statement
-------------------------------------------------------------------
PRINT list
Examples
Location = 9
Number = 7
Name$ = "Rick"
Statement
Output
PRINT Location, Number, Name$ 9 7 Rick
PRINT Location; Number; Name$ 97Rick
-------------------------------------------------------------------
Note: ONE blank space before NUMBERS
-------------------------------------------------------------------
PRINT USING Statement
-------------------------------------------------------------------
PRINT USING format; list
Example
Line1$ = "###BB###.##BB$#,###.##BB$$.##BB$***.##BB###+"
Number = 12
Cost = 12.34
Amount = 555.68
Value = -5
PRINT USING Line1$; Number, Cost, Amount, Cost, Cost, Value
The EXACT Output would be
b12BBb12.34BB$$bb555.68BBb$12.34BB$*12.34BBbb5-
-------------------------------------------------------------------
Numeric Format Characters - Rounding on Output
### Digits appear, right-justified
. Period
, Comma if significance started
$ Dollar sign here
$$ Floating
** Star fill
+ Sign appears
- - if negative, blank if not
B blank appears
_ Following character appears as is
-------------------------------------------------------------------
Example
Line2$ = "!BB\b\BB&"
First$ = "Rick"
Last$ = "Gaydos"
PRINT USING Line2; First$, First$, Last$
The EXACT output would be
RBBRicBBGaydos
-------------------------------------------------------------------
Character Format Characters
\BB\ Allows Four characters to print
! Single character
& Whole string, left-justified
B blank appears
_ Following character appears as is
-------------------------------------------------------------------
CLS clears the screen
-------------------------------------------------------------------
LPRINT has the same two formats as PRINT, but sends the output to
the PRINTER
-------------------------------------------------------------------
File I/O
-------------------------------------------------------------------
To CREATE a file on disk
OPEN "filename.ext" FOR OUTPUT AS #number
WRITE #number, list probably several times
CLOSE #number
Example
OPEN "my.fil" FOR OUTPUT AS #1
WRITE #1, Number, Value
CLOSE #1
-------------------------------------------------------------------
Note: Output in file separated by commas
Note: Character file output stored WITH double quotes around text
-------------------------------------------------------------------
Then, to READ from the file you created
OPEN "filename.ext" FOR INPUT AS #number
INPUT #number, list probably several times
CLOSE #number
Example
OPEN "my.fil" FOR INPUT AS #1
INPUT #1, Number, Value
CLOSE #1
-------------------------------------------------------------------
Input
-------------------------------------------------------------------
Screen input
-------------------------------------------------------------------
INPUT "prompt", list Use a comma to separate data you type in
Example Type in Stored
INPUT "Please enter a number", Number 43 43
INPUT "Please enter a name", Name$ Rick Gaydos Rick Gaydos
INPUT "Enter a Number & Name", Number, Name$ 12,RJG 12 and
RJG
-------------------------------------------------------------------
Input from your Program
-------------------------------------------------------------------
READ list
DATA data,data,data
Example
READ Name$, Number
DATA "Rick Gaydos",12
-------------------------------------------------------------------
Conditions
-------------------------------------------------------------------
A Condition compares two quantities, using one of the
Relational Operators
= Equal to
<> Not Equal to
> Greater than
>= Greater than or Equal to
< Less Than
<= Less Than or Equal to
The RESULT is either TRUE or FALSE
-------------------------------------------------------------------
Examples
Hours > 40
Name$ = "Rick"
-------------------------------------------------------------------
Multiple Conditions
AND BOTH conditions must be true for the multiple
condition to be true
OR AT LEAST ONE of the conditions must be true for the
multiple condition to be true
-------------------------------------------------------------------
Examples
Hours > 40 AND Name$ = "Rick"
Hours > 40 OR Name$ = "Rick"
-------------------------------------------------------------------
Decisions
-------------------------------------------------------------------
Simple One-line IF Statement
IF condition THEN statement
-------------------------------------------------------------------
The IF Statement
-------------------------------------------------------------------
IF condition THEN
statements
ELSEIF condition
statements
ELSE
statements
END IF
-------------------------------------------------------------------
Example
IF Hours > 40 THEN
PRINT "Overtime"
ELSE
PRINT "No overtime"
END IF
-------------------------------------------------------------------
Example
IF Hours > 40 THEN
PRINT "Overtime"
END IF
-------------------------------------------------------------------
A Big Decision
-------------------------------------------------------------------
IF Score >= 90 THEN
Grade$ = "A"
ELSEIF Score >= 80 THEN
Grade$ = "B"
ELSEIF Score >= 70 THEN
Grade$ = "C"
ELSEIF Score >= 60 THEN
Grade$ = "D"
ELSE
Grade$ = "F" <----- Notice NO COMPARISON needed !!
END IF
-------------------------------------------------------------------
The CASE Statement
-------------------------------------------------------------------
SELECT CASE expression
CASE test1
statements
CASE test2
statements
CASE ELSE
statements
END SELECT
-------------------------------------------------------------------
Tests can be 1. expression1, expression2, ... for possible equalities
2. expression1 TO expression2 for a range
3. IS relational-op expression for an inequality test
-------------------------------------------------------------------
Example
SELECT CASE Score
CASE IS >= 90
Grade$ = "A"
CASE IS >= 80
Grade$ = "B"
CASE IS >= 70
Grade$ = "C"
CASE IS >= 60
Grade$ = "D"
CASE ELSE
Grade$ = "F"
END SELECT
-------------------------------------------------------------------
Loops
-------------------------------------------------------------------
DO pretest
body of loop
LOOP posttest
-------------------------------------------------------------------
Pretest or Posttest can be
WHILE condition or
UNTIL condition
-------------------------------------------------------------------
Example
Number = 5
DO WHILE Number > 0
PRINT Number
Number = Number - 1
LOOP
produces the output
5
4
3
2
1
-------------------------------------------------------------------
Example
Number = 5
DO UNTIL Number = 0
PRINT Number
Number = Number - 1
LOOP
produces the output
5
4
3
2
1
-------------------------------------------------------------------
I prefer to NEVER USE posttest loops !
-------------------------------------------------------------------
The Standard Processing Loop
-------------------------------------------------------------------
Read a valid record
DO WHILE the record is valid
Process the valid record
Read the next record
LOOP
-------------------------------------------------------------------
Example
A file of test scores might look like
88
99
77
98
95
89
-3
We could process these records by
READ Score
DO WHILE Score >= 0
Process the score
READ Score
LOOP
-------------------------------------------------------------------
The For ... Next Loop
-------------------------------------------------------------------
FOR counter = start TO end STEP increment
body of loop
NEXT counter
-------------------------------------------------------------------
Example
FOR Number = 1 TO 5 STEP 2
PRINT Number
NEXT Number
produces the output
1
3
5
-------------------------------------------------------------------
Example
FOR Number = 1 TO 3
PRINT Number
NEXT Number
produces the output
1
2
3
-------------------------------------------------------------------
The EXIT FOR Statement
-------------------------------------------------------------------
Example
FOR Number = 1 TO 50
PRINT Number
IF Number = 4 THEN
EXIT FOR
END IF
NEXT Number
produces the output
1
2
3
4
-------------------------------------------------------------------
Subprograms - Subroutines & Functions
-------------------------------------------------------------------
Subroutines - Get New Sub Under Edit Menu
Get SUBs Under View Menu
-------------------------------------------------------------------
DECLARE SUB name (arguments)
CALL name (arguments)
END
SUB name (parameters)
the subroutine
END SUB
-------------------------------------------------------------------
Example
DECLARE SUB Testit (Count)
Count = 5
CALL Testit (Count)
END
SUB Testit (Number)
PRINT Number
END SUB
produces the output
5
-------------------------------------------------------------------
Functions
-------------------------------------------------------------------
DECLARE FUNCTION name (arguments)
... name (arguments) ... <--- USED in some statement, rather than CALLed
END
FUNCTION name (parameters)
the function
name = formula <------ MUST end with this for the RETURN
END FUNCTION
-------------------------------------------------------------------
Example
DECLARE FUNCTION Addone (Count)
Count = 4
Count = Addone (Count)
PRINT Count
END
FUNCTION Addone (Number)
Addone = Number + 1
END FUNCTION
produces the output
5
-------------------------------------------------------------------
Global vs. Local Variables
-------------------------------------------------------------------
All variables in subprograms are local, unless exposed as parameters
to the arguments (which allows for TWO-WAY communication) in the
calling program OR ...
-------------------------------------------------------------------
The COMMON SHARED Statement - Placed in MAIN program ONLY
-------------------------------------------------------------------
COMMON SHARED list <---- Globalizes variables
-------------------------------------------------------------------
Example
DECLARE SUB Printit ()
COMMON SHARED Count
Count = 4
CALL Printit ()
END
SUB Printit ()
PRINT Count
END SUB
produces the output
4
-------------------------------------------------------------------
The next three programs are used to illustrate one reason why we
have arrays.
Program One Print detail lines, then totals
Program Two Print totals FIRST, then detail lines, FILE version
Program Three Print totals FIRST, then detail lines, ARRAY version
With the ARRAY version, we do NOT have to read from the disk TWICE,
rather we store the numbers in RAM.
REM-------------------------------------------------REM
REM REM
REM Simple Loop, Totals LAST REM
REM REM
REM-------------------------------------------------REM
OPEN "a:number.fil" FOR INPUT AS #1
REM-------------------------------------------------REM
REM REM
REM Main Loop, including Print Detail Lines REM
REM REM
REM-------------------------------------------------REM
INPUT #1, Number
DO WHILE Number > 0
Total = Total + Number
PRINT Number
INPUT #1, Number
LOOP
REM-------------------------------------------------REM
REM REM
REM Print Totals REM
REM REM
REM-------------------------------------------------REM
PRINT
PRINT "Total is"; Total
CLOSE #1
END
REM-------------------------------------------------REM
REM REM
REM Simple Loop, Totals FIRST, File Version REM
REM REM
REM-------------------------------------------------REM
REM-------------------------------------------------REM
REM REM
REM Main Loop, NO Print Detail Lines REM
REM REM
REM-------------------------------------------------REM
OPEN "a:number.fil" FOR INPUT AS #1
INPUT #1, Number
DO WHILE Number > 0
Total = Total + Number
INPUT #1, Number
LOOP
CLOSE #1
REM-------------------------------------------------REM
REM REM
REM Print Totals REM
REM REM
REM-------------------------------------------------REM
PRINT "Total is"; Total
PRINT
REM-------------------------------------------------REM
REM REM
REM 2nd Loop, To Print Detail Lines REM
REM REM
REM-------------------------------------------------REM
OPEN "a:number.fil" FOR INPUT AS #1
INPUT #1, Number
DO WHILE Number > 0
PRINT Number
INPUT #1, Number
LOOP
CLOSE #1
END
REM-------------------------------------------------REM
REM REM
REM Simple Loop, Totals FIRST, Array Version REM
REM REM
REM-------------------------------------------------REM
REM-------------------------------------------------REM
REM REM
REM Main Loop, NO Print Detail Lines REM
REM REM
REM-------------------------------------------------REM
DIM HoldNumber (15)
OPEN "a:number.fil" FOR INPUT AS #1
INPUT #1, Number
DO WHILE Number > 0
Total = Total + Number
Count = Count + 1
HoldNumber (Count) = Number
INPUT #1, Number
LOOP
CLOSE #1
REM-------------------------------------------------REM
REM REM
REM Print Totals REM
REM REM
REM-------------------------------------------------REM
PRINT "Total is"; Total
PRINT
REM-------------------------------------------------REM
REM REM
REM 2nd Loop, To Print Detail Lines REM
REM REM
REM-------------------------------------------------REM
FOR Slot = 1 TO Count
PRINT HoldNumber (Slot)
NEXT Slot
END
-------------------------------------------------------------------
Arrays
-------------------------------------------------------------------
DIM name (lower TO upper)
DIM name (lower TO upper, lower TO upper)
-------------------------------------------------------------------
Examples
DIM Name$ (20)
DIM Sales (10)
DIM Years (1986 TO 1995)
DIM TwoDimensional (4, 6)
-------------------------------------------------------------------
The TYPE Statement - Sets up a pattern, defines NO space
-------------------------------------------------------------------
TYPE name
variable AS STRING size
INTEGER
SINGLE
END TYPE
-------------------------------------------------------------------
NOTE: variable can NOT have $, !, etc. in definition
-------------------------------------------------------------------
TYPE Student
Name+ AS STRING * 15
Number AS INTEGER
GPA AS SINGLE
END TYPE
-------------------------------------------------------------------
To implement:
DIM some variable AS typename
-------------------------------------------------------------------
Need to QUALIFY individual names: StructureName.field
-------------------------------------------------------------------