QBasic Manual
QBasic Manual
QBasic Manual
Training
QBASIC HANDS-ON TRAINING MANUAL
An Introduction
to QBASIC
PROGRAMMING
Benson Idahosa University
5th – 6th July 2012
QBasic has all the necessary facilities to teach a first computer course in problem solving
programming, data filing and management, vector based graphics, pixel level raster
graphics, sound, and more.
You cannot teach (or learn) programming principles using the huge top end programming
packages which are the tools of trained and experienced computer professionals who,
themselves learnt QBasic in their earlier years.
QuickBasic 4.5 is a more advanced version that is a compiled language. It has library files
and other features more suited to teaching those who have completed an introductory
course and may be required to be taught more advanced principles with an emphasis on
teaching how to produce functional software or how to produce software to solve specialist
tasks. The capacity to teach how to make library and produce 'executables' (i.e programs
converted to machine code that can be run on a computer without the QuickBasic program
being present), using software that fits on a diskette (940K zipped or 2.2 meg installed)
makes the software unrivalled for teaching advanced computer programming principles for
Senior High School or College technical discipline students. There is no other software that
comes remotely close. You can teach a beginner's first course using Quickbasic if you want
to. The interface is the same as Qbasic, you just don't mention the additional features.
When a problem is solved on a computer it is essential to work out in detail all the decisions
to be taken by the machine, the actions to be performed and the sequence in which these
are to be performed. This planning is aided by flow-charts.
A flowchart depicts in a pictorial form the sequence of steps performed for solving the
problem.
It identifies the steps needed in solving a problem and shows these steps with connecting
lines and arrows to indicate the order in which the steps are performed.
Flowchart is the most frequently used tool to develop a solution procedure. One should be
skilled in drawing flowcharts to be a successful programmer. The figure below illustrates
some of the most frequently used flowchart symbols.
These flowchart symbols are used in some of the examples given to illustrate how they are
applied.
Get the qbasic compiler by copying or downloading a copy of the qb45.zip folder which you
can get through google.com
Step 2
Extract the qbasic.zip folder to the root drive of the computer, mostly Local Disk C:
Step 3
Step 4
Type qb.exe on the command prompt and press enter to launch the QBasic environment
* Quit QBasic
To QUIT QBasic Program :
- Press Alt- FX Key
- QBasic editor is a full screen, context sensitive, syntax sensitive and self – correcting
tool for writing programs.
1. Heading
2. Body
DATA TYPES
Data is differentiated into various types. The type of data element restricts the data element
to assume a particular set of values.
String (STR) - This string is a special type. It can contain characters like ‘A’ or ‘ƒ’. Strings are
always notated with double quotation marks around them (“). The length of the character-
list can range up to 64k.
Integer (INT) - This data type is the most used data type of all. You can store only non-
decimal numbers in an integer. Since an integer contains 16 bits, you can only store values
from - 32768 up to 32767.
Long (LNG) - Long integers are not used that often, since mostly integers work fine. Long
integers are actually the same as normal integers, so only non-decimal numbers. However, a
long integer contains 32 bits, allowing you to store values from –2147483648 to
2147483647.
Single (SGL) – This type of data type can accommodate decimal numbers. Singles are 32 bit
and you can store values from around 9e40 to 9e-39.
Double (DBL) – Double can also contain decimal numbers. The only difference is that
doubles are 64 bits large, thus providing space for values from around 9 e305 to 9e-310.
IDENTIFIERS
Identifiers are names given to various program elements, such as, variable, constant, arrays,
functions, sub-program etc
* Variable
*Constant
Reserved word: There are certain reserved words, called Keywords that have standard,
predefined meanings in QBasic. These Keywords can be used only for their intended
purpose; they cannot be used as programmer defined identifiers.
CONSTANTS
A Constant is an entity whose value does not change during execution of program.
Constants are of different types :
VARIABLE
A variable is a reference that can change its value during the execution of a program. In
QBasic, variables can be of the basic data types or user defined data type. A variable name
is used to refer to a value.
Uses of Variable:
Variable Name:
• Cannot contain other than letters ,numbers ,period and type declaration characters(
$,%,#,&,! )
VARIABLE DECLARATION
$ String
% Integer
! Single Precision
# Double Precision
MAXAMT% = 1000
For Example,
The following program should be typed and then run in the QBASIC editor. It displays 2 lines
on the screen
REM text
PRINT Statement: Allows text to be sent to the screen when the text is placed in double
quotes (“ ”)
Syntax:
PRINT “Text to be displayed”
ARITHMETIC OPERATORS
QBasic provides ARITHMETIC operators for comparing the values of two expressions.
+ Addition
- Subtraction
* Multiplication
/ Division (Real)
\ Division (Integer)
^ Exponent
MOD Remainder
HIERARCHY OF OPERATIONS
The order of priority or precedence in which the arithmetic operations are performing arithmetic
statement is called the hierarchy of operations.
PRIORITY OPERATORS
1ST Parentheses – All brackets are evaluated first.
RELATIONAL OPERATORS
QBasic provides six relational operators for comparing the values of two expressions.
LOGICAL OPERATORS
QBasic provides three logical operators for joining two or more relational expressions.
AND - Operator will produce result as true only if two operands have true value
OR - Operator will produce result as false only if two operands have false value
NOT – Operator will produce result as true if operand is false and vice versa
LET Statement is used to assign a value to a variable. The value can be an expression or a
literal and must be the same type as the variable.
Syntax:
The [LET] itself is optional. The variable can be assigned without explicitly using the word
[LET]
For Example:
a = 10:b = 5
LET area = 0.5 * BASE * HEIGHT
QUE$ = “HOW ARE YOU? “
Example 1
Example 2
Output
SHREEJI CONSULTANCY SERVICE 21
GOTO STATEMENT
GOTO Statement is an Unconditional branch. i.e. Program control will continue with the
line or label that the GOTO references. The Unconditional branch can only be at the same
level as the GOTO statement. You cannot use GOTO to enter or exit a subroutine or
function.
Syntax:
GOTO line number / line label
line label - A Valid Line Label , String with a letter can’t exceed 40 characters in length and
always terminated with colon
For Example
END STATEMENT
Syntax:
END [{DEF | FUNCTION | IF | SELECT | SUB | TYPE}]
Example
PROGRAM over.
STOP STATEMENT
Syntax :
STOP
Example:
Output
1
2
3
4
6
7
8
9
10
Output is 55.5
INT - returns the largest integer less than or equal to a numeric expression.
COS, SIN, TAN - returns the cosine, sine, and tangent of a specified angle.
SGN - returns a value indicating the sign of a numeric expression, 1 if the expression is
positive, 0 if it is zero, or -1 if it is negative
EXP - returns e raised to a specified power, where e is the base of natural logarithms
Syntax:
where variable1 and variable2 are two variables of the same type.
INPUT STATEMENT
Syntax:
This is one of the most commonly used methods in QBasic for entering values of variables.
"prompt" - An optional literal string that is displayed before the user enters data. A
semicolon after prompt appends a question mark to the prompt string.
NOTE:
For keyboard input, a semicolon immediately after INPUT keeps the cursor on the same line
after the
user presses the Enter key. INPUT can be accepted into STRING, NUMERIC, ARRAY, and
RECORD ELEMENTS.
Example 1:
CLS
END
Output
NAME : John Grant
AGE : 36
AGE : 36
If value entered was of incorrect type or too many or too few values were entered then
compiler gives following error “redo from start”
Example 2
CLS
DIM P, R, T, SI AS SINGLE
PRINT “ENTER THE VALUES OF P, R, T”
INPUT P, R, T
SI = P * R * T / 100
PRINT SI
END
Output
Example 3
DO IT YOURSELF!
Write a QBASIC program to calculate a student’s average score of 4 subjects taken in an
exam and the scores entered from the keyboard
1. if-then-else statement
2. if-then-elseif … else statement
IF-THEN-ELSE STATEMENT
It is used to test the condition or to take decision. Sometimes it is also referred as decision-
making statement.
Syntax:
Allows conditional execution of a group of statements. However, there are many situations
when two groups of statements are given and it is desired that one of them be executed if
some condition is true and the other be executed if the condition is false. In such situations,
we make use of the if-else statement whose general format is:
Syntax:
IF condition1 THEN
[statementblock-1]
[ELSEIF condition2 THEN
[statementblock-2]]
...
[ELSE
[statementblock-n]]
END IF
Example 1
Output
Enter any number : 4
Even
Enter any number : 7
Odd
Example 2
While purchasing certain items a discount of 10% is offered if the quantity purchased is
more than 1000. If quantity and price per item is input through the keyboard, write a
QBASIC program to calculate the total expenses
If 3 numbers are input through the keyboard, write a QBASIC program to determine the
largest of the 3 numbers.
INPUT A, B, C
LARGEST = A
IF B > LARGEST THEN LARGEST = B
IF C > LARGEST THEN LARGEST = C
PRINT LARGEST
END
DO IT YOURSELF!
If 3 numbers are input through the keyboard, write a QBASIC program to determine the
smallest of the 3 numbers
When one of the many alternatives is to be selected, we can design a program using if
statement to control the selection. However, the complexity of such a program increases
dramatically when the number of alternatives increases. The program becomes difficult to
read and follow. Some times it may even confuse the person who designed it.
In ‘QBasic’, there is a built-in multi-way decision statement, which is known as SELECT CASE
statement. The SELECT CASE statement tests the value of a given variable/expression
against a list of case values and when a match found, a block of statements associated with
that case is executed.
...
statementblockn
Example 1
Output
Enter A B OP : 3 2 +
ADDITION
RESULT = 5
Enter A B OP : 10 7 -
SUBTRACTION
RESULT = 3
Enter A B OP : 10 7 *
MULTIPLICATION
RESULT = 70
Enter A B OP : 6 3 /
REAL DIVISION
RESULT = 2
Enter A B OP : 8 3 =
INVALID OPERATOR
Example 2
To control the flow of execution of programs, the ‘QBasic’ language provides very powerful
facilities.
1. Sequential
2. Selective or decision making or conditional
3. Repetitive or iterative or looping
It consists of:
* an entry point that may include initialization of certain variables
* a loop Continuation Condition
* a loop body
* an exit point
A repetitive structure must always be entered at the entry point to ensure that the
appropriate initialization takes place. The loop body consists of statements that are
normally executed several times.
The exit point is the first statement following the loop body. The number of repetitions in a
repetitive structure is controlled by a loop continuation condition, which is tested once for
each execution of the loop body.
1. For Loop
2. Do ... While/Until Loop
3. While ... Wend Loop
FOR Loop
The for loop is a very flexible and powerful repetitive structure (PRE-TEST LOOP). If you
know the number of times a block of statement(s) that you want to execute a specific
number of times then for - loop is most appropriate.
[statementblock]
NEXT [counter]
Where
increment: The amount the counter is changed each time through the loop
Example 1
FOR I = 1 TO 5
PRINT I
NEXT I
Output
1
2
3
4
5
Example 2
FOR I = 7 TO -6 STEP –3
PRINT I
NEXT I
Output
7
4
1
-2
-5
Example 3
REM PRINT 1st N NUMBER. ALSO CALCULATE SUM & AVERAGE OF N NUMBERS
CLS
DIM I, N, SUM AS INTEGER
DIM AVG AS SINGLE
FOR I = 1 TO N
PRINT I
SUM = SUM + I
NEXT I
AVG = SUM / N
PRINT "SUM : "; SUM
PRINT "AVERAGE : "; AVG
END
Output
ENTER N : 5
1
2
3
4
5
SUM : 15
AVERAGE : 3
Example 4
Output
ENTER N : 5
N N*N N*N*N
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
SUMN = 15
SUM OF SQUARE = 55
SUM OF CUBE = 225
Example 5
Example 6
PRINT J
J = J + 2
NEXT I
END
Output
ENTER N : 5
1ST N ODD NUMBERS ARE AS FOLLOWS
1
3
5
7
9
1ST N EVEN NUMBER ARE AS FOLLOWS
0
2
4
6
8
Do
body of loop
[ { While } ] (expression)
The do-while type of loop repeats the body of the loop while expression is TRUE
Example
REM
CLS
DIM count AS integer
count = 0
do
PRINT “ Count = ”,count
count = count + 1
while (count < 10)
END
Output
Count = 0
Count = 1
Count = 2
Count = 3
Count = 4
Count = 5
Count = 6
Count = 7
Count = 8
Count = 9
Do
body of loop
[{Until}](expression)
body of loop may contain one or more statements
The While … Wend loop is a pre-test loop, whose general format is:
While (expression)
body of loop
Wend
body of loop may contain one or more statements
Example
REM
DIM count AS integer
CLS
count = 1
While ( count <= 10 )
PRINT “Count = ”,count
count = count + 1
Wend
END
Output
Count = 1
Count = 2
Count = 3
Count = 4
Count = 5
Count = 6
Count = 7
Count = 8
Count = 9
Count = 10
ARRAYS
An ordered finite collection of data items, each of the same type, is called an array, and the
individual data items are its elements.
Only one name is assigned to an entire array, and individual elements are referenced by
specifying a subscript. A subscript is also called an index.
In QBasic,
* Subscripts Range: - 32768 to + 32767
Arrays, like simple variable, need to be declared before use. An array declaration is of the
form :
: - 32768 TO + 32767
: Maximum 60 dimension
For example,
1st element is referenced by a (0), 2nd element is referenced by a (1), and so on.
When an array is referenced with a subscript that is outside the declaration, message “
Subscript out of range “ appears.
EXIT STATEMENT
Exit Statement: Exits a DO or FOR loop, a FUNCTION or SUB procedure, or a DEF function.
Syntax:
EXIT {DEF | DO | FOR | FUNCTION | SUB}
DO Exits a DO loop.