The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by yamarajkarki, 2021-07-10 01:56:27

Green-Computer-Book-8

Green-Computer-Book-8

Press Esc key to clear the welcome dialog box or Click on the area with the
message to clear the screen.

Press Alt + Enter to enlarge the QBASIC window to fit in the screen.

Parts of QBASIC Windows

QBASIC Editor Screen consists of four main parts. They are Menu Bar, View
Window, Immediate Window and Reference Bar.

1. Menu Bar: Menu Bar provides pull down menus and their options to work
with QBASIC easily.

2. View Window: We write and edit programs in View Window. It is the
largest part of the QBASIC Editor Screen.

3. Immediate Window: You can type and test a functioning of statement in
this window by pressing Enter key after a line of statement.

4. Reference Bar: It displays the information about the active keys of the
QBASIC Editor as SHIFT +F1, F2, F5 and F8.

Writing New Program

1. Click on File menu or press ALT + F.

2. Select New Program option.

GREEN Computer Book-8 251

A blank screen with blinking cursor appears. Now, you can type your program.

Saving a Program

1. Click on File menu or press ALT + F.
2. Select Save or Save As option.
A dialog box appears.

3. Specify the desired drive location and folder where you want to save the
file. (Example: E:\Class 8)

252 GREEN Computer Book-8

4. Type the filename in File Name box.
5. Click on OK or Press Enter key.

Running Program

1. Click on Run menu or Press ALT +R.

2. Select Start option.

Or

Press F5 key to
continue execution
from previous point
or SHIFT + F5 key
to execute from the
beginning.

You can press any key to return to the QBASIC Editor Screen.

Opening an Existing File

1. Click File menu or Press ALT + F.

2. Select Open option. A dialog box
appears

3. Choose the filename from the location
where the file is saved.

4. Select the filename.

5. Click on OK or Press Enter key.

Cut and Paste

1. Highlight the line of statement or block
of statement to be moved.

2. Click on Edit menu and select Cut.

Or

Press Shift + Del key to cut.

3. Go to the desired location

GREEN Computer Book-8 253

4. Click on the Edit menu, Select Paste.
Or
Press Shift + Insert key for paste.

Exiting from QBASIC

1. Click on File menu or Press ALT + F.

2. Select Exit option.

Characters Set in QBASIC

One of the virtues of BASIC language is a simple way in which data is
represented.

Data, in a basic program, may be of two types. These are number or numeric
data and a set of characters called string or literal data.

Type Character or Symbol

Alphabet A to Z or a to z
Digits 0 to 9
Operators +, -, *, /, \, MOD, ^, ( ), =, <, >, >=, <=, <>
Special Characters $, #, !, %, &

Constants and Variables

Constants

Constants are the data or values that do not change when the program executes.
The data types accepted by QBASIC can be termed as Constants. There are
two types of Constants in QBASIC.

1. Numeric Constant 2. String Constant

1. Numeric Constant

A number or a set of numbers used in mathematical calculations and
comparisons is known as Numeric Constant. It is the general type of number
and appears normal like we write in English or mathematics. It should be
stored in Numeric Variable.

254 GREEN Computer Book-8

Valid Numeric Constant
1000 -253 1056.50 -25.64

Numeric Constant can be positive or negative numbers with decimal values.

Invalid Numeric Constant

a. 10,00 Comma is not acceptable.

b. 100% No sign is accepted.

c. 1 23 Space is not allowed.

2. String Constant

A set of alphanumeric values and special characters is known as String
Constant. Normally, it is enclosed within double quotation marks. It should
be stored in String Variable.

Valid String Constant Invalid
a. “Kathmandu” Kathmandu”
b. “Programming is fun” “programming” “is”
“fun”
c. “---------special characters can be used -------” I love Nepal
d. “5526783” 984150

Variables

Variable is the name or an entity of space in the memory location that can hold
alphanumeric and numeric values. A variable can contain a number, a special
character, a word, a sentence or an entire paragraph as its value. The value of
variable can change during the program execution. It is further divided into
two categories on the basis of data it stores.

1. String Variable 2. Numeric Variable

1. String Variable

It is the data entity that holds alphanumeric values. The name of string variable
ends with dollar ($) sign. It also accepts number as its value but the number
does not take part in mathematical calculations. The value of String Variable is
the string constant.

GREEN Computer Book-8 255

Valid String Variable

a. N$ b. FirstName$ c. Address$ d. pH$ e. R15$

Invalid String Variable Variable name doesn’t start with a number.
a. 1N$ Space is not allowed.
b. First Name$ $ sign is missing.
c. Address

