QBASIC Statements
Introduction
A statement is a set of
instructions written using keywords or commands of QBasic. Every programming
language used keywords as a statement with certain rule (syntax). The keywords
have specific meaning to the QBASIC programming. There are different types of QBASIC statements. They are as follows
a) Assignment statement
b) Declaration statement
c) Input/output
statement
d) Control flow
statement
a. Assignment
statement
The statement which is used to assign the value
to the variable is known as assignment statement. LET, SWAP and READ…DATA are
the commonly used assignment statements.
LET Statement
It is an assignment statement. The value can be assign in the variable with the help of LET statement. LET is an optional statement.
Syntax: LET <variable> = <expression>
Where,
Variable : is the name of the numeric or string variable.
Expression : is the numeric or string value that the
variable will hold
Also Read: Array in QBASIC
Example 1: Program to calculate the simple
interest |
Example 2: Program to display the name,
address and age of the student |
CLS REM program to calculate and display the simple
interest LET p=10000 LET t=5 LET r- si=(p*t*r)/100 PRINT “Simple interest”;si END |
CLS LET a$=”Ajaya Sharma” LET b$=”Dhangadhi” LET c=20 PRINT “Name”,”Address”,”Age” PRINT a$,b$,c END |
SWAP Statement
The SWAP statement is used to interchange the
values between two different variables of same type.
Syntax: SWAP <variable 1>
, <variable 2>
CLS REM this
is the test of SWAP statement LET x= 20 LET y= 30 PRINT
“It is Before Swapping” PRINT x,y SWAP x,y PRINT
“It is After swapping” PRINT x,y END |
Output:
It is Before
swapping 20 30 It is After
Swapping 30 20 |
Here variable a and b
will change their value to each other.
READ…..DATA Statement:
READ: Read the values (numeric or string) and
assigns them to variables.
DATA: It specifics values to be read by subsequent
READ statement.
Syntax:
READ variable list
…………………….
…………………….
DATA constant list
Example:
REM this is the test of READ……DATA statement
READ n$, a$, a
PRINT “Name=”;n$
PRINT “Address”;a$
PRINT “Age”;a
DATA Ram, Dhangadhi, 16
Restore Keyword
Restore keyword is used
in READ…DATA statement. RESTORE keyword allows data for the reused. Once a DATA
element has been read, it cannot be read again until either the program
restarts or the RESTORE statement is used.
Syntax: RESTORE [line
number/label]
CLS REM
Program to test the RESTORE statement DATA
5,10 READ a,b RESTORE READ c,d PRINT
a,b,c,d |
Output: 5 10 5 10 |
b. Declaration statement
The statements which are used to declare the
variables or constants are known as declaration statements. DIM, CONST and REM
are the declaration statements.
DIM Statement
In the type of explicit variable declaration DIM
statement is used.
DIM declares simple variable as well as array
variable with their data type.
Syntax:
DIM variable
[subscripts] [AS type]
DIM variable1[subscripts]
[AS type], variable2[subscripts] [AS type]
Example:
DIM age AS integer
DIM name AS string, age
AS integer
CLS
REM sample of DIM
statement
DIM n AS STRING
DIM a AS INTEGER
CLS
INPUT “Enter your
name”;n
INPUT “Enter your age”;a
CLS
PRINT “Name”;n
PRINT “Age”;a
END
CONST Statement
CONST declares symbolic constants to use in
place if numeric or string values.
Syntax: CONST
constant name = expression
Example:
CLS
REM sample of CONST
statement
INPUT “Enter radius”;r
CONST pi=3.146
A=pi*r^2
PRINT “Area”;a
END
REM Statement
It is a BASIC declaration statement that allows
explanatory remarks to be inserted in a program. It does not execute in the
program. It is also called non-executable statement. The alternative of REM is
(‘)
Syntax: REM
<remarks>
Or,
‘<remarks>
Example
CLS
PRINT "Some text"
‘ This text is ignored.
c. Input / Output statement
Input/Output statements are used to get user
input and display the processed result to the outside world (users). CLS,
INPUT, LINE INPUT, PRINT, PRINT USING, LPRINT, LPRINT USING are the input /
output statements.
CLS Statement
The CLS statement clears the screen. If you
write CLS statement on the middle of the program than you cannot see that part
because it clears the screen.
Syntax: CLS
INPUT Statement
INPUT statement inputs or accepts data from the
keyboard entry and stores to list of variables during the time of program
execution. Variables separated by comma “,”. INPUT statement brings
question mark “?” on the screen. INPUT statement causes the program to pause
and wait for data. Unless any response is entered the control does not move the
next line.
Syntax: INPUT [“prompt string”] [;|,]
variable list
Prompt string is used to display on the screen to tell the user of the
program what to enter at the keyboard.
Variable list is one or more variables used in the program are separated
by comma.
Example:
CLS
INPUT “First Number:”;A
INPUT “Second Number:”;B
Sum=A+B
PRINT “The Sum is:”;Sum
END
LINE INPUT
This statement is used to read an entire line of
input from the keyboard and store it in one string variable. It inputs data
ignoring delimiters.
Syntax: LINE INPUT [“prompt string”] ;
string variable
When the INPUT statement
is used to input multiple values from the user, commas must be used to delimit
the input. If there are two variables being requested by INPUT, a comma must
separate the two values, otherwise an input error occurs. If a comma is used
when there is only one variable being inputted, there will be an error as well.
With LINE INPUT, only one variable may be inputted, but commas do not cause
errors when used. The fact that LINE INPUT will accept commas as part of the
text string is the only difference between LINE INPUT AND INPUT.
Syntax:
LINE INPUT "Type a
sentence:"; sentence$
Example:
CLS
LINE INPUT “What is your
Name?”;N$
PRINT N$
END
INPUT #
This statement is used to retrieve data items
from a sequential data file and store them into a list of variables.
Syntax:
INPUT # [file serial number, variable 1,
variable 2, …]
LINE INPUT #
This statement is used to retrieve an entire
record from a sequential file and store it into one string variable.
Syntax:
LINE INPUT # [file
serial number, string variable]
INKEY$
It is an I/O function
returns a one or two byte string containing a character read from the keyboard.
A null string is returned if no character is waiting there. A one character
string contains the actual character read from keyboard that reads a character
from the keyboard.
Example:
PRINT “Press any key …”
DO
LOOP WHILE INKEY$ = “ ”
Note: This example
pauses until the user presses a key.
PRINT Statement
PRINT statement provides
output on the screen. An alternative of PRINT statement is question mark (?)
Syntax:
PRINT [expression list]
[ , | ; ]
Or,
? [expression list] [ ,
| ; ]
Where.
Expression list: It is the list of one or more numeric or
string expression.
Comma (,) is used to produce output on the same line after some space.
Semicolon(;) specify the next output to be displayed on
the same line after a space immediately.
Example: |
Output: |
CLS REM test of PRINT statement PRINT “Wel-come to QBASIC” PRINT PRINT “====================” PRINT “AVN, Dhagadhi” LET a$=”RAM” LET b$=”KTM” ? a$, b$ END |
Wel-come to QBASIC AVN, Dhangadhi RAM KTM |
PRINT USING Statement
This statement is used to print strings or
numbers using a specified format to the screen.
Syntax::
PRINT USING “format
string” ; expression
Example:
PRINT USING “####.##” ;
123
PRINT USING”##.#” ;
12.275
Output:
123.00
12.2
Formatting character for numeric data
Character |
Result |
# |
Digit position |
+ |
Position of number’s sign |
. |
Decimal point position |
- |
Prints trailing minus sign for negative numbers only |
** |
Fills leading spaces with * |
$$ |
Prints leading $ |
**$ |
Combines ** and $ |
^^^^ |
Prints number in exponential format |
Formatting character for string data
Character |
Result |
& |
Prints entire string |
! |
Prints only the first character |
\ \ |
Prints first ‘n’ characters, where n is the number of
blanks between slashes + 2 |
LPRINT and LPRINT USING
Statements:
These statements
function in the same way as the PRINT and PRINT USING statements except that
output goes to the line printer and file number option is not permitted. .
LPRINT will accept the same parameters that PRINT will, such as variables
(numbers and string expressions), static text, and multiple combinations of
each.
Also Read:
Display the difference of input two numbers
C program to calculate the sum of n integer numbers using function.
No comments:
Post a Comment