0% found this document useful (0 votes)
100 views51 pages

Introduction To QBASIC

Notes of qbasic

Uploaded by

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

Introduction To QBASIC

Notes of qbasic

Uploaded by

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

Introduction to QBASIC

QBASIC stands Quick Beginners All purpose Symbolic Instruction Code which is a
popular high level language for beginners developed by Microsoft corporation
originally developed by Thomas Kurtz and John Kemeny. It is very easy to use and
understand so, It is popular among students.
Features of QBASIC
1. It use simple English like structure.
2. It has user friendly interface.
3. It automatically checks syntax error.
4. It has wide range keywords.
5. We can use both mouse and keyboard on its interface.
6. It capitalize keyword automatically.

Software Breakdown
Data types used in QBASIC
Numeric data: It consist of numeric values i.e. numbers 0-9 and its combination.
For eg; 45,12,1
String data: It consist of alphanumeric value i.e combination of letters and
numbers. For eg; “global” , “pokhara7”
Variables: Those entities which holds either numeric or alphanumeric (string)
values and changes its value throughout the time of program execution is called
variables. There are two types of variables.
Variables in QBASIC
Numeric variable: Those entities which holds only numeric values and changes
its value throughout the time of program execution is called numeric variables.
Eg, age = 17, i =2 , wardno = 12
String variable: Those entities which holds only alphanumeric values and
changes its value throughout the time of program execution is called string
variables.
Note: String variable should end with ‘$’ sign and its respective value should be
enclosed within double quotation “ “.
Example: school$ = “global”, fname$ = “Krishna” , address$ = “Pokhara”
Rules while writing variables name:
1. Variable name should be 1 to 40 character long.
2. Blank space in variable name is not allowed.
For eg, ward no = 12 (Invalid) , wardno = 12(valid)
School name$ = “global” (Invalid)
Schoolname$ = “global” (Valid)
3. Any special symbol except $, #, !, %, & symbols are not allowed in variable
name.
Ward*no = 13 (Invalid) , ward$no = 14 (invalid)
4. Keyword cannot be used as a variable name.
Print =3 (Invalid)
Cls =8 (Invalid)
Name =4 (invalid) if,else,while,for
5. Variable name should not start with number.
123abc = 2 (invalid), abc123 =2 (valid)
123fname$ = “global” (invalid)
fname123$ = “global” (valid)

Constant: Those entities which holds either numeric or alphanumeric (string)


values and doesn’t changes its value throughout the time of program execution
is called constant. There are two types of constant.
Numeric constant: Those entities which holds only numeric values and doesn’t
changes its value throughout the time of program execution is called numeric
constant.
Eg, age = 17, i =2 , wardno = 12
String constant: Those entities which holds only alphanumeric values and doesn’t
changes its value throughout the time of program execution is called string
constant.
Note: String variable should end with ‘$’ sign and its respective value should be
enclosed within double quotation “ “.
Example: school$ = “global”, fname$ = “Krishna” , address$ = “Pokhara”
Operators: The special sign or symbol that we use in our program to perform
some specific functions or operation are known as operators. Types of operators:
Arithmetic operators: It performs mathematical calculations
+ , - , * , / , \ , MOD, ^
5+2 =7
5/2=2.5 (exact division)
5\2 =2 (integer division)
5 MOD 2 = 1 (Modulus division)
5^2 = 25
Relational operator: It helps to compare different operands
>, <, >= , <= ,= , <> (not equal to)
Logical operator: It helps to make logical comparison.
AND, OR, NOT
String operators: It helps to add two or more string. The process of adding two or
more string is known as string concatenation.
+
C$ = a$+b$
QABSIC Expression
a=r2. →→ a=r^2
a=l.b →→ a =l*b
i =p*t*r100p*t*r100 →→i = (p*t*r)/100
QBASIC statements and program
CLS (Clear Screen)
PRINT Statement: Display information on screen.
Syntax:
PRINT “Sample text”
OR
age = 13
PRINT age
PRINT “Age is =”;age

INPUT Statement: It helps to take input from the user.


Syntax:
INPUT variable_name
INPUT “Any text”;VN
Q1) Write a program to calculate area of rectangle. [A=l*b]
CLS
INPUT “Enter length”;l
INPUT “Enter Breadth”;b
A=l*b
PRINT “Area is: ”;A
END
Q2) WAP to calculate simple interest [i=(p*t*r)/100]
CLS
INPUT “Enter principal”;p
INPUT “Enter time”;t
INPUT “Enter rate”;r
i=(p*t*r)/100
PRINT “Simple interest is: ”;i
END
Q3) WAP to calculate volume of cylinder. [v=pr 2h]
CLS
pi = (22/7)
INPUT “Enter radius”;r
INPUT “Enter height”;h
v = pi*r^2*h
PRINT “Volume is”;v
END
Q4) WAP to calculate average of three number. [Av=(a+b+c)/3]
CLS
INPUT “Enter first number”;a
INPUT “Enter second number”;b
INPUT “Enter third number”;c
av=(a+b+c)/3
PRINT “Average is: ”;av
END
Q5) WAP to convert Nepali rupee into American dollar. [1$ = 118Rs]
CLS
INPUT “Enter Nepali rupee”; N
A = N/118
PRINT “America dollar”; A
END
Q6) WAP to convert Celsius temperature into Fahrenheit. c−0100=f−32180c-
0100=f-32180
18*c = 10*f-320
18*c+320 = 10*f
f = (18*c+320)/10
C = (10*f-320)/18
CLS
INPUT “Enter Celsius”; c
f = (18*c+320)/10
PRINT “Fahrenheit temperature”; f
END
IF Statement:
1. IF statement
2. IF ELSE statement
3. ELSEIF statement
1) IF statement
Syntax:
IF (condition) THEN
Block of statements//
END IF
Program example:
CLS
INPUT “Enter your age”; a
IF a>18 THEN
PRINT “You are eligible to vote”
END IF
END
CLS
INPUT “Enter your percentage”; p
IF p>=40 THEN
PRINT “You are pass”
END IF
END
2) IF ELSE statement
Syntax:
IF (condition) THEN
Block of statements no.1//
ELSE
Block of statements no.2//
END IF
Program example:
CLS
INPUT “Enter your age”; a
IF a>18 THEN
PRINT “You are eligible to vote”
ELSE
PRINT “You are not eligible to vote”
END IF
END
CLS
INPUT “Enter your percentage”; p
IF p>=40 THEN
PRINT “You are pass”
ELSE
PRINT “You are fail”
END IF
END
Q1) Write a program to input 2 different numbers and find the greatest number.
CLS
INPUT “Enter two number”; a,b
IF a>b THEN
PRINT a; “is greatest”
ELSE
PRINT b; “is greatest”
END IF
END
Q2) Write a program to check whether the given number is odd or even.
CLS
INPUT “Enter a number” ; n
a = n MOD 2
IF a=0 THEN
PRINT n; ”is Even”
ELSE
PRINT n; ”is Odd”
END IF
END
Q3) Write to check whether the given number is exactly divisible by 5 and 7.
CLS
INPUT “Enter a number” ; n
a = n MOD 5
b = n MOD 7
IF a=0 AND b=0 THEN
PRINT n;” is divisible by both 5 and 7”
ELSE
PRINT n;”is not divisible by both 5 and 7”
END IF
END

