0% found this document useful (0 votes)
36 views40 pages

QBASIC

Uploaded by

Sanjeev Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views40 pages

QBASIC

Uploaded by

Sanjeev Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

QBASIC

QBASIC is one of the most popular high level


programming language. Various versions of BASIC have
been developed by Microsoft Company. This language is
quite simple to understand and has been adopted by most
of the microcomputers. It is suitable for both mathematical
and business problems. It is compatible with MSDOS
environment and it has two basic files QBASIC.EXE and
QBASIC.HLP. We can edit, debug and execute the
program using these two files.
The advantages of QBASIC
QBASIC is easy to learn and fun to practice. It may be
called a "People's language".
It is available almost in every computer from micro to
mainframe. Therefore, a program developed in a micro
can run on bigger system with minor modifications.
It is suitable for mathematical and business application.
Program development cycle is quick, debugging is
simple, and
Modification of program is quite easy.
Features of QBASIC
It is a user friendly language.
It is widely known and accepted
programming language.
It is one of the most flexible language, a
modification can easily be done in already
existing program.
Language is easy since the variables can be
named easily and uses simple English phrases
with mathematical expressions.
QBASIC has the character set consisting of the following
elements:
Alphabets: A, B, C,....Z
Digits: 0, 1, 2........,9 and
Special characters: + - * / ( ) . , $ ; ,: ,= ,> ,< , ^
The symbol ^ (caret) is used to denote exponentiation
operator, the symbol * (asterisk) is used to denote
multiplication and other symbols; have their usual
meanings.
Constants and Variables
A quantity in a computer program which does not change its
value during the execution of the program is called a constant
and the quantity which may change its values during the
execution of the program is called variable. QBASIC allows
the following constants:
1. Numeric constant
Numeric constant is one that is formed by a sequence of digits
0, 1, 2,.....9 and may include a decimal point. A numeric
constant known as number may be integer or a real number.
383, +57, 0, -6.2 and 6.15E4 are valid numeric constants.
2. String constant
A string constant consists of a sequence of characters which
must be enclosed by a quotation mark. Example “A”, “hi”,
Variable
In QBASIC, variables are also of two types:
Numeric variable :Numeric variable can assume
numeric value and is represented by an alphabet or an
alphabet followed by another alphabet or digit. For
example A, C, A2, ABC, A6 etc, represent numeric
variables.
String variable : A string variable is represented by an
alphabet followed by dollar ($) sign. It should be kept in
mind that while constructing the string variable, dollar ($)
should be the last character. For example B1$, NAME$,
BOOK1$, etc are valid string variables.
Variables -----
A variable is just like the variables used in CODE-----
math class. If I showed you x + 3 = 4, then CLS
you would know that x = 1 because x=4
subtracting 3 from each side leaves x by y=2
itself on the left while 1 is left on the right PRINT 4 + 2
after solving 4 – 3.
PRINT x + y
The first one we will discuss is numeric
--------------
variables. Numeric variables are used just
-----
like the variables in math, so you could say
OUTPUT----
something like
PRINT x + y
-
but instead of you solving for x and y, you 6
get to state what they are and the interpreter 6
does the rest for you. Let’s make that ----------------
example work like a real program.
Variable
A few more notes to remember about the naming of variables.
The name of variable must:
Only contain numbers and/or letters, no symbols
Not be a keyword.
Keywords are the words that already have a function or use in
the language. If you tried to use them, then the interpreter
would be confused and wouldn’t let you run the program.
Be less than 40 characters
Some examples of good variables are:
answer
7eleven
seven11
variable6354
TYPES OF MODE IN QBASIC
DIRECT MODE : The accepts single line
instructions from the user and the output is
viewed as soon as enter key is pressed. The
instructions are not stored in the memory .This
mode can be used to do quick calculations.
They do not have line numbers.
e.g. Print 3+4 Print ”This is the direct mode in
QBASIC”
TYPES OF MODE IN QBASIC
PROGRAM MODE : The mode is used to
type a program which is stored in the memory.
They have line numbers. We have to give the
command to get the output.
E.g. 10 PRINT 3+4
20 END RUN
Programs are built up with set of instructions
or commands. Every Programming Language
has its owns SYNTAX (rules) and
COMMANDS.
QBASIC COMMANDS
CLS:- This command is used to clear the screen.
PRINT:- Print command is used to display the output on
the screen. Let us see how the PRINT command works.
If you put the PRINT alone, without any text, it will print an
empty line.
PRINT with Semi-Colon (;) placed after the message to be
displayed prints the messages on the same line although the
commands are on different lines.
PRINT “Name – ”; name$; “. Age – ”; age
END:- This command is usually given at the end of the
program. Statements written after end are not executed
since the program terminates execution on reading this
command.
QBASIC COMMANDS
REM:- It stands for Remark.
It gives an explanation of the program or of the statements
in the program thereby making the program more
understandable to the reader.
The computer does not execute this statement since
whatever is written after REM is ignored by the compiler.
REM can be used anywhere and many times in a program.
DELETE:-To delete a line number in a program. e.g.
SYNTAX: DELETE <LINE NO.>
Example: Delete 10 — will delete line number 10
Delete 30-50 — will delete all line numbers from 30 to 50
QBASIC COMMANDS
INPUT:- This statement allows the user to enter a value
for the variable while running the program.
A question mark (?) appears on the output screen waiting
for the user to enter a relevant data and then press enter key.
Once the Return key or Enter key is pressed the data is
stored in the variable and then processed.
SYNTAX: INPUT < VARIABLE >
Example: Input A—Enter Numeric constant
Input N$—Enter Alphanumeric constant
COMMANDS WHICH WORKS ON
PROGRAM
 LIST: The command is used to list the program on the screen.
 RUN: The command is used to execute a program.
 LLIST: The command is used to list of program as a hardcopy.
 LPRINT: The command is used to get the output of the program on the hard