2. Numeric Variable

It holds number for mathematical calculations. It cannot accept text as its
value. Numeric Variable is further divided into four types.

Data type Declaration Maximum Minimum Bytes used
Integers % 2
Long Integers & 32,767 -32,768 4
Single (7-digit) ! 4
Precision 2,147,483,647 -2,147,483,648
Double (16-digit) # 8
Precision 3.402823 +38 1.401298 E-45
String $ 2
1 . 7 9 7 6 9 3 1 4.940656 D-324
D+308

32767 0

Rules for naming Variable
a. First letter of a variable name must be an alphabet.

b. Variable name can be up to 40 characters long.

c. Variable name can be in UPPERCASE, lowercase or a combination of both.

d. Space in between the variable name is not allowed.

e. Variable name should be precise and meaningful.

f. Some reserved words such as SQR, PRINT, LEN, etc. cannot be used as
variable name

g. Letters (a to z an A to Z) and numbers (0 to 9) can be combined to write
variable name.

256 GREEN Computer Book-8

Operators and Expressions

Operators are signs or symbols that perform mathematical and logical
operation such as addition, multiplication, comparisons, assign values and
make logical decisions. The types of operators are:

a. Arithmetic Operators b. Relational Operators

c. Logical Operators d. String Operators

e. Assignment Operators

a. Arithmetic Operators

They perform mathematical calculations like addition, subtraction,
multiplication, division, etc. They give numeric values (numbers) as a result.

Arithmetic Operators Function Example

^ (Exponentiation) calculates exponent (Power of the 2^3=8
number)

* (Asterisk) multiplies the numbers 2*3=6

/ (Forward slash) divides the number 5/2=2.5

\ (Backward slash) performs division and gives only 5\2=2
integer number as quotient

MOD calculates remainder 5 MOD 2=1

+ performs addition 3+1=4

- performs subtraction 3-1=2

b. Relational Operators (=, <, >, >=, <=, <>)

They compare the values and give result as True or False. They are used to
make decisions in the program.

If a= 5, b= 8 then;

= is equal to a=b = false
> is greater than a>b = false
< is less than a<b = true
> = is greater than or is equal to a>= b =false
< = is less than or is equal to a<=b = true
<> is not equal to a<>b true

GREEN Computer Book-8 257

c. Logical Operators (AND, OR ,NOT)

They help to make decisions as True or False. It combines multiple conditions
to make a complex logic.

Truth Table

A truth table shows the outputs obtained from a logic circuit or gate as
consequences of specific inputs.

Truth Table for OR operator

Condition1 (x) Condition2 (y) Output z= (x OR y)
False(0)
False(0) False(0) True(1)
True(1)
True(1) False(0) True(1)

False(0) True(1)

True(1) True(1)

Let a=10, b= 15, c= 20 Condition 2 (y) Output (x OR y)
a>c=false False OR False = False
Condition 1 (x) a<b= true False OR True=True
a>b= false a>b= false True OR False=True
a>b= false a<c= true True OR True= True
a<b=true
a<b=true

Truth Table of AND operator

Condition1 (x) Condition2 (y) Output z= (x AND y)
False(0)
False(0) False(0) False (0)
False (0)
True(1) False(0) True(1)

False(0) True(1)

True(1) True(1)

258 GREEN Computer Book-8

Let a=10, b= 15, c= 20 Condition 2 (y) Output (x OR y)
a>c=false False AND False = False
Condition 1 (x) a<b= true False AND True=False
a>b= false a>b= false True AND False=False
a>b= false a<c= true True AND True= True
a<b=true
a<b=true

Truth Table of NOT Output
True (1)
Input False(0)
False(0)
True(1)

d. String Operator

It combines text with the " + " sign. This process is known as concatenation.

Example:

A$="Program"

B$="ming"

Print A$+B$

It will result in Programming.

Operands

Operands are constant values or variables on which mathematical or logical
operations take place. Let us look at the example below:

X = 5 + 10

Sum = A + B

In the above two expressions, the numbers 5, 10 and the variables A and B are
called the operands. The plus is the arithmetic operator which is working on
the operands.

GREEN Computer Book-8 259

Expressions

The combination of operators, variables and constants is known as an
expression. The variables or constants used before or after an operator are
known as operand. An expression gives out a value as a result.

Conversion of Algebraic Expression into QBASIC Expression

Algebraic Expression QBASIC Expression

a. h2 = p2 + b2 h^2 = p^2 + b^2

b. (a2 – b2 )=(a +b) (a –b) (a^2+b^2) = (a + b) * (a – b)