4.2.2 Variables and Constants


(a) Data and Data Type
Data is any value (raw or unprocessed) given to the program for processing.

Basically, QBASIC supports two types of data.

i) String - any alphanumeric values and valid


symbols ii) Numeric - only numbers (0-9)

QBASIC supports further sub-types of numeric data:


Non-floating-point numbers from -32,768 to 32,767
Integer
Examples: -34, 987, 0
Non-floating-point numbers from -2,147,483,648 to
Long Integer 2,147,483,647
Examples: 340035, 7, -24, 0, -127010, 14, 8, -10
Floating-point numbers from -3.37x10^38 to
3.37x10^38 (up to 7 digits)
Single-Precision
Examples: 3.45, 31.4567, -35.23, 0.45, -3.15, 890, -
98
Floating-point numbers from -1.67x10^308 to
1.67x10^308 (up to 15 digits)
Double Precision
Examples:
723661.90226, -128.78, 185321.093423, 2345544,
-123456897
Character Set
Characters from the QBASIC character set are combined to form keywords,
variables, operators and labels. They are further combined to form instructions
that make up a program. The QBASIC character set consists of all alphabetic
letters (both uppercase and lowercase), all digits and some special characters
having special meaning to the basic translators.
They are suffixes used at the end of variable names to
#, $,!, %,&
specify the type of variable.
+,-,*,/,\ They are mathematical operators.
<,>,= They are relational operators.
(,) They are parenthesis.
: It is used to write multiple statements in a single line.
They are separators and are used to separate the
,;
arguments in input and output statements.
^ It is exponentiation and is used to raise power to.
? It can be used in place of a PRINT statement.
‘ It is used to mark a line as a comment instead of a REM
statement.
. It is a decimal point and is used in numbers.
A character denotes any alphabet, digit or special symbol used to represent
information. The below table shows the valid alphabets, numbers and special
symbols supported by QBASIC.
A,B,C, …., X,Y,Z
Alphabets
a,b,c, …., x,y,z
Digits 0,1,2,3,4,5,6,7,8,9
, =, +, -, _, *, /, \, ^, %, $, &, !, #, “, ;, ., ‘, ,, ?, <,
Special Characters
>
Words
These are mainly the following two types:

Reserved words: Words having a special meaning to QBASIC such as the name
of the statements, library functions etc. are termed as reserved words or
keywords.

E.g. Statements PRINT, INPUT, LET, CLS etc are reserved words and should not
be used as the name of the variables or functions.

User-Defined words: These are the words used in statements as variables or


user-defined functions. These variables may have different meanings in different
programs.
Example: LET NAME$=”STEVE”

Here, LET is a keyword and is a reserved word while NAME$ is a variable and is a
user-defined word.

(b) Variable
A variable is a small amount of computer memory (RAM) that has been given a
name. It stores the value of a particular data type while the program is being
executed. The location of a variable in RAM is called the "address."

Rules for naming variables


i) Each variable name must start with an alphabetic character though it can
contain alphabetic and numeric characters and periods. ii) The variable name
cannot be a BASIC keyword. iii) Variable names can be up to 40 characters long.
iv) Each variable name is followed by a symbol. The symbol refers to the data
type it might hold.
Few Examples:
Variable Name Valid Invalid Reason
1BC × The first letter is a numeric
character
LET × Keyword

Starts with alphabets and $


ABC$  sign indicates the data type
string.
BC+ × The invalid symbol is used

(c) Constants
A constant is an entity that doesn’t change, whereas a variable is an entity that
may change.

Example:
a=4

4 a

Since the location whose name is a can hold different values at different times,
so a is known as a variable. As against this, 4 does not change, hence is known
as constant.

Types of Constants
Numeric Constant
It has only numbers from 0 to 9. For example: 45,456.35, -44 etc.

String Constant
It accepts both numbers and alphabets (alphanumeric values). Examples:
“Nepal”, “45.67”, “A45” etc.

Symbolic Constant
A constant whose value remains the same through the whole program is called a
symbolic constant. In QBASIC, CONST statement is used to declare a symbolic
constant.

(d) Variable Declaration


QBASIC supports two types of variable declaration.

i) Implicit Declaration
In this type of declaration, the type of variable is defined as certain special
declaration characters. We use %,&,!,# and $ symbols to declare the variable
type implicitly.

Example:
A$=”Nepal”

Here, we use $ symbol after the variable name A, which indicates the type of
variable is string.

INPUT “Type any number “;B%

Here, we use % symbol to declare the variable type integer.

ii) Explicit Declaration

In this type of declaration, we use DIM …AS statement.

DIM variable AS type


It defines the type of variable and we can use the following keywords.

INTEGER, LONG, SINGLE, DOUBLE, and STRING Example:


DIM A AS STRING, B AS INTEGER
CLS
INPUT "Type your name "; A
INPUT "Your roll number "; B
C$ = MID$(A, 1, 1)
PRINT "First character of your name is "; C$
PRINT "Your roll number is "; B
END
Here, in the above program, the declaration characters ($ and %) are not used
for the string-type variable A and integer-type variable B. So, A and B are
defined explicitly using DIM…AS statement and C is defined implicitly.

Data types and variable declaration in QBASIC


Data Type Declaration Byte Length Maximum Minimum
String $ [STRING] 1 byte/char 32,767 chars 0 chars
Integer % 2 bytes 32,767 -32,768
[INTEGER]
Long & [LONG] 4 bytes 2,147,483,64 -
Integer 7 2,147,483,64
8
Single
Precision ±3.402823 ±1.401298 E-
! [SINGLE] 4 bytes
(up to 7 E+38 45
digits)
Double
±1.7976
Precision ±4.940656 D-
# [DOUBLE] 8 bytes 931
(up to 15 324
D+308
digits)