copy.
 NEW: The command is used clear the memory of the existing program and
write a totally new program.
 SYSTEM: The command is used to take you back to dos prompt.
 RME: The command is used to show the position of the mistake.
 SAVE: The keyword is used to save the program. e.g.
 SAVE “PROGRAM1” — QBasic will automatically add a period and an
extension “bas” to the filename.
 LOAD: The keyword is used to LOAD the program from the disk to the
memory. e.g.
 LOAD “PROGRAM1”
OPERATOR
Operators are symbols that indicate the type of operation
QBASIC has to perform on the data or on the values of
variables.
 There are four types of operators in QBASIC. They are Arithmetic
Operators, Relational Operators, Logical Operators and Sting
Operator.
Arithmetic Operators:- Arithmetic Operators are used to
perform mathematical calculations like addition, subtraction,
division, multiplication and exponential. The following table
shows arithmetic operators used in QBASIC.
Arithmetic Operators
 Operation -------------Operator --------------Example -----------------Result
i. Addition -----------------(+) --------------------- 5+8 ----------------------13
ii. Subtraction -------------(-) ---------------------- 8-6 -----------------------2
iii. Multiplication ---------(*) --------------------- 5*4 -----------------------20
iv. Division ---------------- (/) --------------------- 8/2 ------------------------4
v. Integer Division -------- (\) -------------------- 9\2 -------------------------4
vi. Exponential ------------ (^) ------------------- 4^3 ------------------------- 64
vii. Modular Division --- Mod ------------------ 7 mod 3 --------------------1

 Relational Operators:- Relational Operators are use to perform comparisons


on two values of same type. A comparison of sting data with numeric data
cannot be done. The comparison of sting data is done on the basis of ASCII
value. The result of comparison is either true (non zero) or false (zero).
The following table shows the relational operators used in QBASIC.
Relational Operators
Operator -----------Relation ---------------------------- Example
i. = ------------------ Equal to -------------------------- A = B, A$ = B$
ii. > ----------------- Greater than --------------- A > B, “CAT”>”RAT”
iii. < ---------------- Less than --------------------- A < B, "cat" < "cat"
iv. > = -------------- Greater than or equal to ---- A > = B, X$ > = Y$
v. < = --------------- Less than or equal to -------- A < = B, X$ < = Y$
vi. < > -------------- Not equal --------------------- A$ < > B$, X <> Y.
Logical Operators
Logical Operators combine two or more relational expressions to
evaluate a single value as True (Non Zero) or False (Zero). The result
of evaluation is used to make decisions about the program flow. The
commonly used logical operators in QBASIC are AND, OR and NOT.
Logical Operators
AND Operator:
AND operator returns ‘True’ when all the results returned from
individual relational expressions are ‘True’ otherwise it returns
‘False’.
The AND Truth Table is given shown below.

Condition1 (P) ------ Condition2 (Q) ------ Result (P AND Q)