c. V = L3 V = L^3

d. A ÷ B x C A/B*C

e. average = total ÷ N average = total / N

POINTS TO REMEMBER

1. QBASIC is the high level programming language.

2. Constants are the data or values that do not change when the program
executes.

3. Variable is the space in the memory location that can hold alphanumeric
and numeric values.

4. String variable is the data entity that holds alphanumeric values.

5. Numeric variable holds number for mathematical calculations.

6. Operators are signs or symbols that perform mathematical and logical
operation such as addition, multiplication, comparisons, assign values
and make logical decisions.

7. Operands are constant values or variables on which mathematical or
logical operations take place.

8. The combination of operators, variables and constants is known as an
expression.

9. Relational operators compare the values and give result as True or False.

260 GREEN Computer Book-8

Exercise

1. Answer the following questions.
a. What is QBASIC? Write the versions of QBASIC.
b. Write the features of QBASIC.
c. Write steps to starting QBASIC.
d. What is Immediate Window?
e. Write steps for saving a program.
f. Write steps to cut and paste block of codes in QBASIC program.
g. Define operator. List out the types of operator.
h. Make truth table of AND, OR, and NOT operators.
i. What is variable? Write rules for naming variable.

2. Write the function of the following shortcut keys.
• ALT + F
• ALT + R
• Shift + Insert
• Shift + Del
• F5
• SHIFT + F5

3. Fill in the blanks.
a. String Variable ends with a ........................sign.
b. Variable name can be up to ........................ characters long.
c. A variable name should always start with an .........................
d. QBASIC was first invented by …………………………….. of Dartmouth
College, U.S.A. in 1964.
e. String variable is the data entity that holds …………………..values.

GREEN Computer Book-8 261

4. Evaluate and find the output of the following if a=15, b=20 and c= 5.

a. (a + b +c )/4
b. (a + b +c ) \5
c. b MOD c
d. b MOD a * c
e. (a+b)^(1/2)
f. (a+b+c)^(1/3)
g. 12 MOD 4 = 3
h. 45>56 AND 45=45
i. 45>76 AND 25<>5
j. a<b OR a<>c

5. Identify the valid and invalid variables and constants:

a. 2654 b. Name$ c. 0.65 d. “hi!”
e. “12/7/1972” f. 20/A
i. A$ j. 9+8 g. “Ktm Nepal” h. 1N
l. -77 m. Roll
j. Num k. $city

n. sum of two numbers

6. Complete the Table if a=15, b= 5, c= 10.

Condition 1 (x) Condition 2 (y) Output (x OR y) Output (x AND y)

a>b= True A<c=False True OR False = True AND False =

True False

a>b= ? a<b= ? ? ?

a<b=? a>b= ? ? ?

a<b=? a<c= ? ? ?

7. Covert the following Algebraic Expressions into QBASIC Expressions:

a. A = 2( L + B) b. SI = PTR/100
c. A = ½ (b x h) d. F=180/100C + 32
e. (a2 – b2)=(a + b) (a – b) f. a2 + 2ab + b2

262 GREEN Computer Book-8

21 QBASIC Statements and

Library Functions

QBASIC Statement is an instruction used in QBASIC to perform a specific task
in a program. It helps to solve problems with the help of a computer.
All the command words have to be written using some standard rules, which
are called “Syntax Rules”. Syntax is the grammar of writing a statement in a
language.

Some of the statements are :

Clearing the screen

CLS

This statement is used as the first line of every program so that the screen is
cleared each time the program runs. It helps the program to start with a fresh
screen.
You can insert the CLS statement anywhere in a program.
Syntax: CLS

REM Statement