4.2.3 Operators, expressions and operands


(a) Operators
Operators are the symbols used to represent a particular operation. QBASIC
supports three types of operation: arithmetic, relational and logical operators.

i) Arithmetic Operators
These operators represent arithmetic operations such as addition, subtraction,
multiplication etc.

Operator Operation Example Result


^ (caret) Exponentiation 10^3 1000
/(slash) Division 10/3 3.333333
\(back slash) Integer Division 10\3 3
* (asterisk) Multiplication 10*3 30
+ (plus) Addition 10+3 13
- (minus) Subtraction 10-3 7
MOD Modular Division 10 MOD 3 1
Sequence of operation in BASIC (Hierarchy/Precedence of operation)
The hierarchy of operation in BASIC is commonly called as the PEDMAS rule
where;
P Parenthesis ()

E Exponentiation ^
/, \,
D Division
MOD
M Multiplication *

A Addition +

S Subtraction -
 Multiplication and division has the same hierarchy and hence whichever
comes first from left to right has to be dealt first.
 Similarly, addition and subtraction also have the same hierarchy.
 E.g.: In the expression A*B/C; A will be divided by B first after which the
result will be divided by C.
 Similarly, in the expression X-Y+Z, Y will be subtracted by X after which Z
will be added to it.

ii) Relational (Comparison) Operators


Relational operators are used to comparing two values and return either true or
false.

Assuming that the values of a and b are 6 and 3 respectively.


Operator Operation Example Result
= Equal to a=b False
< Less than a<b False
> Greater than a>b True
<= Less than or equal to a <= b False
>= Greater than or equal to a >= b True
<> Not equal to a <> b True
iii) Logical Operator
Logical operators are used to combining two or more relational expressions and
return a single value as True or False after evaluating them.

AND Operator
An AND operator returns True (1) at its output only when all of the inputs are
high (1).
False (0) is returned when one or more of the inputs are low (0).

OR Operator
An OR operator returns True (1) at its output when one or more of its inputs are
high
(1). False (0) is returned only when all of the inputs are low (0).

NOT Operator
The NOT operator (Inverter) performs the logic function called inversion or
complementation. If the input signal is low then the high output signal is
obtained and vice versa.

iv) String Operator


+(plus) → Concatenation

Concatenation is the process of joining two string expressions.


A$ = “Visit”
The value of C$ is:
B$= “Nepal”
VisitNepal
C$=A$+B$
(b) Expressions
An expression is a set of variables and constants having a definite value
(operands) in which various mathematical operators are used to show the
relationship between the variables and constants.

Example:

a+b)^2-2*a*b
X$=MID$(A$,N,1)

Types of expression
Algebraic Expression
The expressions that are used in mathematical and other fields are called
Algebraic expression.

Example: S = ut + at2
QBASIC Expression:
The expressions that can be used in QBASIC programs are called QBASIC
expressions.

Example: S = u*t + (a*t^2)/2

Boolean Expression
Any expression that yields true or false value (0 or 1) is known as a Boolean
expression.
The conditions we check in the programs are Boolean expression.

Example: a >= b

Algebraic Expression into QBASIC Expression


QBASIC has the provision to handle any complex arithmetic expression.
However, the system cannot understand terms like X2, A × B, etc. in the present
form. Certain arithmetic operators (or symbols) which are meaningful to the
computer are to be used to evaluate such arithmetic expressions.
Algebraic Expression QBASIC Expression
A+B-C A+B-C
X×Y÷Z X*Y/Z

I=(P*T*R)/100

(a+b)(a-b) (a+b)*(a-b)

(a/b)*x^2

(c) Operands
Operands are the values on which the operator performs its operation.

For example, in the below expression

C = A+B
The symbols ‘=’ and ‘+’ are the operators and the variables ‘C’, ‘A’ and ‘B’ are
the operands.

4.2.4 Program Statements


QBASIC Statements
The instructions within a program are called statements. Each QBASIC
instruction is like a sentence. As a sentence must have a verb in it, each QBASIC
instruction must have a QBASIC keyword. A QBASIC statement is either
executable or non-executable.

Types of Statements
Statements can be categorized into four groups.

→ Declaration Statements
→ Assignment Statements
→ I/O (Input/Output) Statements
→ Control Statements

(a) Declaration Statement


i) REM or ‘ statement [Comments]
Comments may be included in a QBASIC program to describe the purpose or to
clarify some portion of code in the program. In QBASIC, we use REM or ‘ for
comments. Purpose: To write explanatory remarks (comments) to be inserted in a
program

Example:
REM to find the sum of two numbers
CLS
INPUT "Type any two numbers: "; a, b
PRINT "Sum = "; a + b
END

ii) END statement


You have already used the END statement to terminate your program.

Purpose: It is a declaration that ends a QBASIC program, procedure or block.

Syntax:
END [{DEF | FUNCTION | IF | SELECT | SUB | TYPE}]

Note: END statement is also used to terminate function, IF construct,


subprocedure etc., which will be covered in the next chapter.

iii) CONST statement [Symbolic Constant]


A symbolic constant is a symbol represented by a name such as a variable which
stores one and only one value throughout the whole program.

Symbolic constants may be numbers or strings. A symbolic constant can be


declared using keyboard CONST.

Purpose: A non-executable statement that declares symbolic constants to use in


place of numeric or string values

Syntax:
CONST constant name = expression

- constant name is a name of a variable.


- expression consists of literals, other constants, or any of the arithmetic and
logical operators except exponentiation (^).
Example:
CONST pi=3.14

Here, pi is a symbolic constant and its value remains same as 3.14 throughout
the whole program and cannot be changed.
General Constant Symbolic Constant
A = 45 CONST A = 45
A = A+5 A= A + 5
Possible to change the value of Not possible to change the
general constant. value of symbolic constant.
iv) DIM statement
Purpose: DIM specifies a data type for a variable (explicit declaration) or declares
an array.

We have discussed about explicit declaration of variable in previous chapter.


Array will be discussed later on.

Explicit declaration of Variable using DIM statement:

Syntax:
DIM variable [AS type]

- type declares the data type of variable (INTEGER, LONG, SINGLE,


DOUBLE, STRING) Example:
DIM A AS STRING

Here, 'A' is declared explicitly as string variable.

(b) Assignment Statement


These statements are used to assign a value to a variable.

i) LET statement
Purpose: To assign a value to a variable.