F ------------------------------- T ----------------------------------- F
T ------------------------------- F ----------------------------------- F
F ------------------------------- F ----------------------------------- F
T ------------------------------- T ----------------------------------- T
Logical Operators
OR Operator:
OR Operator return ‘True’ if any one of the relational
expressions returns ‘True’. If all the relational expressions
returns ‘False’ then only the combined result returned by OR
operator will be ‘False’.

The OR Truth table is as given below.


Condition 1 (A) --------Condition2 (Q) -------Result (A or B)
F ----------------------------------- T ----------------------- T
T ----------------------------------- F ------------------------T
T ----------------------------------- T ----------------------- T
F ----------------------------------- F ----------------------- F
NOT Operator
NOT Operator operates on one operand and returns ‘True’
if the logical operation returns ‘False’. The NOT truth
table is as given below.

Condition1 (A) --------------------------- Result (NOT A)


F ----------------------------------------------- T
T ----------------------------------------------- F
String Operator
String Operator joins two or more than two sting data. The plus
sign (+) is used as the String operator. The act of combining two
stings is called concatenation. The following table shows the use
of Sting Operator.

String Data (A$) ----------- Sting data (B$) ----------- A$ + B$


“Ram” -------------------------- “Thapa” --------------- RamThapa
“50” ----------------------------- “45” ------------------------- 5045
Operator and its meaning
operator meaning example in words
^ power 3^2 3 to the power 2
- negation -23 negative 23
* multiply 1.5 * 8 1.5 times 8
/ divide 12 / 4 12 divided by 4
+ addition 4.2 + 3 4.2 plus 3
- subtraction 9-2 9 minus 2
Mod Modular Division 9 mod 2 Remainder = 1
Working of operator
When all the operators are the same, start at the left and do
the arithmetic one operator at a time. Doing this for the
above:

11 + 31 + 28 + 19 42 + 28 + 19 70 + 19 89
-------- -------- -------
do first do second do third
EXPRESSIONS
An expression is the combination of operators, constants and variables
that is evaluated to get a result. The result of the expression is either
string data, numeric data or logical value (true or false) and can be
stored in a variable. For example, the following are expressions in
QBASIC.
(A + B) > C
A>=B+C
u* t + ½*a*t^2
An arithmetic expression may contain more than one operator. While
evaluating such expressions, a hierarchy is followed. The hierarchy in
arithmetic operations is listed as given below:
a. Exponentiation (^)
b. Negation (-)
c. Multiplication and division
d. Integer division
e. Modular division
f. Addition and Subtraction
Program to Concatenate two String

-----CODE---------------------
CLS
first$ = "Gandalf"
last$ = "The Grey"
name$ = first$ + " " + last$
PRINT first$; " "; last$
PRINT name$
------------------------------
Conditional Programming
In many programs (for example - games), the user has a
choice of what to enter. In this case, QBasic has to check
what the user has typed, and to react accordingly. This can
be done with the IF...THEN command.
IF…THEN…ELSE
This command checks if an argument involving a variable
is true. An argument may look like this: IF a = 15 THEN...
If the argument is true (and a really equals to 15), then
QBasic executes the command you put after the IF...THEN.
Example: IF a = 15 THEN PRINT "OK"
Conditional Programming
If the argument is not true (if a is not equal to 15), QBasic
bypasses this line and goes to the next. In some cases, you
can use the ELSE command, which tells QBasic exactly
what to do if the argument is not true.
IF a = 15 THEN PRINT "OK" ELSE PRINT "It's not 15“
This example means that if a equals to 15, the computer
will show OK on the screen:
But if a is not equal to 15, you'll see: It's not 15
1 CLS
score = 0
PRINT "How many days are there in a week?"
INPUT a
IF a = 7 THEN GOTO 2
PRINT "Wrong answer!"
PRINT "To try again – press 'y'."
INPUT a$
IF a$ = "y" THEN GOTO 1 ELSE END
2 score = 10
PRINT "It's the right answer!"
PRINT "Your score is now"; score; "!"
PRINT "Thanks for playing."
END
Write a program to enter your name, city, country, age
and print them.

CLS
Input " Enter the name ";N$
Input " Enter the city";C$
Input " Enter the country";CO$
Input " Enter the age";A
Print " The name is ";N$
Print " The city is ";C$
Print " The country is ";CO$
Print " The age is ";A
End
Write a program to find the area of the circle.