This command is used to put remarks to a program or a line of statement to
make the code more understandable.
REM statement is used throughout the program to explain what your code is
trying to accomplish.
You can type (') apostrophe instead of REM statement.
Syntax: REM remarks

GREEN Computer Book-8 263

LET Statement

It is used to assign value to a variable. It is optional.
Syntax: [LET] variable = expression

LAB Practice 1

CLS
REM program to find sum of two numbers
LET a= 10
LET b= 15
LET sum = a+b
PRINT sum
END

PRINT Statement

It displays the value or values of variables on the screen. The variables and the
values are used to the right of PRINT statement.
Syntax: PRINT [prompt string];expression
Where, the prompt here means the message you keep enclosed within the
double quotation marks. Generally, it is a string constant defining the type of
input in the variable.

LAB Practice 2

CLS
REM program to print your name
LET Name$= “Isha”
PRINT “My name is”;Name$
END

264 GREEN Computer Book-8

LAB Practice 3

REM program demonstrating print statement
CLS
INPUT “Enter a name of student :”; N$
INPUT “Enter marks in English”; Eng
INPUT “Enter marks in Nepali”; Nep
INPUT “Enter marks in Science”; Sci
INPUT “Enter marks in Computer”; Comp
Total = Eng+Nep+Sci+Comp
PRINT “The student is”; N$
PRINT “Marks in English is”; Eng
PRINT “Marks in Nepali is”;Nep
PRINT “Marks in Science is”;Sci
PRINT “Marks in Computer is”;Comp
PRINT “The total score in four subjects is”; Total
END

The above program will get executed as described below:

Enter a name of student? Bikram

Enter marks in English? 80

Enter marks in Nepali? 82

Enter marks in Science? 85

Enter marks in Computer? 90

The student is : Bikram

Marks in English is 80

Marks in Nepali is 82

GREEN Computer Book-8 265

Marks in Science is 72
Marks in Computer is 90
The total score in four subjects is 337

READ.....DATA statement

READ statement accepts data to a variable which are stored in DATA
statement. More than one variable name can be used with READ statement.
DATA statement supplies data to the variables in READ statement.
Syntax: READ variable list
DATA value list
Example: READ Name$, Add$, salary
DATA Shanvi, Sanepa, 5000
In this example, the variable Name$ will read the data Shanvi from DATA
statement. Likewise, Add$ and salary will read the data Sanepa and 5000 from
the DATA statement respectively.

LAB Practice 4

Type the following codes in QBASIC screen and run the program.
REM PROGRAM USING READ DATA STATEMENT
CLS
READ Name$, Add$, salary
DATA Shanvi, Sanepa, 5000
PRINT Name$, Add$, salary
END

Note: Run the program by pressing F5 or Shift + F5.

LAB Practice 5

REM to find area of circle using READ – DATA statement
CONST PI = 3.1416

266 GREEN Computer Book-8

READ r
LET a = PI * r^2
PRINT “Area of circle:”; a
DATA 5
END

END Statement

END statement is used to end the program. It assures you that the program is
complete.

INPUT Statement

INPUT statement allows a user to enter values to variable during program
execution. This is the most commonly used statement for entering the values.
Syntax: INPUT [;] [prompt string] [;][,] variable, [variable, ....]

Understanding Syntax

INPUT Keyword in capital represents BASIC statement.

[;] It causes the cursor to remain in the same line after the user
presses Enter key.

Prompt string It displays a prompt to input. The string must be enclosed
within double quotes.

[;] It prints a question mark (?) at the end of the prompt string.

Variable It denotes a valid variable name to be used.

[Variable, ...] It denotes other variable names that can be typed separated
by a comma (,).

CONST statement

Purpose: Declares one or more symbolic constants.
Syntax:
CONST constantname = expression [,constantname = expression]...

GREEN Computer Book-8 267

LAB Practice 6

CLS
LET R= 5
CONST PI = 3.141593
PRINT "Area = "; PI * R ^ 2
END

Conditional Branching Statement

This type of statement is executed only when a given condition is satisfied.
For example: IF…THEN…ELSE statements.

IF...THEN…ELSE Statement

It is used to make comparison between two or more values. The statement is
also used while making decisions.
Syntax:
IF condition THEN
(block of statement1 to be executed)
ELSE
(block of statement2 to be executed)
END IF
The condition following the IF statement is processed first. If the condition is
true, statements which appear after THEN statement will be processed. If the
condition is not true, then the ELSE statement will be executed.

LAB Practice 7

REM program to enter a number and check it is less than 10 or not
CLS
INPUT "Enter a number: "; N
IF N < 10 THEN
268 GREEN Computer Book-8

PRINT "It is less than ten";N
ELSE
PRINT "it is more than ten";N
END IF
END

LAB Practice 8

REM program to enter any two numbers and check find the greater one
CLS
INPUT "Enter first number: "; N1
INPUT "Enter second number: "; N2
IF N1> N2 THEN
PRINT "the first number is greater";N1
ELSE
PRINT "the second number is greater";N2
END IF
END

IF...…ELSE/IF……END/IF Statement

These blocks of statements allow you to have more than one IF…THEN
statements in the same program.

Syntax: More to know
IF condition1 THEN
statement block1 When the program encounters the
ELSEIF condition2 THEN IF statement, the program tests the
statement block 2 first condition1. If the first condition1
ELSEIF condition 3 becomes true, the program executes
the firs statement block1. If condition1
is false then it checks condition2….. if
all conditions are false then it executes
the statement block after ELSE.

GREEN Computer Book-8 269

statement block 3
ELSE
Statement block 4
END IF

LAB Practice 9

REM program to enter any three numbers and find the greatest number

CLS

INPUT “ Enter the first number”, a

INPUT “ Enter the second number”, b

INPUT “ Enter the third number”, c

IF a>b AND a>c THEN

PRINT “ First number is greatest”;a

ELSEIF b>a AND b>c THEN

PRINT “ Second number is greatest”;b

ELSEIF c>a AND c>b THEN

PRINT “ Third number is greatest”;c

ELSE

PRINT “ All are equal”

END IF

END

Statements variable Operators output
CLS, INPUT a, b, c > relational
Third number is
IF…THEN..ELSE, all are numeric AND logical greatest 20
operator
PRINT, END variable

Description Table

270 GREEN Computer Book-8

Press F5
Enter the first number? 10

Enter the second number? 15

Enter the third number?20

Third number is greatest 20

LAB Practice 10

REM Program to enter any number from 1 to 7 and print the matching day
CLS
INPUT “Enter any number ”; Num
IF Num =1 THEN
PRINT “It’s Sunday”
ELSEIF Num =2 THEN
PRINT “It’s Monday”
ELSEIF Num =3 THEN
PRINT “It’s Tuesday”
ELSEIF Num =4 THEN
PRINT “It’s Wednesday”
ELSEIF Num =5 THEN
PRINT “It’s Thursday”
ELSEIF Num =6 THEN
PRINT “It’s Friday”
ELSEIF Num =7 THEN
PRINT “It’s Saturday”
ELSE
PRINT “Invalid number”
END IF
END
on

GREEN Computer Book-8 271

SELECT CASE statement

Select case is another conditional statement. It is almost like IF...THEN...ELSE.
What if you have 5 or 6 friends that might use your computer and you want
the computer to say something different to each of them?
Syntax:
SELECT CASE <variable>
CASE <value1>
(Block of statement 1 to be executed)
CASE <value2>
(Block of statement 2 to be executed)
CASE <value3>
(Block of statement 3 to be executed)
.
.
.
.
.
END SELECT

CLS
INPUT "Enter your name: ", Name$
SELECT CASE Name$
CASE "Ram"
PRINT "Greetings, oh powerful master"
CASE "Hari"
PRINT "Go away!"

272 GREEN Computer Book-8

CASE ELSE
PRINT "Hello, "; Name$; ". How are you?"
END SELECT
END
SELECT CASE first checks Name$ for the value "Ram". If it finds it, it does the
PRINT after the CASE "Ram". When the PRINT is done, it skips over the rest
of the CASEs. It keeps checking against each CASE until it gets to CASE ELSE.
If it hasn't found anything, it will do whatever is after the CASE ELSE.
SELECT CASE can also be used with numbers as well as strings. Here's a quick
example:
CLS
INPUT "Enter a number: ", Number
SELECT CASE Number
CASE 1234
PRINT "Thank you for entering the secret number 1234"
CASE 22
PRINT "Well, 22 is an interesting number"
CASE ELSE
PRINT "You must not know the secret number"
END SELECT
END

Library Functions

Library Functions are the set of executable block of instructions capable of
doing some specific tasks. They are also called built-in functions as they
are ready to be used. They are programmed by the software developer and
attached along with the software. Library Functions are of two types. They are
String Functions and Mathematical.

GREEN Computer Book-8 273

Functions take data or variables inside the parenthesis (), i.e. bracket and is
known as argument. They are used along with other QBASIC statements.

Generally, the String Functions give out string data as output after processing
of the data whereas Mathematical Functions give number as an output after
the processing.

Some of the Library Functions are:

1. UCASE$( )

This function converts the supplied string to capital letters. You can directly
supply a string data enclosed within inverted comma or a string variable
containing string data. This can be categorized under String Function.

Syntax : UCASE$ (string/variable)

Example1 : UCASE$ ("kathmandu is the capital city of nepal.")

This will change the text "kathmandu is the capital city of nepal." to capital
letters as "KATHMANDU IS THE CAPITAL CITY OF NEPAL."

Example2 : n$="kathmandu is the capital city of nepal."

UCASE$(n$)

It converts the text "KATHMANDU IS THE CAPITAL CITY OF NEPAL."

2. LCASE$( )

This function changes the supplied string into lowercase characters i.e. small
letter alphabet.

You can directly supply a string data enclosed within inverted comma or a
string variable containing string data. This is also a String Function.

Syntax : LCASE$(string or variable)

Example1 : LCASE$("I Love Nepal")

It converts the text into lower case “i love nepal”

Example2 : n$="I Love Nepal "

LCASE$(n$)

It converts the text into lower case “i love nepal”
274 GREEN Computer Book-8

3. LEFT$( )

This function returns specified number of characters from the left most position
of a string.
Syntax : LEFT$(string, number)
Example1 : LEFT$("Janakpur",4)
It returns 4 characters from the left of the string “Janakpur” i.e. Jana.
Example2 : L$="Janakpur"
LEFT$(L$,3)
It returns four characters from the left of the string "Janakpur" i.e.

Jan.

Solved Example

REM program to extract number of characters from left most position of the
string
CLS
L$="COMPUTER"
PRINT LEFT$(L$,3)
END
This will print three characters from left of the string COMPUTER i.e. COM.
Use of LEFT$ with FOR..NEXT
CLS
C$="NEPAL"
FOR J=1 TO 5
L$= LEFT$(C$,J)
PRINT L$;
NEXT J
END

GREEN Computer Book-8 275

Dry run helps you to understand how the program will be executed.

J= 1 TO 5 L$=LEFT$(C$,J) Output (L$) NEXT J (J=J+1)

1 L$=LEFT$(“NEPAL”,1) N 1+1=2

2 L$=LEFT$(“NEPAL”,2) NE 2+1=3

3 L$=LEFT$(“NEPAL”,3) NEP 3+1=4

4 L$=LEFT$(“NEPAL”,4) NEPA 4+1=5

5 L$=LEFT$(“NEPAL”,5) NEPAL 5+1=6 FALSE

4. RIGHT$( )

This function returns specified number of characters from the right side of the
supplied string.
Syntax : RIGHT$(string, number)
Example1 : RIGHT$("COMPUTER", 3)
This returns three characters from the right of the string COMPUTER i.e. TER.
Example2 : C$="COMPUTER"
RIGHT$(C$,5)
This will return five characters from the right of the string COMPUTER i.e.
PUTER.ession

Solved Example

CLS
C$="NEPAL"
FOR J=1 TO LEN(C$)
R$= RIGHT$(C$,J)
PRINT R$
NEXT J
END
276 GREEN Computer Book-8

Dry run helps you to understand how the program will be executed.

J= 1 TO LEN(C$) R$=RIGHT$(C$,J) Output (L$) NEXT J (J=J+1)
1 R$=RIGHT$(“NEPAL”,1) L 1+1=2
2 R$=RIGHT$(“NEPAL”,2) AL 2+1=3
3 R$=RIGHT$(“NEPAL”,3) PAL 3+1=4
4 R$=RIGHT$(“NEPAL”,4) EPAL 4+1=5
5 R$=RIGHT$(“NEPAL”,5) NEPAL 5+1=6

5. MID$( )

This function returns specified number of characters from the specified
position of a supplied string.
Syntax : MID$(string, position, number)
Example1 : MID$("COMPUTER", 4, 3)
This will return three characters of the string COMPUTER starting from the
fourth position of the string i.e. P.
It will return PUT.
Example2 : C$="COMPUTER"
MID$(C$, 2, 4)
This will return four characters of the string COMPUTER starting from the
second position of the string i.e. O.
It will return OMPU.

Solved Example

REM program to reverse a string
C$="NEPAL"
FOR J=5 TO 1 STEP -1
M$=MID$(C$,J,1)
REV$=REV$+M$
NEXT J

GREEN Computer Book-8 277

PRINT "REVERSED STRING IS";REV$
END
Dry run helps you to understand how the program will be executed.

J= 5 TO M$=MID$(C$,J,1) REV$=REV$+M$ Output J=J-1
1 (REV$)
5-1=4
5 M$=MID$(“NEPAL”,5,1) “”+“L” 4-1=3
4 3-1=2
3 M$=MID$(“NEPAL”,4,1) “L”+ “A” 2-1=1
2 1-1=0
1 M$=MID$(“NEPAL”,3,1) “L”+“A”+“P” FALSE

M$=MID$(“NEPAL”,2,1) “L”+“A”+“P”+“E”

M$=MID$(“NEPAL”,1,1) “L” + “A” + “P” + “LAPEN”
“E” + “N”

6. LEN ( )

This function counts numbers of characters that are present in the supplied
string. A space is counted as one character if there is space used in the string.
It is a mathematical function.
Syntax : LEN(string)
Example1 : LEN("COMPUTER")
This will count number of characters in the supplied string COMPUTER and
returns 8 as there are eight characters in the string.
Example2 : C$="QBASIC is a high level language"
LEN(C$)
This will count number of characters in the string including spaces. It will
return 31.

7. SQR( )

This function returns square root of a supplied number. It is a mathematical
function.

Syntax : SQR(number)

Example1 : SQR(25)

This will return square root of 25 i.e. 5.

278 GREEN Computer Book-8

Example2 : N=100
SQR(N)
This will return square root of 100 i.e. 10.

Solved Example

REM program to display the square root of a supplied number
CLS
INPUT "Enter a number"; N
SQ=SQR(N)
PRINT "Square root of "; N; " is ";SQ
END

8. ASC( )

This function returns the ASCII value of a supplied character. ASCII stands for
American Standard Codes for Information Interchange. There are 256 ASCII
values ranging from 0 to 255 representing different characters, digits and
symbols.

Syntax : ASC(character)

Example1 : ASC("A")

This will return the ASCII value of the letter A. The letter A is represented by
65 in ASCII.

So, it will return 65.

Solved Example

REM program to display A to Z
CLS
FOR J=64 TO 92
PRINT ASC(J),
NEXT J
END

GREEN Computer Book-8 279

POINTS TO REMEMBER

1. IF……….THEN……….ELSE is a conditional statement.

2. The combination of operators, variables and constants is known as an
expression.

3. QBASIC Statement is the word used in QBASIC to perform specific task
in a program.

4. CLS clears the screen each time you run the program.

5. REM statement is used throughout the program to explain what your
code is trying to accomplish.

6. PRINT statement displays the value or values of variables on the screen.

7. Input allows user to enter value to variable during program execution.

8. Library Functions are the set of executable block of instructions capable
of doing some specific tasks.

9. UCASE$ () function converts the supplied string to capital letters.

10. LCASE$() function converts the supplied string to small letters.

11. MID$() function returns specified number of characters from the
specified position of a supplied string.

12. LEN() function counts numbers of characters that are present in the
supplied string.

Exercise

1. Write the syntax and purpose of the following statements and functions.

a. INPUT b. PRINT c. IF…THEN….ELSE

d. SELECT…..CASE e. LEN( ) f. MID$( )

g. LEFT$( ) h. RIGHT$( ) i. UCASE$( )

j. LCASE$( ) k. SQR( ) l. ASC( )

2. Write a program for the following.
a. To calculate area of a rectangle. [A=l x b]
b. To calculate perimeter of a rectangle. [P=2( l + b )]
c. To calculate area of a circle. [A=pr]

280 GREEN Computer Book-8

d. To calculate perimeter of a circle. [P=2pr]
e. To calculate sum of two different numbers.
f. To calculate product of two different numbers.
g. To calculate difference of two different numbers.
h. To input three different numbers and decide the smallest number

amongst the three using IF…THEN statement.
i. To input days of a week and decide “school day” or “holiday” using

IF…THEN statement
j. To decide whether an input number is divided by 5 and 3 or not?
k. To input any number from 0 to 9 and check whether it is single digit

number or not using SELECT CASE statement.
l. To input name of the SAARC country and print name of their capital

city using SELECT CASE statement.

3. WAP to perform the following using library function
a. Write a program to return MAN from the word KATHMANDU.
b. Write a program to return NEP from the word NEPAL.
c. Write a program to return PAL from the word NEPAL.
d. Write a program to return ASCII value of letter 'a'.
e. Write programs to display following pattern:
i. N
NE
NEP
NEPA
NEPAL
ii. N E P A L
NEPA
NEP
NE
N
f. Write a program to return RETUPMOC from the word COMPUTER.

GREEN Computer Book-8 281

22 Looping in Qbasic

Looping is another name for iteration where a set of statements executes
more than once. A loop is defined as a set of instructions that repeat a block of
statements to the given number of times or till the given condition is satisfied or
until a certain condition becomes true. Programming process jumps forwards
and backwards in looping. A loop helps us to do more work with a lesser
number of statements.

There are three loop statements in QBASIC:

1. FOR ……….NEXT More to know

2. WHILE ……………. WEND and Use the CTRL+ pause break
3. DO ………….. LOOP key to stop the infinite loop.

a. DO WHILE/ UNTIL……………..LOOP

b. DO ……………LOOP WHILE/ UNTIL

FOR ……….NEXT Statement

Purpose: Repeats a block of statements for a specified number of times.
Syntax:
FOR counter = start TO End [STEP increment/ Decrement]
[block of statement to be executed]
NEXT counter

Where, counter is a numeric variable used as the loop counter. Start and end
are the initial and final values of the counter. Increment is the amount the
counter is changed each time through the loop.

282 GREEN Computer Book-8

LAB Practice 1 Dry Run

CLS CNT Output(CNT) CNT=CNT+1

REM to print first 5 natural numbers. 1 1 1+1=2

FOR CNT = 1 TO 5 22 2+1=3
PRINT CNT; 33 3+1=4
NEXT CNT 44 4+1=5
55 5+1=6 False

END Note: If you don’t write step then

LAB Practice 2 qbasic suppose +1 itself

Write a program to print the sum of first 5 even numbers.

CLS

a=2

Sum=0

For i= 1 to 5
PRINT “Sum of the numbers:”; Sum

Sum = Sum + a

a=a+2

NEXT i

END

ai Sum=Sum+a Dry Run a=a+2 I=i+1
21 Sum=0+2 Sum(output) 2+2=4 1+1=2
42 2 4+2=6 2+1=3
63 2+4 6 6+2=8 3+1=4
84 6+6 12 8+2=10 4+1=5
10 5 12+8 20 10+2=12 5+1=6
20+10 30

LAB Practice 3

REM to generate the following numbers 2,4,6,8,10,…50.

CLS

FOR I = 2 to 50 Step 2

PRINT I

GREEN Computer Book-8 283

NEXT I
END
LAB Practice 4
REM generates the following numbers 5,10,15,…90
CLS
FOR I = 5 to 90 Step 5

PRINT I
NEXT I
END

WHILE …………WEND statement

Purpose: Executes a series of statements as long as the specific condition is
true.
Syntax:
WHILE condition
Statement block
WEND

Flow chart of WHILE …..WEND statement

Start

Initial value of the counter

Yes Is the No
condition
true?

Display output Stop

Increase/Decrease the
value of the counter

284 GREEN Computer Book-8

Let us see an example to understand WHILE...WEND more clearly.

LAB Practice 5 Start
I=1
REM to print first 10 natural numbers.
CLS Yes No
I=1 Is I < = 10
WHILE I<=10
Print I
PRINT I ;
I=I+1 Stop
WEND
END

LAB Practice 6 I=I+1

REM generates the following numbers 2,4,6,8,10….50

CLS

I=2

WHILE I < =50

PRINT I;

I=I+2

WEND

END

LAB Practice 7

REM to print numbers stated below 1,3,5,7,9,…99
CLS
I=1
WHILE I <=99

PRINT I;
I=I+2

GREEN Computer Book-8 285

WEND
END

LAB Practice 8

REM to print numbers stated below1,4,9,…up to 10th term.
CLS
I=1
WHILE I < =10

PRINT I^2;
I=I+1
WEND
END

LAB Practice 9

REM to print numbers stated below10,9,8,7…up to 1.
CLS
I=10
WHILE I > = 1

PRINT I;
I=I-1
WEND
END

Exercise

1. Answer the following questions.
a. What is looping?
b. List out the types of loop statements in QBASIC.
c. Write the purpose and syntax of FOR….NEXT statement.
d. Write the purpose and syntax of WHILE…..WEND statement.
e. What happens if you don’t write stem in FOR… NEXT statement?

286 GREEN Computer Book-8

2. Debug the following program:

CLS CLS

REM to print your name 5 times REM to print table of 9.

INPUT “ Enter your name: $N P=9

FOR A= 1 TO 5 STEP -1 C=1

PRINT “My name is:”;$N WHILE C<=10

NEXT A T=P*C

END PRINT T

C=C-1

WEND

END

CLS

REM to print length and entire character from third position of input string

INPUT “Enter a string”; N$

L = LEN$(N$)

ch= MID(N$, 3)

PRINT L, ch

END

3. Write a program for the following:
a. To print multiplication table of 7.(eg. 7x1=7) [FOR..NEXT]
b. To print your mother’s name 15 times. [WHILE…WEND]
c. To print numbers from 15 to 1. [FOR..NEXT]
d. To print sum of odd numbers between 2 and 20. [WHILE…WEND]
e. To print the series 1, 8, 27…..up to 10th terms.
f. To print the series 1,1,2,3,5,8……….up to 10th terms.
GREEN Computer Book-8 287

g. To print the following output:

i. 5, 10, 15……up to 50

ii. 70, 65,60…..up to 10

iii. 5, 55, 555 up to 5th terms

h. Write a program to print the following output of “NEPAL”

i. N ii. N

NE E

NEP P

NEPA A

NEPAL L



iii. LAPEN iv. NEPAL

NEPA

NEP

NE

N

i. Write a program to print input string into reverse order.

288 GREEN Computer Book-8


Click to View FlipBook Version