Syntax:

[LET] variable=expression
Example:
LET A = 200
C$ = “SCHOOL”
Here, A is a numeric variable and the value 200 is assigned to A. Similarly, C$ is
a string variable and the value “SCHOOL” is assigned to C$.

Note: In QBASIC, a value can be assigned to a variable without LET statement


also.

Example:
LET A=200
The above statement is equivalent to
A=200

ii) SWAP statement


Purpose: an assignment statement that exchanges the values of two variables of
same type.

Syntax:
SWAP variable1, variable2

- variable1 and variable2 must be of exactly the same


type Example: CLS

a = 5: b = 10
SWAP a, b
PRINT "The value of a = "; a
PRINT "The value of b= "; b
END

(c) I/O (Input/Output) statements


The statements that are used to supply the data during the execution of the
program from the keyboard or file are called input statements.

Similarly, the statements that are used to get the result of the processing from
the output device or to the file are called output statements.

i) CLS statement
Purpose: To clear the display screen Syntax:
CLS
Note: It is a good practice to start your every program with CLS statement so that
the previous displayed content will be erased before executing your new
program.

ii) INPUT statement


Purpose: To read input from the keyboard during program execution and stores it
into a list of variables.

Syntax:
INPUT[;]["prompt string"{;|,}]variable list

- variable list is one or more variables separated by commas


- promptstring, if used, is displayed on the screen to tell the user of the
program what to enter on the keyboard

- the characters ";" and "," are the separators

Example:
INPUT “Type First Number”;a
Here, a is a numeric variable.
“Type First Number” is a prompt string
Note: You can also ask more than one value from a single INPUT statement.

INPUT “Type any two numbers:”; a, b


Here, comma (,) is used to separate the list of variables.
INPUT a, b
The above statement is also valid without a prompt string.

iii) LINE INPUT statement


Purpose: To input an entire line (up to 255 characters) to a string variable,
without the use of delimiters.

Syntax:
LINE INPUT[;] ["promptstring";] stringvariable

Example:
CLS
LINE INPUT "Describe about yourself "; S$
PRINT S$
END

iv) PRINT statement


Purpose: To display the output on the screen.

Syntax:
PRINT [expressionlist][{,|;}]

- If all arguments are omitted, a blank line is printed.

- If expression list is included, the values of the expressions are printed on


the screen.

- The expressions in the list may be numeric or string expressions. (String


literals must be enclosed in quotation marks.)

- The optional characters "," and ";" are the separators.

Example:
N = 50
PRINT “I live in Kathmandu.” PRINT 5;6
PRINT (3+2 + N) / 3

Note: You must enclose string constant with double quotes (“ ”).
As a shortcut, use a question mark symbol (?) for PRINT. Try it. Press <Enter> to
start typing on a new line. Now

Type this:

? "Programming is fun." and


press <Enter> again.

Other Examples: RINT


A blank line is printed.
PRINT 2*8+5
Here the expression is 2*8+5 and the evaluated value of this expression 21
(2*8+5=21) is printed.

PRINT “Nepal”
String literals “Nepal” enclosed in quotation marks is printed.

Similarly, the values of variables can be displayed in the following ways.

INPUT “Type your name ”; N$


PRINT “Your name is ”; N$
INPUT “Type your roll number ”;r
PRINT “Your roll number is ”;r
Or you can write this statement.

PRINT “Your name is ”; N$; “ and your roll number is ”;r


Try these statements:
PRINT N$;r
PRINT N$,r
Now find the difference in the output using; and, as a separator.

v) PRINT USING statement


Purpose: To print strings or numbers using a specified format

Syntax:
PRINT USING formatstring; expressionlist[{,|;}]
- formatstring, a string-expression, specifies the format.

- expressionlist contains the items to be printed


Characters used to format a Numeric Expression

# Digit position
- Decimal point position
, Placed left of the decimal point, prints a comma every third digit
$$ Prints leading $
+ Position of number's sign
^^^^ Prints number in exponential format
** Fills leading spaces with *
**$ Combines ** and $ Example:

Statement Output
PRINT USING "##.##"; .12 0.12
PRINT USING "###.##"; 117.614 117.61
PRINT USING "##.## "; 10.2; 5.3; 10.20 5.30 66.78
66.789; .234 0.23
PRINT USING "+##.## "; -68.95; 2.4; -68.95 +2.40 +55.60
55.6; -.9 -0.90
PRINT USING "**#.# "; 415.4; -.5; 415.4 *-0.5 *76.1
76.14
PRINT USING "$$###.##"; 125.18 $125.18
PRINT USING "**$##.##"; 7.69 ***$7.69
PRINT USING "##.##^^^^"; 758.56 7.59E+02
PRINT USING "####,.##"; 1452.9 1.452.90
Characters used to format a String Expression

& Prints entire string


! Prints only the first character of the string
\ \ Prints first 'n' characters, where n is the number of blanks between slashes +
2

Example:
A$ = "LOVE": B$ = "NEPAL"
PRINT USING "!"; A$; B$ 'First characters of A$ and B$.
PRINT USING "\ \"; A$; B$ 'Two spaces between backslashes, prints 4
'characters each from A$ and B$
PRINT USING "&"; B$ 'all of B$

Output:
LN
LOVENEPAL
NEPAL vi) LPRINT and LPRINT USING

statements

Purpose: I/O statements that print data on the printer LPT1

Syntax 1:
LPRINT [expressionlist][{;|,}] Syntax 2:
LPRINT USING formatstring; expressionlist[{;|,}] Example:
LPRINT "LOVE"; TAB(76); "NEPAL"

vii) READ and DATA statement


READ statement – an I/O statement that reads values from a DATA statement
and assigns the values to variables

Syntax:
READ variablelist
- Variablelist is made up of one or more variables, separated by commas,
which are to receive the data. The variables may be string or numeric.
DATA Statement – a non-executable statement that stores the numeric and
string constants used by a program's READ statements

Syntax:
DATA constant[,constant]...

- constant is any valid numeric or string constant.

Example 1:
READ a, b, c
PRINT a, b, c
DATA 34,56,78

Example 2:
READ a$, b
PRINT a$, b
DATA "Nepal's View",45

Example 3:
FOR i = 1 TO 5 READ n
s=s+n
NEXT i
PRINT "Sum = "; s
DATA 56,78,65,43,21
END

