Adobe Scan Feb 13, 2023 (3)
Adobe Scan Feb 13, 2023 (3)
QBASIC
w w w w w A
Chapter Focus
to
Till now, we have been using computers Introduction
of QBASIC
run some applications,
the Internet or
access Benefits and Limitations
like if we a QBASIC
Program
play games. How would you Starting
and REM
Statements
or PRINT, CLS
proceed towards designing an application Constants and Variables
some specific
Logical
the hardware so as to perform
y IF-THEN-ELSE Statements
skills and
It provides good
logic base,
a
move over
to higher level of
concepts to
Visual Basic, C, C+t and Java.
programming using
Fe
91 Stcarting QBASIC Fig 92 Welcome screen of QBASIC
The main menu of the QBASIC editor
window has the following menus-File, Edit, View,
Search, Run, Debug and Options
(Fig 93).
a SE KS x
File Edit ie Search ebes Opt ien Help
htitled
Fig 93 Main menu options of QBASIC
Tostart typing a new program, click on File> New Program (Fig. 9.4).
The PRINT statement helps to display a message or a value on the Neu Frogran
pen Progran.-
standard output devicethe monitor. Save S.
rint.
The general syntax of this statement is as follows: E it
133
The simplest form is just the PRINT
with all other parameters excluded. This
prints a blank line and shifts the cursor
to the next line. Info Hub
If the expression list is included, then ?is the shortcut for the Print statement.
QBASc
converts? into a Print command automatica
it prints the value of the list.
The expression list can contain either
o
Any QBASIC program must end with the END
a number or a numeric expression or a statement.
A program can also be executed by
string (an alphanumeric combination typing
Shift and F5 keys together.
which is generally used as a display A printed number is always followed by a spocs
message). A positive number always nasa leading spc
I f i t contains a number, then it whereas a negative number is preceeded by
displays
the number directly. sign.
If
the message to be displayed is wider than the
If it contains a numeric expression, width of the screen, then the message is primted
then the computed value is displayed. on to the next line during display
If it contains a string expression, then
the expression should be enclosed within double quotes.
Example: Type the following PRINT commands in the editor window:
PRINT 3
PRINT 3+5-2
END
Run De bug Options
Now, to see the output of these commands, you
Start Shift F
have to run this program. To run the program, click
on
estaret
Continue FS
Run> Start (Fig. 9.5).
Make E E File...
On clicking Start, you get the following output:
QBASICdivides each screen. The symbos ols "" and "" act to join to or nore prind itens
statement forc print line
forces the next item into c zones of
to sof 14spaces cach. A comma () afler the PPISNT
PRINT state be
atement, on the other e displayed in the next zone. The senicolon C) afier a
displayed
previous one. ifIf the
the PRINT a hand
PRINT statement forces the next
rces ner iten to be printed immediately afer the
stransferred to the ends witho a comma or a semicolon, thes ontr
beginning of thedswithout
he
icolon
semicolon ends the list
of
next line for further
ne for further display,
display 1ff either
e a comna o
same row. expressions, then the successive
suco PRINT statement continues in the
Example:
The stateme PRINT 1, 2,
3, 4, 5, 6
3
will display
4
Ifaprogram does not give a desired output, then it must be checked for errors. errorsSyntax
are the most common
types of errors. Syntax errors arise when program codes are not written
properly. A misspelled word in a program keyword results in a syntax error. In QBASIC, a
program is read and executed line-by-line. It means that if an error is present on line 10 of a
program, the previous nine lines are read and executed without any
dificulty.
CLS COMMAND
When successive output of a program gets generated, it appears only after the previous
output.
Thus, it can becomeconfusing as to which output has been generated by the current execution
135
of the program. The CLS command to the aid in these
comes
situations. It stands for
CLear
Clear
Screen and helps us to clear all the
previous contents of the output
screen. sh
So, fresh
output can
output Ca
be generated the codes the
henceforth. The CLS can also be used between so that previous
output is erased and the future output is displayed on a fresh screen dependin ding on the
requirement.
The general syntax of this command is:
CLS [optional parameters) .
The optional parameters are beyond the reach ofthe current syllabus.
REM STATEMENT
REM stands for REMarks which are sometimes required to be gven within a program for
Detter understanding of the logic to the reader. The comments following the REM statement
are not interpreted by QBASIC. The limitation of this statement is that it should start with the
it exceeds the current
line,
Keyword and the remark should end within the current line. If
V
then the corresponding line should also start with the REM keywora.
II a remark is to be mentioned within the same line, just after the end of the current statement,
then the (apostrophe) symbol should be used instead of the REM keyword. The symbol
can also replace the REM keyword for a full line remark.
to state what the current
The REM statement may also be used at the beginning of a program
date, time and other
program iS supposed to do. The name programmer, module number,
ofthe
using the REM
details about the program are generally written at the beginning of a program
statement.
Example:
of a PRINT statement
REM This program displays the use
a numbe
PRINT 5 'Displaying
END
Constants are
the execution of a program. There
which remain unchanged during
These are fixed values constant
constants and numeric
constants-string
two types of
basically
vit ble
alphanumerie
characters
enclosed
wtt
s e q u e n c e of
constants: They refer to character set excep
a
Srng
value ofthe QBASIC
A string constant may hold any of 32767
charactcts
quotes.
Enter). It can be a sequence of a
maximum
(Carriage returm or
c a n be
adl
negative
numbers. They oi
refer to either positive or
Numeric constants: They
classified into the following types.
These a r e user-allocated
names given to memory
varables.
variables and numeric
or a string
constant. They As
a character For example,
String variables: They may hold either variable is NULL.
ofa string
afterthevariable
name. The default value detault
the S sign
constants
mentioned above. The
N u m e r i c variables: They may hold any ofthe numeric
sum.
variable is zero. For example,
value of a numeric
Variables
Rules for Naming variables:
mind while naming
rules must be kept in
The following
to 40
characters long. like_(underscore).
assignment
known as
the
LET STATEMENT variable. So, it is also
constant to a
a
statementhelps
to assign
The LET to left.
works from right
statement,
It follows:
as
statementis
LET
of the v a r i a b l e >
of the
variable on
constant name
on
[LET] and the variable
or a
is
reserved
reterred by the (either a
string
memory
space memory
space the data type
Here, a to the statements,
allocated
LET
side is =. In all the
nght-hand operator 137
assignment sides.
both
side ofthe should be the
same
on
number)
Example
LuET AS
assigns the value 5 to the numeric variable A.
variable AS
A cOMPUTRR COMPUTER to the string
assigns the string
C45
assigns the value 4.5 to the numerie vara
Now look at the following code segment
LET A5
as A 1s
assigned the value 5 and B is assigned the value of A which is also.
ne cONST
keyword allows you to declare a variable as a constant such that the value of that
variable may not be
changed throughout the program
Example:
cONST A=5
PRINT A
LET A=6
PRINT A
Example:
LET A=5
LET A=6
PRINT AA
This code will display 6 and not 5 as the previous value of 5 is overwritten with 6.
138
INPUT STATEMENT
IUsing the LEI Statchieht, a value
can be
given toa variable by
entering it in the coding pa
static
(sta input). So, to change the
value of the variable, the program itself has to be
for a program to be iiexible, the
code should not change changed. But,
every time the value
anged. S0, dynamic ipput must be taken needs be to
he INPUT
during the execution of program. This is
a by done
statement.Qt allows to write interactive programs by accepting keyboard
during the program execution It temporarily halts pu
from the user. the program and waits for the entry ot daa
the variable. So
" "
displayed.
Table 9.1 shows various examples of INPUT statement.
Message Purpose
Statement displayed on a
?
to accept a
numeric
T0 GRMS
RDM ALGo1 R226 KILOGRS
cLa CLS
zG
LET FIRS73 IMPOT zNTZR
KILOGRAMS,
CLS CLS
PRINTTHE SIMPLE
INTEREST IS", SI LET S = TEMP
END
CLS
GOTO E
Info Hub
without
Programming using the GOTO
10: INPUT "ENTER B", B statement
100: PRINT B
END
In this program, first the value of A is accepted. The GOTO E statement then transfers the
control where the value of A is displayed. Next, the GOTO 10 statement is encountered which
branches to the line where B is accepted. Finally cOTO
again uncondtionally 100 transfers
the control to the PRINT B statement where the value of B is displayed and the program
terminates.
OPERATORS
Operators arecommands or symbols that perform specific arithmetic or logical operations on
data values.JThe data values on which an operator works are known as operands.An operator
can be unary (I operand) or binary (2 operands). The operators help to mathematically or
logically join group of operands to form an expression. An expression can also be a string or
numeric constant, a variable or a computed value obtained by combining constants, variables
and other expressions with operators. Operators in QBASIC can be of five types:
String operators
We will be studying only about the first three types.
Arithmetic Operators used for performing mathemati
numeric data
values and are
thmetic operators work on %. Table
9
gives a detailed explanation to the
Calculations. Examples include t,- / ,
functions of various arithmetic operators.
Table 9.2 Arithmetic operators
Example Result
Operator Purpose
PRINT 2+3
Addition
PRINT2-3
Subtraction
PRINT 2*3
Multiplication PRINT 5/2 2.5
Quotient of division
PRINT 52 2
division
Quotient of integer
PRINT 23
Exponent PRINT 5 MOD 2
MOD Remainder of division
Examples:
R = 2 + 3 / 4 * 5 - 6 willdisplay-.25 and R = (2 + 3 ) / 4 * (5 6)
will display-1.25.
I fan expression has and -side-by-side then - is executed first. For example, PRINT 5 - 2
Relational Operators
Relational operators are used for comparing two values. The result of this comparison can be
zero or non-zero. Zero denotes false while non-zero denotes true. This evaluation can be used
in a decision-making statement to decide the flow of control. Table 9.3 lists the various
relational operators.
142
Tble 9.3 Relational operators
Reational Operato Relation Expression
Equality X-Y
Inequality XY
Less than X«Y
Greater than X>Y
Less than or equal to X-Y
Greater than or equal to
The order of execution of relational operators is:,>,<, , ,
Logical Operators
Logical operators are operators that logically combine and evaluate a result according to
some pre-detined logical operations. They generate a true or a false output that may be used
for decision-making statements. Some of the logical operators in the hierarchical order are
Table 9.4
Logical operators
Result
Operator Meaning
NOT Inverse Inverts the operand.
true.
Retums a true value only when both the operands are
Examples:
PRINT 5>3 AND 2<4 will give an output-1.
PRINT 5>3 AND 2>4 will give an output O0.
-1.
PRINT 5>3 OR 2>4 will give an output
OR 2<4 will give an output-1.
PRINT 5>3
algorithms
and
decision-making
to control
on This
the chapter
While studying
program.
e x e c u t i o n of
a statements.
the
d e c i s i o n s during
I F - T H E N - E L S E
the
incorporated
using the
c a n be
program
lt is a control flow statement that allows conditional
branching based on whether the e
is true or false. It has two basic forms: ession
1. Singlelinestatement
2 IF-THEN-ELSE-ENDIFblock
Single-line Statement
The general syntax of the single line statement is:
IF boolean expression THEN true s tmt [ELSE false_stmt]
where boolean expression is a valid expression that returns either true
falese
or
Example:
IF A>B THEN PRINT A ELSE PRINT B
144
IF-THEN-ELSE-ENDIF Block
The general syntax of the block IF-THEN- ELSE is
IF boolean
expression1 THEN
[statement block-1]
(ELSEIF boolean expression2 THEN
[atatement block-2]
ELSE]
[statement block-n] ]
END IF
where
DoOLean_expression is a valid expression that returns either true or false.
statement block consists of
any number of statements on one or more lineS.
Here, the program starts by
evaluating the first boolean expression. If it is true then the
statements following the THEN
are executed and
block. If the first
the control is transferred outside the total
expression is false, the control is transferred to the ELSEIF part where thee
second expression is evaluated.
Again if this is true the statements following the THEN part is
evaluated. This process continues
and if none of the conditions are true then the statements
following the ELSE part is executed. Finally, the block ends with the END IF statement
signifying the end of IF-ELSE block.
The following points need to be noted while handling IF-THEN-ELSE block
The ELSE and ELSEIF blocks are statements
both optional. Many ELSEIF clauses can be present
within an IF block.
QBASIC scans the next word after the THEN keyword. If anything other than a
comment appears after THEN, the statement is treated as a single line IF statement.
A block IF statement must be the first statement on a line. The ELSE, ELSEIF, and
END IF parts of the statement can only have a line number line label
or preceding
them. The block must end with an END IF statement.
Example:
***********
IF A< 10 THEN
INPUT Enter a number >0 but <
10000:", A DIG 1
DIG = 2
PRINT "WITHIN RANGE"
ELSEIF A<1000 THEN
ELSE
DIG=3B
PRINT OUT OF RANGE"
ELSE
END IF ****sa******
DIG=4
END IF
a Single Line Statement
Code
Ovantages of the IF-THEN-ELSE Block over single line
IF-THEN-ELSE block s tatements have the following advantages
over
statements
They are more flexible in nature.
IF-THEN-ELSE block statements
More complex conditions can be tested using
IF-THEN-ELSE block statements.
Onger statements may be written using
Block statements lend more logic to a program.
A program written using IF-THEN-ELSE block statements is easSier to read, maintain
n
and debug.
REM GRADE
CLS
M2,
ZNPUT AcCEPT THE MRKS Or s sUBJECTS,
M
M M3,
LET GRADE(M1+M2 M3 M4) 4
IF GRADE<50 THEN PRINT FAIL ELSE PRINT PASS
Info Hub
END
It is a
g00d programming
Code: 2 practice to include the CLS
and REM statements in a
REM DIVISIBLE program as it adds to the
6
Code: 4
REM UPTO 800 HORSE POWER TAX IS 1000 LET TAX = 2000
CLS END IF
INPUT ENTER HORSE POWER OF CAR", HPi PRINT THE TAX TO BE PAID IS RS
TechBytes
Constants: fixed values that remain
Program: a set ofinstructions which direct of a program
a computer to carry out a particular task unchanged during the execution
.Variables: user-allocated names given to
.BASIC: beginner's All-purpose Symbolic hold varying
locations which can
Instruction Code memory
values
Syntax: rules for writing a program and the to logically
commands therein Operators: commands that help
or arithmetically
combine a group of values
Bug: an error in a program
which the operator
.Operands: values on
Bits to Remember
com-
English-like
is good for beginners and has an easy
QBASIC programming language understand.
to implement and
mand syntax which is easy or a value on the monitor.
statement helps
which to display a message
PRINT is a QBASIC
? is the shortcut for the PRINT command.
during the execution of a
program.
helps to clear the
screen
CLS
comments within a program.
T h e REM statement helps to add
REM s t a t e m e n t s in a program.
should include the CLS and the
One denotes the end of a program.
statement QBASIC program that
of any
E n d is the last
and Numeric.
variables are of two types String
Constants and
initialise a variable.
T h e LET statement can
be used to
changed during the
value to a variable such that it may not be
to assign a
The CONST keyword helps
program. variables during runtime.
statement can
assign the values to
be used to
The INPUT operators.
and Logical are the major types of
Arithmetic, Relational conditional branching statements.
to implement
help
IF-THEN-ELSE statements