QBASIC
QBASIC
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.