Note:
→ READ statement cannot be used without DATA statement.
→ DATA statement can be placed anywhere in the program.
→ Data and variable types should be same.
→ In DATA statement, string data is not necessary to be enclosed by double
quotes.
→ If number of variable in READ statement is more than number of data in DATA
statement, an error message “Out of DATA” will be displayed.

Summary of the chapter


- The instructions within a program are called statements.

- Types of statements: Declaration, Assignment, I/O and Control

- Declaration Statement: REM or ', CONST, DIM

- Assignment Statement: LET, SWAP

- I/O Statement: INPUT, PRINT, INKEY$, READ - DATA

Sample Programs
1. REM ask name, address & class and display them
CLS
INPUT "Type your name "; n$
INPUT "Type your address "; a$
INPUT "Type your phone "; c
PRINT "Your name is "; n$
PRINT "You live in "; a$
PRINT "You study in class "; c
END

2. REM To find the sum of any two integers


REM Implicit declaration
CLS
INPUT "Type first number "; a% INPUT "Type second
number "; b% c% = a% + b%
PRINT "Sum = "; c%
END

3. REM To find the area of a circle


' Symbolic constant
CONST pi = 3.14
INPUT "Radius of a circle "; r a = pi * r ^ 2
PRINT "Area = "; a
END

4. REM Explicit declaration


DIM a AS INTEGER, b AS INTEGER, c AS LONG
CLS
INPUT "Type first number "; a INPUT "Type second number ";
bc=a*b
PRINT "Product = "; c
END

5. REM find average using READ ... DATA


CLS
READ a, b, c avg = (a + b + c) / 3
PRINT "Average = "; avg
DATA 45,67,52

4.2.5 Program Flow and Control Structures


A program is a set of statements. When we run the program, its statements are
executed to perform our tasks. Normally, they are executed one after another
from top to bottom. The order of execution of statements of a program is known
as the flow of program or program control.

Control Structure
Control structures are used to alter the way of execution of the program or used
to control the flow of a program. The controls are classified into three types.

→ Sequential Structure
→ Selection Structure
→ Looping Structure
(a) Sequential Structure
In sequential structure, the statements are
executed one after another sequentially from top to
bottom without changing the flow of the program. Statement 1 The
programs which you have written in previous
chapters are based on sequential structure.

(b) Selection Structure Statement 2


It is also called a branching structure. In this
structure, the control of the program is
transferred from one part of the program to
another on the basis of a specified condition or Statement 3
without condition.
Fig. Flowchart of a
Branching or Jumping: The process of sequential structured
departure program
of the control of the program conditionally or unconditionally from its sequential
flow depending upon the result of the condition used is termed as branching or
jumping. There are two types of branching statements. → Unconditional
statement

→ Conditional statement
Unconditional Statement
The statement that transfers the control of the program from one step to
another without testing the condition is called an unconditional statement. In
QBASIC, GOTO statement is the unconditional statement.

GOTO statement
Purpose: a control flow statement that branches unconditionally to the specified
line

Syntax:
GOTO {linelabel | linenumber}
linelabel or linenumber is the label of the line to execute next. This line must be
in the same procedure or subroutine as the GOTO statement

Example:
CLS
PRINT "Kathmandu"
GOTO abc
PRINT "Birgunj" abc:
PRINT "Biratnagar"
END

Output:
Kathmandu
Biratnagar
Biratnagar
Here, abc is a linelabel. After displaying “Kathmandu”, the control is transferred
to “abc” and displayed “Biratnagar” bypassing the statement PRINT "Birgunj".

Using linenumber
The below program gives the same output using line number.

CLS
PRINT "Kathmandu"
GOTO 10
PRINT "Birgunj"
10 PRINT "Biratnagar"
END

Conditional Statement
a) IF .. THEN … ELSE statement
Purpose: It is a control flow statement that allows conditional execution or
branching, based on the evaluation of an expression that must be either true or
false.

True
Condition

Statement 1
False

Statement 1

QBASIC supports variety of IF statement.


i) Single line IF statement
Purpose: It allows conditional execution based on the evaluation of an expression
that must be true.

Syntax:
IF boolean expression THEN [statement]
- booleanexpression is an expression that must return non-zero (true) or zero
(false) - statement consists of a task to be executed if the booleanexpression
returns true.
Example:
CLS
INPUT “Type your marks ”;m
IF m>=40 THEN PRINT “Pass”
END
Note: In the above program, if the booleanexpression m>=40 returns true, it
displays “Pass”. The program displays nothing if the value is less than 40.

ii) Single line IF … ELSE statement


Purpose: It allows conditional execution based on the evaluation of an expression
that must be either true or false.

Syntax:
IF booleanexpression THEN [statement1] ELSE [statement2]

Example:
CLS
INPUT “Type your marks”;m
IF m>=40 THEN PRINT “Pass” ELSE PRINT “Fail”
END
Note: statement1 will be executed if the booleanexpression returns true,
otherwise statement2 will be executed.

iii) Multiline IF Statement


Purpose: To execute a block of statements based on the evaluation of an
expression that must be true.

Syntax:
IF booleanexpression THEN
[statementblock]
END IF

- statementblock consists of any number of statements on one or more lines.


Example:
CLS
INPUT “Type your marks ”;m
IF m>=40 THEN
PRINT “Pass”
PRINT “Congratulations”
END IF
END
Note: END IF terminates the multiline IF statement which is not required in single
line IF statement.

iv) Multiline IF … ELSE statement


Purpose: To execute a block of statements based on the evaluation of an
expression that must be either true or false.

Syntax:
IF booleanexpression THEN
[statementblock-1]
ELSE
[statementblock-2]
END IF

Example:
CLS
INPUT “Type your marks ”;m
IF m>=40 THEN
PRINT “Pass”
PRINT “Congratulations”
ELSE
PRINT “Fail”
PRINT “Work hard”
END IF
END

v) IF …. THEN … ELSEIF statement


Purpose: To execute a block of statements based on the evaluation of multiple
expressions that must be either true or false.

Syntax:
IF booleanexpression1 THEN
[statementblock-1]

ELSEIF booleanexpression2 THEN

[statementblock-2]

...

ELSE

[statementblock-n]

END IF

Example:
CLS
INPUT "Type your percentage "; p
IF p >= 80 THEN

PRINT "Distinciton"

ELSEIF p >= 60 THEN

PRINT "First Division"

ELSEIF p >= 45 THEN

PRINT "Second Division"

ELSEIF p >= 32 THEN

PRINT "Third Division"

ELSE

PRINT "Fail"

END IF
END

b) SELECT CASE statement


