QBASIC(Quick Beginners all
Purpose Symbolic Instructions Code) is a programming language. It supports
structured programming. The older
version of the QBASIC is BASIC invented
by Professor J. G. Kemeny and T. E. Kurtz of Dartmouth College, New Hampshire,
U.S.A. as a language for the beginners and was implemented in 1964. Later Microsoft
Inc has been releasing QBASIC programming language. New version of QBASIC
contains new features. QBASIC has its own compiler. QBASIC is supplied as an
additional programming environment in DOS diskettes as an interpreter.
Basics of QBASIC
The foundation of QBASIC
programming is made with various elements. These elements are discussed in the
coming sections.
Basic Data Types: Different types of data are sued in QBASIC.
These are listed as follows.
DATA Type
|
Declaration Symbol
|
Maximum
|
Minimum
|
String
|
$
|
32767 Chars
|
0 Chars
|
Integer
|
%
|
32767
|
-32768
|
Long Integer
|
&
|
2147483647
|
-21474836478
|
Single(7 digit) precision
|
!
|
3.402823E+38
|
1.401298E-45
|
Double(15 digit) precision
|
#
|
1.7976931
|
4.940656 D324
|
User – Defined Data Types
|
String
There are two kinds of strings
used in QBASIC. They are:
(a) Variable – length String
(b) Fixed length string
(a)
Variable –
length string : A variable – length sring is a sequence of up to 32767
characters. The codes for these characters range from 0 – 127 for regular ASCII
(American Standard Code for Information Interchange) characters, and from 128 –
255 for the extended – ASCII characters.
(b)
Fixed Length
string: A fixed – length string contains a declared number of
characters, no more than 32767. Like variable – length strings, a fixed –
length string contains characters with code ranging from 0 – 255.
String type of
data are assigned from right to a variable on the left of equal to sign. the
variable contains $ sign as suffix.
A$= "Happy
Diwali"
here, A$ is a
variable and happy diwali is a actual data.
Integer Long Integer
Integers are stored as 16 bit
binary numbers (two bytes) ranging in value from -32768 to +23767. Long
integers are stored as assigned 32 – bit binary numbers( four bytes) ranging in
value from -2147483648 to +2147483647.
User Defined Data Typed
Basic lets you define new data
types using the TYPE statement. A user – Define type is an aggregate type made
up of elementary BASIC types. for example, The following type statement defines
a type, SymTabEntry.
TYPE SymTabEntry
Identifier AS STRING *40
LineNumber AS LONG
Value AS LONG
END TYPE
Constants
Constants are predefined values
that do not change during program execution. There are two types of constants.
There are:
(a)
String Constant
(b)
Numeric Constant
(a). String Constant: A string
constant is a sequence of alphanumeric characters enclosed by double quotation
marks. There alphanumeric characters can be any of the characters whose ASCII
codes fall within the range 0 – 255 (except the double quote character(").
(b). Numeric constants : A
Variable
A variable is a name that refers
to an object, i.e. a particular number, string or record. A record is a
variable declared to be a user-defined type, Simple variables refer to a single
number, string or record. Array variables refer to a group of objects, all of
the same type.
A numeric variable, whether, simple
or array, can be assigned only a numeric value either integer, long integer,
single precision or double precision. A string variable can be assigned only a
character- string value. You can assign one record variable to another
only if both variables are the sage user defined type.
Example,
A constant value:
a=9.5
The value of another string or
numeric value:
B$="Disk of operators"
A$=B$
Variable Name
A QBASIC variable name may
contain up to 40 characters. The characters allowed in a variable name are
letters, numbers, the period(.) and the type
- declaration characters(%, &, !, # and $). The first character in a
variable name must be a letter. If a variable begins with FN, it is assumed to
be called to DEF FN function.
Expressions and Operators
An expression can be a string or
numeric constant, a variable, or a single value obtained by combining
constants, variable and other expressions with operators.
Operators perform mathematical or
logical operations on value. The operators provided by BASIC can be divided
into five categories.
a) Arithmetic Operator
b) Relational Operator
c) Logical Operator
d) Functional operator
e) String operator
a)
Arithmetic
Operators: Arithmetic operators
perform calculations.
Examples,
+ : Addition
- : Subtraction
* : Multiplication
/ : Division
% : Percentage
b)
Relational
Operator : Relational operators
compare strings and numeric values. Relational operators compare two values.
The result of the comparison is either true or false.
These are given
below:
Operator
|
Relation
|
Expression
|
=
|
Equality
|
x=y
|
<>
|
Inequality
|
x<>y
|
>
|
Greater
|
x>y
|
<
|
Less
|
x<y
|
>=
|
Greater than Equal
|
x> = y
|
<=
|
Less than Equal
|
x < = y
|
c)
Logical
Operator : Logical operators text complex conditions or manipulate
individual bits. Logical operators performs texts on multiple relations, bit
manipulations, or Boolean operations and returns a true or false values to be used
in making a decision.
Example,
if D<=200
And F<5 then 40
While i>=10
or k<0
The six logical
operators in BASIC, listed in order of precedence,
Operators
|
Meaning
|
NOT
|
Logical Complement
|
AND
|
Conjunction
|
OR
|
Disjunction (inclusive
"or")
|
XOR
|
Exclusive "or"
|
EQV
|
Equivalence
|
IMP
|
Implication
|
d)
Functional
Operators : - Functional operators supplement simpler operators. A
function is used in an expression to call a predetermined operation to be
performed on an operand. For example, SQR is a function used twice in the
following assignment statement.
A=SQR(25)
e)
String
Operator :- String operators combine and compare strings. A string
expression consists of string constants, string variables, and other string
expressin consists of string constants, string variables, and other strings
expressions combined by string operators. There are two classes of string
operations: concatenation and string function.
Example,
A$="FILE"
:B$="NAME"
PRINT A$+B$
Output
FILENAME
Flowchart
A flowchart is a diagram that represent the
acts such as inputs, calculations and output that must be performed in proper
order to solve the problem with the help of program. Flowchart helps us design
logically correct solutions to a problem. We can keep on making improvements on
the flowchart until all the aspects of the problem have been represented in the
diagram.
Algorithm
An algorithm is a well-defined
sequence of acts or procedures to perform the solution of problem in a finite
number of steps. Hence, a flowchart is an excellent vehicle to represent an
algorithm. Stepwise instructions to solve the problem as known as algorithm.
Control Statement
Statements that controls the
program statements. There are two types of control statements.
- Decision Making
a). If…….. then
……..else …….statement
b). Select……….
Case Statement
2. Loop
Statements
a). For Loop
b). While Loop
c) Do Loop
e) Goto Loop
(breaking statement)
a)
If …. then ……else statement
The if else
statement used to express conditional expressions. If the given condition is
true then it will execute the statements; otherwise it will execute the
optional statements.
Syntax: 1
if (Conditional
_ Expression) then
statement1
statement2
else
statement1
statement2
end if
Example,
if(x>y) then
print "X
is greater"
else
print "Y
is greater"
end if
Syntax2.
if(conditional
_Expression)
statement1
statement2
elseif(conditional
_expression)
statement1
statement2
elseif(conditional
_ expression)
statement1
statement2
else
statement1
statement2
endif
Example,
cls
input
"Enter your choice";ch
if(ch=1)
print "The
day is Sunday"
elseif(ch=2)
print "The
day is Monday"
elseif(ch=3)
print "The
day is Tuesday"
elseif(ch=4)
print "The
day is Wednesday"
elseif(ch=5)
print "The
day is Thrusday"
elseif(ch=6)
print "The
day is Friday"
elseif(ch=7)
print "The
day is Saturday"
else
print
"Please don't type more than 7 and less then 0"
end if
b)
Select Case statement
The select case
is a special multi way decision maker that tests whether an expression matches
one of the number of constant values, and braces accordingly. The syntax is
given below:
Select case
<expression>
case is
=<exp>
statement1
case is
=<exp>
statement1
end select
Example,
cls
input "Your choice";ch
select case ch
case is=1
print "Sunday"
case is=2
print "Monday"
case is = 3
print "Tuesday"
case is=4
print "Wednesday"
case is =5
print "Thursday"
case is =6
print "Friday"
case is=7
print "Saturday"
end select
c). For Loop
The for loop is most commonly
used statement in QBASIC. This loop consists of three expressions. The first
expression is used to initialize the index value, the second to check whether
or not the loop is to be continued again and the third to change the index
value for further iteration.
Syntax:
for expression _ 1 to expression
_ 2 step value
statement1
statement2
next <expression>
Example,
CLS
for i=1 to 10 step 2
print i
next i
d). While Loop
The second type
of loop , the while loop, is used when we are not certain that the loop will be
executed. After checking whether the initial condition is true or false and
finding it to be true, only then the while loop will enter into the loop operations.
Syntax:
While conditional _ expression
statement 1
statement 2
wend
Example,
CLS
x=1
while x<20
print x
x=x+1
wend
e). Do Loop
It also loop statement.
Syntax:
do
statement1
statement2
loop while(condition)
Example,
CLS
x=1
do
print x
x=x+1
loop while(x<=20)
f). Goto Statement
The Goto statement is used to
alter the program execution sequence by transferring control to some other part
of the program. There are two types of goto statement.
Unconditional Goto
: The unconditional goto statement is used just to transfer the control from
one part of the program to the other part without checking any condition.
Normally, a good programmer will not prefer to use the unconditional goto
statement in his program as it may lead to a very complicated program like a
never ending process.
Syntax:
Start:
statement1
statement2
goto start:
Conditional Goto
The conditional goto is used to
transfer the control of execution from one part of the program to the other in
certain conditional cases.
Syntax:
start:
statement1
statement2
statement2
if(conditional_ Expression) then
goto start;
else
end
Practical
1.
Write a program to print radius of the circle.
DEF fna (a) = 3.14 * r * r
INPUT "value of radius"; r
PRINT "the area of circle"; fna(a)
END
2. Write a program to print the simple interest.
CLS
pv = 5000
r = 12
t = 2
si = (pv * r * t) / 100
PRINT si
END
3. Write a program to find qube value.
CLS
INPUT "first value"; a
INPUT "second value"; b
INPUT "third value"; c
x = (a + b + c) ^ 3
PRINT x
END
4. Write a menu driven program to display a menu by using
case statement.
s:
PRINT "main manu"
PRINT "*********"
PRINT " 1. change ic to nc"
PRINT "2.area of the circle"
PRINT "3. greater number"
PRINT "4.to calculate the total salary"
PRINT "5. month of the year"
PRINT "6. student result sheet"
PRINT "7.exit"
INPUT
"enter your choice", ch
SELECT
CASE ch
CASE
IS = 1
CLS
INPUT
"indian currency", ic
nc =
ic * 1.6
PRINT
"the nepali currency", nc
GOTO
s:
CASE
IS = 2
CLS
INPUT
"radius of circle", r
pi =
3.14
ac =
pi * r ^ 2
PRINT
"area of the circle", ac
GOTO
s:
CASE
IS = 3
CLS
INPUT
"value of x", x
INPUT
"value of y", y
IF (x
> y) THEN
PRINT "x
is greater"
ELSE
PRINT
"y is greater"
END IF
GOTO
s:
CASE
IS = 4
INPUT
"basic salary", bs
hra =
bs * 5 / 100
ma =
bs * 4 / 100
ta =
bs * 2 / 100
da =
bs * 4 / 100
ts = bs
+ ma + da + hra + ta
PRINT
"total salary", ts
GOTO s:
CASE IS
= 5
INPUT
"value of the month", m
SELECT CASE m
CASE IS
= 1
PRINT
"junary"
CASE IS
= 2
PRINT
"february"
CASE IS
= 3
PRINT
"march"
CASE IS
= 4
PRINT
"april"
CASE IS = 5
PRINT
"may"
CASE IS
= 6
PRINT
"june"
CASE IS
= 7
PRINT
"july"
CASE IS
= 8
PRINT
"august"
CASE IS
= 9
PRINT "september"
CASE IS
= 10
PRINT
"october"
CASE IS
= 11
PRINT
"november"
CASE IS
= 12
PRINT
"december"
END
SELECT
GOTO s:
CASE IS = 6
INPUT
"student result sheet", result
INPUT
"eng", eng
INPUT
"math", ma
INPUT
"nepali", ne
INPUT
"science", sc
tot =
eng + ma + ne + sc
percent = tot / 4
IF
(percent >= 60) THEN
PRINT
"first"
ELSEIF
(percent >= 45) THEN
PRINT
"second"
ELSE
PRINT
"third"
END IF
PRINT
"total", tot
PRINT "percent", percent
END
CASE IS
= 7
END
END
SELECT
5. Write a program to print maximum value and minimum
number. number input through the keyboard.
CLS
DIM x(10)
FOR i = 1 TO 10
INPUT " any value"; x(i)
NEXT i
max = x(1)
min = x(1)
FOR i = 1 TO 10
IF max < x(i) THEN max = x(i)
IF min > x(i) THEN min = x(i)
NEXT i
PRINT "the maximum value"; max
PRINT "the minimum value"; min
END
6. Write a program to print Fibonacci series.
CLS
x = 1
y = 1
PRINT x
WHILE z < 144
z = x + y
PRINT z
x = y
y = z
WEND
END
7. Write a program to enter
student marks from the keyboard and print the total mark, percentage,
division and result.
CLS
INPUT "marks of english=", en
INPUT "marks of math=", ma
INPUT "marks of nepali=", ne
INPUT "marks of science=", sc
INPUT "marks of social=", soc
total = en + ma + ne + sc + soc
percent = total / 5
f$ = "first"
s$ = "second"
t$ = "third"
p$ = "pass"
f1$ = "fail"
CLS
PRINT "**********************************************************"
PRINT "marks of english=", en
PRINT "marks of math=", ma
PRINT "marks of nepali=", ne
PRINT "marks of science=", sc
PRINT "marks of social=", soc
PRINT "total marks= ", total
PRINT "percentage=", percent
IF (percent >= 60) THEN
PRINT "the division is:first"
ELSEIF (percent >= 45) THEN
PRINT "the division is:second"
ELSE
PRINT "the division is:third"
END IF
IF (en >= 32 AND ma >= 32 AND ne >= 32 AND sc >=
32 AND soc >= 32) THEN
PRINT "the result is:pass"
ELSE
PRINT "the result is:fail"
END IF
PRINT
"******************************************************************"
END
Functions:
Functions are the ready made
formula in QBASIC . There are mainly two types of functions in QBASIC.
1.
String Functions
For Example,
CHR$, ASC, STR$, UCASE$,
LCASE$ DATE$, TIME$ Etc.
2.
Numeric Functions
For Example,
ABS, SIN, TAN, COS, Etc.
File Handling
Open
It is a command
which opens a file to read and write in qbasic.
Syntax
Open <mode>, <file number>, <file
Expression>
Example,
OPEN "O", #1,"hari.txt"
Mode in File handling
"O" : -
Write data into a sequencial file.
"I" :- Reads data form the sequencial file.
"A" :- Write data into a sequencial file from the
end of the records. Or appends the records or data.
"R"
Reads or writes the files from the random access file.
"B" :-
Read or write data from binary file.
CLOSE
This command closes all open files which are opened to read
and write data.
Syntax:
CLOSE <File number>
Example,
CLOSE #1
WRITE
This command allows you to write the data into sequecial a
file.
Syntax:
Write <File Number>, Expression1, expression2..exp n.
Example,
WRITE #1,NAME$,ADDRESS$,PHONE
WIRTE #1, "HARI", "BARDIA", 524545
PRINT#
The print# command allows you to store or write data into a
sequencial file.
Syntax:
Print <Fine number> ,Expression1, Expression2....exp
n.
Example,
PRINT #1,NAME$,ADDRESS$,PHONE
INPUT #
INPUT Command reads data form the sequencial file and
assigns them into a variable.
Syntax:
INPUT <FILE Number>, <Exp1.exp2...expn.>
Example,
INPUT #1,NAME$,ADD$,PHONE
LINE INPUT #
The LINE INPUT#
Command reads an entire line from the sequential file.
Syntax:
LINE INPUT <FILE Number>,Exp1, Exp2..expn.>
Example,
LINE INPUT #1, REC$
INPUT$
It reads the strings
or characters up to n from the sequential file.
Here n means number of charactes.
Syntax:
INPUT$(N,<File number>)
Example
INPUT$(12,#1)
EOF( ) :-End of File;
EOF() is a
function which checks or tests the end of file condition. It will check that
the file records are end or not.
Syntax:
EOF(File Number)
Example,
While EOF(#1)
Print
"Name";name$
Print
"Address";add$
Print
"Phone";phone
Wend
KILL
It is command
which deletes the files in DOS.
Syntax:
KILL <file
Name>
Example,
Kill
"hari.txt"
NAME AS
It is a command which renames the file old
name to new name in DOS.
Syntax:
NAME <OLD FILE NAME> AS <NEW FILENAME>
EXAMPLE,
NAME
"Hari.txt" as "Geeta.txt"
MKDIR
It will create a new directory.
Syntax:
MKDIR
<Directory Name>
Example,
MKDIR
"Hari"
CHDIR
Change Directory
To come to one step up of the existing directory.
Syntax
CHDIR <Existing
Directory Name>
Example,
CHDIR
"hari"
RMDIR :- Remove Directory
It can be use to
remove the directory in dos .
Syntax:
RMDIR <existing
directory name>
Example,
RMDIR
"HARI"
thanks bro you helped me alot
ReplyDelete