Cls
Input " Enter the radius " ;R
Let C=22/7*R^2
Print " The area of circle =" ;C
End
Write a program to enter any three numbers, sum and
the average.

Cls
Input " Enter any number" ;A
Input " Enter any number" ;B
Input " Enter any number" ;C
Let Sum = A+B+C
Let Average =Sum/3
Print " The sum=" ;Sum
Print " The Average is " ;Average
End
Write a program to input student's name, marks obtained
in four different subjects, find the total and average
marks.

Cls
Input " Enter the name " ;N$
Input " Enter the marks in English" ;E
Input " Enter the marks in Maths" ;M
Input " Enter the marks in Science" ;S
Input " Enter the marks in Nepali" ;N
Let S=E+M+S+N
Let A=S/4
Print " The name of the student is" ;N$
Print " The total marks is" ;S
Print " The Average marks" ;A
End
Graphics in QBasic
Graphics
QBasic allows you to use the screen on your monitor in
several different modes. These modes are identified by
numbers. By default, QBasic uses mode 0 for the screen.
In mode 0, the screen is a grid of 80 columns and 25 rows.
Your program can write to each of these grid locations.
The top left corner of the screen is the location (1, 1), i.e.,
row 1, column 1. The bottom right corner of your screen is
(80, 25), i.e., column 80, row 25.
Screen
To be able to use a higher resolution for the screen from
your programs, you should use either mode 1 or mode 2.
The SCREEN statement allows you to change the
graphics mode that your program uses.
SCREEN 1 will change the mode of the screen to
graphics mode 1.
Graphics Mode
Graphics Mode 1
In graphics mode 1, the resolution of the screen is 320
columns and 200 rows of pixels. The co-ordinates of the
top, left corner pixel are (0, 0) and the co-ordinates of the
bottom, right corner pixel are (319, 199).
Graphics Mode 2
In graphics mode2, the resolution of the screen is 640
columns and 200 rows of pixels, giving you twice the
number of columns as graphics mode 1. The co-ordinates
of the top, left corner pixel are (0, 0) and the co-ordinates
of the bottom, right corner pixel are (319, 199).
Drawing in Graphics Modes 1 and 2
QBasic has statements to let you draw basic figures on the
screen. When you start a graphics mode with
a SCREEN command, there is a default background
colour (black) and a default foreground colour (white)
assigned to the screen. All that you draw or print from
your QBasic program is in the foreground colour. In
graphics mode 1, QBasic allows you to choose a colour
for your background and a palette of colours for the
foreground with the command COLOR.
Drawing Points
The QBasic command to draw a point is PSET.
PSET (10, 50):- will draw the point at co-ordinates (10,
50) in the foreground colour.
PSET (10, 50), 2:- will draw the same point, but in the
colour whose number is 2 in the current foreground
palette.
The command PRESET erasee the point at the specified
co-ordinates. Actually, it draws the point in the
background colour, hence giving the feeling that the point
was erased.
Drawing Lines
The QBasic command to draw a straight line is LINE.
LINE (10, 50)-(40, 80):- will draw the straight line
joining the points at co-ordinates (10, 50) and (40, 80) in
the foreground colour.
LINE (10, 50)-(40, 80), 0:- will draw the straight line
joining the points at co-ordinates (10, 50) and (40, 80) in
the colour 0.
This way you can use the colour of the background to
"erase" a line.
Drawing Rectangles
The LINE command can also be used to draw unfilled
and filled rectangles.
LINE (10, 50)-(40, 80), , B:- will draw an unfilled
rectangle whose two diagonally opposite corners are (10,
50) and (40, 80).
The last B in the command above, specifies that an
unfilled rectangle is to be drawn. To draw the same
rectangle filled with the foreground colour, replace
the B with a BF in the above command.
Drawing Circles
The QBasic command to draw a circle is CIRCLE.
CIRCLE (10, 50), 5:- will draw the circle whose center is at the
point (10, 50) and whose radius is 5.
CIRCLE (10, 50), 5, 2:- will draw the same circle as above but
in the colour 2.
The CIRCLE command also provides a mechanism for drawing
arcs, i.e., not complete circles. To do this, you have to specify the
starting angle of the arc from the positive X axis and the ending
angle of the arc from the positive X axis. The angles have to be
specified in radians.. For example,
CIRCLE (10, 50), 5, 2, 0, 3.14:- will draw a semicircle in colour
2, above the X axis, with the center at (10, 50) and a radius of 5.

You might also like