Purpose: To execute one of several statement blocks depending on the value of
an expression

Syntax:
SELECT CASE testexpression
[CASE expressionlist1
[statementblock-1]
[CASE expressionlist2
[statementblock-2]
...
[CASE ELSE
[statementblock-n]
END SELECT

- testexpression is any numeric or string expression


- expressionlist contains one or more expressions of the same type as
testexpression - The IS keyword must precede any relational operators in
an expression Example:
CLS
INPUT "Type your roll number ", r
SELECT CASE r CASE 1

TO 10

room = 5

CASE 11 TO 15

room = 6

CASE 18, 21, 28

room = 7

CASE IS >= 35

room = 8 CASE
ELSE room = 9

END SELECT
PRINT "Your room number is "; room
END

(c) Looping Structure


Looping is the process of repeating the execution of a statement or a block of
statements guided by a condition. A loop is terminated when the given condition
is satisfied.

Example: CLS
n = 1 top:
PRINT "Nepal" n = n + 1
IF n <= 5 THEN GOTO top
END
Flowchart of the above program is: Memory table of dry run

Star n OP (Output)
t 1 Nepal
2 Nepal
3 Nepal
n=
1 4 Nepal
5 Nepal
6
Print "Nepal"
 The output of the above program
is
Nepal
Nepal
Yes Nepal
n <=
Nepal
5?
Nepal

No

Stop
Here the statement PRINT “Nepal” is executed 5 times till the condition n<=5 is
true.

QBASIC standard looping statements. a)


DO … LOOP WHILE statement Syntax:
DO

[statementblock]

LOOP [WHILE booleanexpression]

- booleanexpression is an expression that will return non-zero (true) or zero


(false).
- statementblock is any number of statements on one or more lines which
are to be executed as long as boolean expression is true.
Example: CLS
n=1
DO
PRINT "Nepal" n = n + 1
LOOP WHILE n <= 5
END

Counters, Intermediate Value, Sentinel Value & Accumulators


Counters are the numeric variables which store the number of times a
statements or a block of statements is executed in a loop. Counters may have
increment or decrement of any numbers.

The mid-values of a counter except initial and final value in a loop are called
intermediate value and the final value of a counter is called sentinel value.

Accumulators are the variables in which the intermediate value of another


variable is added.

Example:
A=1
DO
PRINT A;
S=S+A
A = A+1
LOOP WHILE A<=10
PRINT “Sum = “;S
END
In the above program, the variable A is used as a counter which counts the
number of execution of a block of statements in the loop. Here, the intermediate
values of the counter A are 2,3,4,5,6,7,8 & 9. Similarly, the sentinel value of A is
10. The variable S is used as an accumulator which adds the intermediate values
of A.

b) DO WHILE … LOOP statement Syntax:


DO [WHILE booleanexpression]
[statementblock]
LOOP
Example: CLS
n=1
DO WHILE n <= 5 PRINT
"Nepal" n = n + 1
LOOP
END

c) WHILE …WEND statement


Syntax:
WHILE condition

[statements]
WEND

- condition is an expression that will return non-zero (true) or zero (false)

- statements are one or more QBASIC statements to be executed as long as


condition
is true
Example: CLS
n=1
WHILE n <= 5
PRINT "Nepal" n = n + 1
WEND
END

d) DO … LOOP UNTIL statement Syntax:


DO

[statementblock]

LOOP [UNTIL booleanexpression]


Example: CLS

n=1
DO
PRINT "Nepal" n = n
+1
LOOP UNTIL n>5
END
Note: The loop will be continued until the condition is true. That is, if the given
Boolean expression returns false (0) the loop will be continued. The loop will be
terminated if the condition is true.

The above program displays “Nepal” 5 times. When the value of n reaches 6, the
given conditions return true (1) value and the loop will be terminated.

e) DO UNTIL …. LOOP statement Syntax:


DO [UNTIL booleanexpression]

[statementblock]

LOOP
Example: CLS

n=1
DO UNTIL n>5
PRINT "Nepal" n = n + 1
LOOP
END

f) FOR … NEXT statement Syntax:


FOR counter = start TO end [STEP increment/decrement]
[statements]

NEXT counter
- counter is a numeric variable used as the loop counter

- start is the initial value and end is the final value of the counter

- increment is the amount the counter is incremented each time through the
loop Example:
CLS
FOR n=1 TO 5 STEP 1

PRINT “Nepal”

NEXT n
END
Note: The above program displays “Nepal” 5 times.

Here, the counter is n and its initial value is 1 and final value is 5 and after each
iteration, the value of the counter will be incremented by 1.

Similarly, the below program also gives the same output.

CLS
FOR n=10 TO 2 STEP -2

PRINT “Nepal”

NEXT n
END
If the counter has to be incremented by 1, then you don’t need to specify it with
STEP keyword.

For example, the below program displays “Mount Everest” 5 times.

CLS
FOR n=1 TO 5
PRINT “Mount Everest”
NEXT n END

Nested Loop
A loop inside another loop is called a nested loop.

Example of a program using a nested loop.


Program Output
FOR X=1 TO 5 1
FOR Y=1 TO X 12

PRINT Y; 123

NEXT Y 1234
1234
PRINT
5
NEXT X
Sample Program
i) To check whether the supplied number is prime or not
CLS
INPUT "Type any number "; n
FOR x = 2 TO n - 1

IF n MOD x = 0 THEN c = c + 1

NEXT x
IF c = 0 THEN PRINT "Prime" ELSE PRINT "Composite"
END

ii) To display the numeric pattern 2,22,222,222,222 CLS


n=2
FOR i = 1 TO 5

PRINT n;

n = n * 10 + 2

NEXT i
END

iii) To display the smallest among three numbers


CLS
INPUT "Type any three numbers "; a, b, c
IF a > b AND a > c THEN

PRINT a

ELSEIF b > a AND b > c THEN

PRINT b

ELSE

PRINT c

END IF
END

iv) To calculate the sum of individual digits of an integer


CLS
INPUT "Type any integer "; n% WHILE n% <> 0
r = n% MOD 10
s% = s% + r

n% = n% \ 10

WEND
PRINT "Sum of individual digits = "; s%
END

4.2.6 Library Functions


Function
Function is a block of statement executed together to perform specific tasks and
usually returns a value.

Types of Function
Basically, there are two types of function available in QBASIC.

→ Library Function
→ User-Defined Function
Library Function
Also called Built-in function or intrinsic function. It is already available with a
programming language. The programmer does not need to define this function.

