Introduction to QBASIC
Introduction
QBASIC (QUICK BASIC) is a programming language developed by Microsoft Corporation to use in the MS-DOS operating system. It is the successor of the earlier version of BASIC. QBASIC was developed by John J. Kemeny and Thomos E Kurtz in 1964 at Dartmouth College, New Hampshire. Today many version of BASIC are available such as BASICA, GWBASIC, Turbo BASIC, VB, and QBASIC etc.
QBASIC interpreter is a simple many driven program that is mostly
used to make programs by beginners but it can be sued to write very complex and
useful programs. It is a full screen editor that checks syntax error. It has
pull down menus with full debugging facilities. It is very user-friendly
language which helps to develop customized software packages for business
application music and games.
Features of QBASIC
i) It is easy to learn
and understand.
ii) It is user friendly
language.
iii) It is portable.
iv) It allows us to
test/debug the program.
v) It checks
automatically syntax errors.
vi) It provides online
help.
vii) It supports
structure programming.
viii) It allows
functions or procedure to be defined in the program.
Elements of QBASIC
Elements of QBASIC are the main building block of QBASIC program. the elements of QBASIC are necessary to build or write program in QBASIC. Following are the elements of QBASIC:
i) Character Set
ii) QBASIC words
iii) Constants
iv) Variables
v) Operators
vi) Expression
Character Set
QBASIC contains three sets of characters. The character set
includes alphabet, number and special characters. Special symbols, sign other than alphabets and numbers are called special characters.
Category |
Character/symbol |
Alphabet |
A
to Z or a to z |
Numerical/digits |
0
to 9 |
Special
characters |
$,%,@,!,(,_,),?,
\, / etc.. |
QBASIC words
The words which are used in QBASIC are called QBASIC words. Following are the two types of QBASIC words:
i) Reserved words
(keywords)
ii) User defined words
Reserved words
(Keywords)
The reserved words are also called keywords which have special
meaning to the QBASIC. Keywords are program, independent and provide the same
meaning in different programs. Examples: CLS, INPUT, PRINT, LET, SQR etc..
User Define Words:
The words which are defined by the user according to his need is
known as User define words. The word does not have any specific meaning. In
each and every program user define has the different meaning.
Constants
The entity (data or
value) which cannot be changed during the program
execution is called constant.
Types of constants:
a) Literal Constant
i) Numeric constant
ii) String constant
b) Symbolic constant
Numeric Constant:
All the numeric values
or data are known as numeric constants. Numeric constants are numbers having
positive or negative values on which mathematical operation such as addition,
subtraction, multiplication, and division could be solved. Examples: LET x=450,
LET p=34.65 etc.
Rules to be
followed while writing numeric constants:
i) Number with comma is
not allowed.
ii) A number can be
preceded by positive or negative sign. Negative sing (-) is compulsory but
positive (+) is optional.
iii) No special
character is allowed with number except decimal point.
String
Constant:
String constant is a text value that consists of alphabets,
digits, symbols and special characters, written within double quotation marks.
It cannot use in mathematical calculations. Two strings can be
concatenated or joined. Example: LET a$= “AVN”, LET p$= “4021” LET m$= “%$#@!”
etc.
Symbolic
Constant:
A scientific symbol
which is used to represent the fixed value is known as Symbolic constant.
Syntax:
CONST constant
name=expression
Example:
CONST pi=3.1416
Note:
Here, the value of pi cannot be changed or modified during the time of program
execution.
Variable
Variable is an entity
whose value changes during the program execution. It represents the storage
location in computer’s memory.
Rules of Variable
naming:
i) All the variable name should be start with alphabet.
ii) It can have numbers,
period(.), and data type declaration characters such as (%,!,#) as suffix.
iii) A variable name can
have maximum of 40 characters.
iv) Blanks spaces are
not allowed in verifiable name.
v) QBASIC keywords (LET,
PRINT, INPUT, CLS etc) are not allowed as variable name.
vi) No special
characters such as [,],*,~ etc. are used as variable name.
vii) A variable name
cannot begin with FN unless its function is defined.
Types of Variable:
Following are the
different types of variables.
i) Numeric variables
ii) String variables
Numeric
Variable:
Numeric variable is the
variable that stores numeric data including whole number and the number with
decimal. Numeric variable does not store string data.
Example: a, a%, a&
String
Variable:
A variable which stores
letters, numbers, and other special characters is known as string variable. A string
variable begins with alphabet and ends with dollar ($) sign. Example:
m$, a34$ etc.
Declaration of Variables
In most programming language, variables must be declared in
advance for the compiler. One of the most popular features of BASIC was that it
didn’t force the programmer to declare all variables in advance. In QBASIC
variable can be declared in following two ways.
Implicit Declaration:
In implicit variable declaration, the type of variable declared at
the same time when the value is assigned to it. It is specially used in simple
variable. The variable type is declared by using data type declaration
characters which are always used as suffix in variable name such as ($,%,! and
#).
Example: a%=45678.56
Note: Here variable has
been declared as an integer variable and the value is assign as 45678.56
Example: m$= “Dhangadhi”
Note: Here, m$ declared
as a string variable to store Dhangadhi string. Here, declaration and
assignment is done at the same place.
Explicit
Declaration:
A variable declaration that can be declared before they are used
in a program without any data type suffix is known as explicit declaration. it
is generally done at the beginning of the program. A variable is declared
explicitly using DIM statement. This type of declaration does not assign the
value but informs the program about variables.
Syntax:
DIM variable name AS
TYPE
Variable: Any valid variable names
Type: defines the types of variables (numeric
and string) it can be INTEGER, LONG, SINGLE DOUBLE, STRING
Example:
DIM marks AS INTEGER
Here, Marks is declared
as an integer variable explicitly
DIM name AS STRING
Here, name is declared
as string variable explicitly.
Date Types
The information such as name, age, date, salary,
marks, etc. is called data. Each data can store by a variable with same type.
The choice of data types for your variables can make a difference in the
results of the calculations. The proper variable types are determined by the
nature of the values they represent. The choice of data types is frequently
increase speed of execution. The varieties of such information it is classified
in different groups. Such data types used in QBASIC are given by:
a) String Data Types
i) Variable Length
String
ii) Fixed Length String
b) Numeric Data Types
i) Integer
ii) Long integer
iii) Single Precision
iv) Double Precision
a) String Data Types
A string is a
combination of characters and they are real objects, and hence, it is possible
to concatenate, modify and test them.
The string data type is a combination of alphanumeric characters
which are enclosed by double quotation mark and assign to string variable. Data
range of string variable is 0 to 32767 characters and memory consumption is 4
bytes. There are two types’ string data types. They are given below:
i) Variable length string
Any text or alphanumeric
value which can be input from or enclosed by double quotation mark is called
variable length string.
Example:
LET N$ = “Class 9”
INPUT “Enter your name:
“ ; N$
DIM Nam AS STRING
In the given example N$ is a variable length variable which do not
have any fixed character length and can be declare or use wherever we need in
the program. The variable N$ may store any string value with different length.
Such variable declaration is called implicit declaration.
ii) Fixed length string
A fixed length string is
also text or alphanumeric value which length is specified by the corresponding
variable.
Example:
DIM Nam AS STRING * 15
DIM Address AS STRING *
30
In the given example Name and Address are the fixed length string
variable. The variable Name and Address can store only 15 and 30 characters
respectively. Fixed length string variable most declare before use with
specified no of characters length. This is an explicit variable declaration.
The Name variable can store maximum 15 characters. In case of
fewer characters it keeps blank spaces to consume given number of characters.
If you supply the name “Ram Singh” it contains with 6 blank spaces.
b. Numeric Data Types
Numeric data types are
any numeric values either positive or negative. There are four different types
of numeric data types in QBASIC. They are given below:
i) Integer
Integer data type is a
positive or negative number which does not contains decimal point. Or the
number without decimal point is called integer data type.
Integer variables are
used to assign the integer data types. Integer variable name used % sign
with name of the variable. The range for integer variable is from -32768 to
32767 and memory consumption 2 bytes.
Example: LET
age% = 15
ii) Long integer
Integer data type has
very short range so that long integer data type is used to store such of the
large numbers. Long integer data stored to the variable which used & sign
with the name. Its range is from -2147483648 to 2147483647 and memory
consumption 4 bytes.
Example:
LET n& = 123454321
iii) Single Precision:
A single precision variable can store floating point number up to
six digits after the decimal point. If the number contains 7 digits, it is
rounded to the closed value. The single precision variable is declared using
exclamation (!) symbol at the end of the variable name. It
occupies 4 bytes of storage space in the memory.
Example:
per!, interest!
etc.
iv) Double
Precision:
The double precision variable holds numbers having decimal point
up to 16 digits. It is declared using hash (#) symbol at the
end of the variable name. It stores numeric data occupying 8 bytes of memory
space.
Example:
total#, gross# etc.
Data type Identifier
Data type declaration
characters are known as data type identifier symbols. Following are the symbols
which are used as data type identifier.
Data
type |
Symbol |
Maximum |
Minimum |
Bytes
used |
Integer |
% |
32767 |
-32768 |
2 |
Long integer |
& |
2,147,483,647 |
-2,147,483,648 |
4 |
Single precision |
! |
3.402823E+38 |
1.401298E-45 |
4 |
Double precision |
# |
1.7976931D+308 |
4.940656D-324 |
8 |
String |
$ |
32767 |
0 |
2 |
Expression
An expression can be a string or numeric constant, a variable or
single value obtained by combining constants, variables and expression. Following are the different types of expression supported by
QBASIC.
a) Arithmetic
Expression:
An expression, which contains values, variables and operators that
return a single numeric value after operations is called arithmetic expression.
Example: LET s=5+8-a
b) Logical
Operations:
A logical expression
contains values and operations, which are evaluated by QBASIC and returns one
of two logical values (1 or 0) or (True or False)
Example: PRINT 2>7
In above expression it
returns false value that is 0 because 2 is not greater than 7.
c) String
Expression:
An expression, which
returns string value after processing string with the help of string operator,
is called string expressing.
Example: PRINT m$+n$
N above example it
display the string value on the screen combining the string values of variables
m$ and n$
Operators
An operator is a symbol
or sign used to indicate a specific operation between the operands in a
program. Operators are special symbols that are used in expression, which
indicates an operation and performs mathematical calculation, comparison, or
logical operations. An operand is something on which the operator acts on. The
operands are variable or constants. The operator tells what operation to be
performed between the operands.
x + y
In the above example the Plus (+) is an operator
that tells the addition operation to be performed between the operands x and y.
The different types of operators that support
QBASIC are given by:
i) Mathematical
operators:
The mathematical operators are the arithmetical
operators used to perform normal mathematical calculations such as addition,
subtraction, multiplication and division. QBASIC does calculation according to
the normal rules of mathematics.
The different symbols are used as mathematical
operator with their use is given below.
Operator |
Operation |
Example |
+ |
Addition |
10 + 3 = 13 |
- |
Subtraction |
10 – 3 = 7 |
* |
Multiplication |
10 * 3 = 30 |
/ |
Division |
10 / 3 = 3.333 |
\ |
Integer Division |
10 \ 3 = 3 |
^ |
Exponential / Power |
10 ^ 3 = 1000 |
MOD |
Modulus Division |
10 MOD 3 = 1 |
Conversion of algebraic
expression to BASIC expression
All of the mathematical
expressions cannot write directly in BASIC so that we have to change such
notations into BASIC notations. In the table given below shows the rules of
conversion of algebraic expressions into BASIC expressions and vice-versa.
Order of preference
As we already discussed, QBasic does calculation according to the
normal rules of mathematics (algebra). The order of preference of mathematical
operators is given below;
Order of operator |
operation |
Example |
^ |
Exponentiation or power |
2^4=16 |
* or / |
Multiplication or Real Division |
2*4=8 / 5/2=2.5 |
\ |
Integer division |
5\2=2 |
MOD |
Modulus (Remainder) |
5 MOD 2 = 1 |
+ or - |
Addition or Subtraction |
5+2=7 / 5-2=3 |
ii) Relational Operators:
Relational operators are used to compare values between two
variables, constant or arithmetic expression. The result of comparison is
either “true” of “false”
The different symbols
are used as relational operator with their use is given below.
Operator |
Relation |
Numeric |
String |
> |
Greater
than |
A
> B |
A$>B$ |
< |
Less
than |
A
< B |
A$<B$ |
> = |
Greater
than and equal to |
A
> = B |
A$>=B$ |
< = |
Less
than and equal to |
A
< = B |
A$<=B$ |
= |
Equal
to |
A
= B |
A$=B$ |
<> |
Not
equal to |
A
<> B |
A$<>B$ |
iii) String /
Concatenation operator ( + )
A string expression
consists of string constants, string variables, and other string expressions
combined by string operator (+).
When a “+”
sign used between two different strings, it is said to be string or
concatenation operator. This operator does not add the string values but brings
together so that it seems a single word.
Example:
A$ = “Raja”
B$ =
“Ram”
C$ = A$ + B$
Now value of C$ will be “RajaRam”
iv) Logical operators
Logical operators are
used in decision making. They combine two or more relational expressions to
evaluate a single value i.e. true or false. QBASIC support six
logical operators (AND, OR, NOT, XOR, IMP & EOQ) among them first three are
commonly used.
Operator |
Meaning |
AND |
Provides true result when all the expressions become true. |
OR |
Provides true result when at least one of the expressions
become true |
NOT |
Provides reverse result |
A truth table is a table showing the necessary
inputs and corresponding output values from the input values. The truth table
is used to calculate the output from the supplied input. Following truth table
makes clear about logical operators.
AND truth table
Expression 1 |
Expression 2 |
Result |
T |
T |
T |
T |
F |
F |
F |
T |
F |
F |
F |
F |
OR truth table
Expression 1 |
Expression 2 |
Result |
T |
T |
T |
T |
F |
T |
F |
T |
T |
F |
F |
F |
NOT truth table
Expression |
Result |
T |
F |
F |
T |
Also Read: Programming Language and its types
Orders on operators
Operators |
Order of preference |
Arithmetic operator |
1st |
Relational operator |
2nd |
Logical operator |
3rd |
You can go through with video also.
No comments:
Post a Comment