a) String manipulation functions


i) LEN function
Purpose: a string processing function that returns the number of characters in a
string or the number of bytes required by a variable

Syntax:
LEN(stringexpression) or
LEN(variable)

- stringexpression is a string constant or string expression

- variable identifies the object for which you want the number of required
bytes
Example: CLS
a$ = "Nepal" b% =
456

c& = 23 d! =
56.789 e# =
44.55
PRINT LEN(a$) ‘ Displays 5
PRINT LEN(b%) ‘ Displays 2
PRINT LEN(c&) ‘ Displays 4
PRINT LEN(d!) ‘ Displays 4
PRINT LEN(e#) ‘ Displays 8
END

ii) LEFT$ function


Purpose: a string processing function that returns a string consisting of the
leftmost n characters of a string

Syntax:
LEFT$(stringexpression,n)
- stringexpression is a string constant, string variable, or string expression
- n, a numeric expression, must have a value between 0 and 32,767. If n is
zero, the null string (a zero-length string) is returned.

iii) RIGHT$ function


Purpose: a string function that returns the rightmost n characters of a string

Syntax:
RIGHT$(stringexpression,n)
iv) MID$ function
Purpose: a string-processing function that returns a substring of a string

Syntax:
MID$(stringexpression,start,length)

- stringexpression identifies the string from which the substring is to be


extracted
- start, a numeric expression that has an integer value between 1 and
32,767, specifies the starting character position of the substring

- length can be omitted if you want all the characters to the right of start
Example of using LEFT$, RIGHT$ and MID$ functions:
CLS
S$ = "BIRATNAGAR"
PRINT LEFT$(S$, 5) ‘Displays BIRAT
PRINT RIGHT$(S$, 5) ‘Displays NAGAR
PRINT MID$(S$, 3, 5) ‘Displays RATNA

PRINT MID$(S$, 6) ‘Displays NAGAR

END

v) CHR$ function
Purpose: a string processing function that returns a one-character string whose
ASCII code is the argument

Syntax:
CHR$(code)

- code, a numeric expression that has a value between 0 and 255, is one of the
ASCII character codes vi)

ASC function

Purpose: a string processing function that returns a numeric value that is the
ASCII code for the first character in a string expression

Syntax:
ASC (stringexpression)
Characters ASCII Value
A-Z 65-90
a-z 97-122
0-9 48-57
Space 32
Enter 13
Example:
PRINT CHR$(65) ‘ Displays A

PRINT ASC("abc") ‘ Displays 97 vii)

VAL function

Purpose: a string-processing function that returns the numeric value of a string of


digits

Syntax:
VAL(stringexpression) viii)

STR$ function

Purpose: a string function that returns a string representation of the value of a


numeric expression

Syntax:
STR$(numeric-expression)

Example: CLS
a=5b=6
c$ = "7" d$ =
"8"

PRINT a + b ‘ Displays 11

PRINT c$ + d$ ‘ Displays 78

PRINT STR$(a) + STR$(b) ‘ Displays 56

PRINT VAL(c$) + VAL(d$) ‘ Displays 15

END

ix) SPACE$ function


Purpose: a string processing function that returns a string of spaces of length n
Syntax:
SPACE$(n)

- n, a numeric expression that has an integer value between 0 and 32,767, is the
number of spaces you want in the string
Example:
CLS
PRINT "Stand-Up"; SPACE$(5); "Online"
END

x) STRING$ function
Purpose: a string processing function that returns a string whose characters all
have a given ASCII code or whose characters are all the first character of a
string expression

Syntax:
STRING$(m,n)

STRING$(m,stringexpression)

- m, a numeric expression, is the length of the string to return


- n, a numeric expression that has an integer value between 0 and 255, is
the ASCII character code of the character that fills the string

- stringexpression identifies a string whose first character can be used to fill


the string
Example:
CLS
PRINT STRING$(4, 65) ' prints AAAA
PRINT STRING$(2, "Nepal") ' prints NN

END xi)

DATE$

As a statement
Purpose: To set the current system date

Syntax:
DATE$ = stringexpression

- stringexpression must have one of the following forms:


mm-dd-yy, mm-dd-yyyy, mm/dd/yy, or mm/dd/yyyy where mm and dd are
the month and day, and yy or yyyy are the year

Example:
DATE$ = "05-25-1995"
Sets the system date as 25th May 1995

As a function
Purpose: To return a string containing the current system date

Syntax:
DATE$

- a ten-character string is returned, in the form mm-dd-yyyy mm is the


month (01-12), dd is the day (01-31), and
yyyy is the year (1980-2099)
Example:
PRINT DATE$
Displays the current system date

xii) TIME$
As a statement

Purpose: To set the current time

Syntax:
TIME$=stringexpression

- stringexpression must have one of the following forms:


hh, hh:mm, or hh:mm:ss where hh is the hour on a 24-hour clock, mm is
the minutes, and ss is the seconds

Example:
TIME$ = "05:25:55"
Sets the system time as 5 Hour 25 Minutes and 55 Seconds
As a function

Purpose: To return the current system time

Syntax:
TIME$
- an eight-character string is returned, in the form hh:mm:ss hh is the hour
(00-23), mm is the minute (00-59), and ss is the second (00-59)

Example:
PRINT TIME$
Displays the current system time

xiii) LTRIM$ function


Purpose: a function that returns a copy of a string with leading spaces removed

Syntax:
LTRIM$(stringexpression) xiv)

RTRIM$ function

Purpose: a string processing function that returns a string with trailing (right-
hand) spaces removed

Syntax:
RTRIM$(stringexpression)

Example:
B$ = “ KATHMANDU”
C$ = “BIRATNAGAR ”
B$=LTRIM$(B$)
C$=RTRIM$(C$)
PRINT LEN(B$);LEN(C$)
END

b) Mathematical Function
i) ABS function
Purpose: a math function that returns the absolute value of a numeric expression

Syntax:
ABS(numeric-expression)

Example:
a = -5
PRINT ABS(a)

ii) SGN function


Purpose: a math function that indicates the sign of a numeric expression

Syntax:
SGN(numeric-expression)

- If numeric-expression is positive, the SGN function returns +1.

- If numeric-expression is zero, the SGN function returns 0.

- If numeric-expression is negative, the SGN function returns -1.

Example: CLS
a = 5 b = 0 c = -9.8
PRINT SGN(a) ' prints 1
PRINT SGN(b) ' prints 0
PRINT SGN(c) ' prints -1
END

iii) SQR function


Purpose: a math function that returns the square root of numeric expression

Syntax:
SQR(numeric-expression)

- numeric-expression must have a value greater than or equal to zero


Example: CLS
a = 9 b = SQR(a) PRINT
"Square root of "; a; " is";
b

END

iv) INT function


Purpose: a math function that returns the largest integer less than or equal to a
numeric-expression

Syntax:
INT(numeric-expression)

v) FIX function
Purpose: a math function that returns the truncated integer part of a numeric
expression
Syntax:
FIX(numeric-expression)

vi) CINT function


Purpose: a conversion function that converts a numeric expression to an integer
by rounding the fractional part of the expression

Syntax:
CINT(numeric-expression) Example:
a=5 a= 5.5 a = -5.5

INT (a) 5 5 -6
FIX (a) 5 5 -5
CINT (a) 5 6 -6
vii) SIN function
Purpose: a math function that returns the sine of an angle given in radians

Syntax:
SIN(numeric-expression)

- numeric-expression is the angle, expressed in radians.

viii) COS function


Purpose: a math function that returns the cosine of an angle given in radians

Syntax:

COS(numeric-expression)

ix) TAN function


Purpose: a math function that returns the tangent of an angle expressed in
radians

Syntax:
TAN(numeric-expression)

Example:
a = 45
PRINT SIN(a)
PRINT COS(a)
PRINT TAN(a)
c) Other useful functions and statements
i) SPC function
Purpose: an I/O function that skips n spaces in a PRINT statement

Syntax:
SPC(n)

- n, a numeric expression that returns an integer value between 0 and 32,767, is


the number of spaces you want in print line

Example:
CLS
PRINT "Kathmandu"; SPC(5); "Nepal"
END

Output:
The SPC function returns 5 spaces which are printed as

below. Kathmandu Nepal ii) TAB function

Purpose: a device I/O function that moves the print position when used in the
PRINT statement

Syntax:
TAB(column)

- column, a numeric expression that has an integer value between 1 and


(columnwidth of the display device )
Note: In normal mode, the column-width of the screen is 80.

Example:
CLS
PRINT "Kathmandu"; TAB(5); "Nepal"
END

Output:
The TAB() function moves the print position to the 5th column of the screen. So,
the string constant “Nepal” will be printed from the 5 columns i.e. after 4 spaces
in the second line as below.

Kathmandu
Nepal
iii) LOCATE statement
Purpose: an I/O statement that moves the cursor to the specified position

Syntax:
LOCATE [row],[column]

- row and column to move the cursor Example:


CLS
PRINT SPC(5); "NEPAL"
PRINT TAB(5); "NEPAL"
LOCATE 5, 2
PRINT "NEPAL"
END

Output:
Column
1
→ 1 2 3 4 5 6 7 8 9
0 Returns 5 spaces by
Row ↓
SPC function
1 N E P A L Moves the cursor to 5th Column

2 N E P A L

3
Prints from 5th row & 2nd column
4

5 N E P A L Sample Program
6 i) To display the reverse of a
string
CLS
INPUT "Type your name "; n$
FOR i = 1 TO LEN(n$) c$ = MID$
(n$, i, 1) r$ = c$ + r$
NEXT i
PRINT "Reversed = "; r$
END

ii) REM To count the frequency of vowels


CLS
INPUT "Type any string "; s$ FOR i = 1
TO LEN(s$) b$ = MID$(s$, i, 1)
IF b$ = "a" OR b$ = "e" OR b$ = "i" OR b$ = "o" OR b$ = "u" THEN c = c +
1
END IF
NEXT i
PRINT "Frequency of vowels = "; c
END

iii) To display the below pattern


EDUCATION
DUCATIO
UCATI
CAT
A
CLS
b$ = "EDUCATION"
n=9
FOR i = 1 TO 5

PRINT TAB(i); MID$(b$, i, n) n=n-

NEXT i
END

iv) To change the letters in uppercase without using UCASE$() function CLS
s$ = "nepal" FOR i = 1 TO 5
b$ = MID$(s$, i, 1) c =
ASC(b$) d$ = d$ + CHR$(c -
32)
NEXT i
PRINT "In uppercase: "; d$
END
v) REM to calculate hex equivalent of a decimal no.
INPUT "DECIMAL NO:"; D
DO WHILE D <> 0

R = D MOD 16

IF R < 10 THEN

A$ = LTRIM$(STR$(R)) + A$

ELSE
R = R + 55
A$ = CHR$(R) + A$
END IF
D = D \ 16
LOOP
PRINT "HEXADECIMAL EQUIVALENT:"; A$

Exercises
1. Answer the following questions:
a) What is a function? Mention its types.

b) What are library functions? List the functions available in QBASIC


library.

c) Differentiate between library function and user-defined function.

2. Debug the following programs:


a) REM Reversing a string

INPUT "Type any number "; n$ FOR p


= LEN(n$) TO 1 b$ = MID$(n$, 1,
p) + b$

NEXT q

PRINT "Reversed = "; b$

END

b) REM Printing a string in a pattern


DIM S AS STRING
S = HAMI NEPALI

FOR P = 1 TO LEN$(S)

PRINT LEFT (S, P)


NEXT S

END

c) CLS
BROWSER=”BROWSER”

BROWSERLEN$=LEN(BROWSER$)

FOR KOUNTER=1 TO BROWSERLEN

PRINT MID(BROWSER$,KOUNTER,1);

NEXT BOUNCER

END

3. Write down the output of the following programs:


a) CLS
FOR i = 1 TO 5
PRINT LEFT$("NEPAL", i)
NEXT i
END

b) CLS
ST$ = "PLNMPBENPLA"
FOR I = 1 TO 5
READ N
PRINT MID$(ST$, N, 1);
NEXT I
DATA 3,7,9,11,2
END

c) CLS
FOR I = 1 TO 5
READ N$, A
PRINT MID$(N$, A, 1);
NEXT I
DATA COMPUTER,4,ORACLE,1,WINDOW,6
DATA KEYBOARD,2,FORTRAN,5
END

d)
i) N ii) N E P A L N ii) N E P A L
N E E P A E P A L
N E P N E P P A L
N E P A N E A L
N E P A L N L
iv) L v) N E P A L
vi) L A L
A L E P A L
P A L
P A L P A L
E P A L N E P A
E P A L A L
L
N E P A L L
vii) N viii) L ix) A
E A C A T
P P U C A T I
A E D U C A T I O
L N E D U C A T I O N
1.2.7 Array

You might also like