Vedanta Let’s Log in Computer Science | Book 9
b) What is the condition to eat supplied food item by vegetarian?
c) Write the execution steps of the program if the input is “Chicken Chowmein”.
d) What is the condition to eat supplied food item by non-vegetarian?
e) Write the execution steps of the program if the input is “Khir (Rice Pudding)”.
2. Refer the given flowchart and answer the questions.
Start a) How many times the statement Print N is
executed?
N =1 Ans:
Print N b) What is the value of N when the program is
stopped?
S=S+N
N=N+1 Ans:
c) What value of S is printed when the program
is executed?
Ans:
Is Yes d) What happens if N = N + 1 is replaced by
N<=10? the statement N = N – 1?
No .................................................................
Print S .................................................................
Stop .................................................................
e) What happens if Is N <= 10 is replaced by
the Is N >= 10?
.................................................................
.................................................................
.................................................................
201 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
Vedanta Let’s Log in Computer Science | Book 9
Unit
4.2
4.2.1 Introduction to QBASIC
Chapter Includes:
How to start QBASIC?
Features of QBASIC
QBASIC Interface
Introduction to QBASIC
You have used many software like MS-Word, VLC Player, Google Chrome, and
many others to do different tasks in computer. These softwares help us to provide
instructions to the computer so that your computer knows what you want to do.
Now, you are going to learn the concept of programming. It gives you the idea of
developing software.
QBASIC is a high level programming language. The full form of QBASIC is Quick
Beginner’s All-purpose Symbolic Instruction Code. It is simple and easy to learn.
Using QBASIC, you can tell the computer what to do, using statements in English
language with some mathematical notation. The computer follows each instruction
one by one, and does exactly what you tell it to do. In 1964, John G. Kemeny and
Thomas E. Kurtz designed the original BASIC language at Dartmouth College.
Starting QBASIC
QBASIC may not be on your computer. At first, you need to have QBASIC in your
computer. It is freely available. You can download it from the internet as well.
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 202
Vedanta Let’s Log in Computer Science | Book 9
QBASIC
In Windows XP,
If you are not using QB64, you
can download QBASIC from
the internet and save it in any
location inside your computer.
For instance, we have saved it
in D:\drive with folder name
QBASIC.
Go inside the folder D:\
QBASIC and double click on
the file QB.EXE.
Now, you see the QBASIC window where you have to write QBASIC
statements to make your program.
In Windows 7, 8 or 10
Since the QBASIC is an old programming language, you cannot directly
open this version of QBASIC like in Windows XP. You need to install
an emulator program like DOSBox.
Use the following steps to run QBASIC using DOSBox.
1. Download DOSBox from https://www.dosbox.com/
2. Install DOSBox
by simply double
clicking on the
installation file.
3. After the
installation of
DOSBox, its
shortcut icon
appears on the
desktop.
4. Open the QBASIC
folder and select
the file QB.EXE.
203 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
Vedanta Let’s Log in Computer Science | Book 9
5. Drag and drop on the icon of DOSBox on your desktop.
6. QBASIC opens inside DOSBox.
QB64
QB64 is developed with the same features of QBASIC to run in Windows 7, 8 or 10
without DOSBox. We can easily write, edit, debug and execute QBASIC program
in QB64.
1. Download QB64 from https://www.qb64.org/ or
https://vedantapublication.com.np/downloads.
2. Unzip or extract the file.
3. Double click on QB64.exe to run QBASIC.
In this chapter, you learn programming using QB64 IDE (Integrated
Development Environment).
Title Bar
Menu Bar
QBASIC Code
Displays the status of code (Displays error message for any syntax error)
Status Bar
QBASIC IDE
QBASIC IDE
IDE is an editor. It checks the code you write to find if there is any error. It is
a menu drive interface, which provides you with a window having the following
features:
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 204
Vedanta Let’s Log in Computer Science | Book 9
Title Bar
It contains the current filename.
Menu Bar
It has 6 menus – File, Edit, View, Search, Run and Option. Each menu has a sub-
menu with several options.
Status Bar
It shows the current line and column of cursor.
Features of QBASIC
It is a user-friendly language.
It is simple and easy to learn.
It is widely known and accepted programming langauge.
Variables can be named easily. It uses simple English phrases with
mathematical expressions.
It has dynamic program debugging.
It supports the local and global variable.
It supports the modular programming.
It capitalizes the reserved words automatically.
The syntaxes of the statements are checked automatically.
Notes: Extra bullet to be designed.
QBASIC Interface
The File Menu
The File Menu is used for different file
operations such as New, Open, Save,
Save As, and Exit.
Click on File Menu
Keyboard Shortcut:
Press on ALT + F to select File
Menu.
205 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
Vedanta Let’s Log in Computer Science | Book 9
A drop-down menu appears with several options.
In each sub-menu, one letter is highlighted in White color. This letter (hotkey)
is the shortcut to select the option.
Opening a new file
This option of File menu is used to write a new program.
Click on File Menu > New
Keyboard Shortcut
Press on ALT + F to select File menu, and press on N for New option.
Opening a New File
Opening an existing file
This option of File menu is used to open a program file, which you have already
created before.
Click on File Menu > Open
Keyboard Shortcut
Press on ALT + F to select File menu, and press on O for Open option.
An Open dialog box appears.
Select the required filename.
Click on OK button.
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 206
Vedanta Let’s Log in Computer Science | Book 9
Opening an existing file
Saving your program
Save option in File menu allows you to save your program file in the secondary
storage like hard disk.
Click on File Menu > Save
Keyboard Shortcut
Press on ALT + F to select File menu, and press on S for Save option.
A Save As dialog box appears.
Type the filename with the extension .bas.
Click on OK button.
Saving a program file
207 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
Vedanta Let’s Log in Computer Science | Book 9
Note:
Save As Option
This option is used to save an already saved file with another name in same place
or in other location.
Exit from QB64
Exit option in File menu allows you to exit
from QBASIC.
Click on File Menu > Exit
Keyboard Shortcut
Press on ALT + F to select File menu,
and press on X for Exit option.
The Run Menu
The Run option allows you to run or execute your QBASIC program.
Click on Run Menu > Start
Note: Before executing your program, the QB64 creates an exe (executable) file. The
executable file (have extension .exe) is an independent file which you can run in any computer
without QB64.)
Keyboard Shortcut
Press on F5 key to run your program.
Your First Program
Just type the statements below in the IDE:
PRINT “Hello Friends!!!”
END
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 208
Vedanta Let’s Log in Computer Science | Book 9
Running (executing) your program.
Click on Run Menu > Start
or
Press on F5 button of your keyboard. Now, you see the output screen as below:
This is a sample program to display the words “Hello Friends!!!” on the output
screen.
The Help Menu
The Help menu guides you how to use a particular keyword or command in QBASIC.
Click on Help Menu > View
Keyboard Shortcut
Press on ALT + H to select Help menu, and press on V for View option.
or
Help menu in QBASIC
Press on F1
Note: Press on Esc key to exit from help.
209 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
Vedanta Let’s Log in Computer Science | Book 9
Full Form:
QBASIC - Quick Beginner’s All-purpose Symbolic Instruction Code
IDE - Integrated Development Environment
Shortcut Keys: - Shortcut Keys
- ALT + F
Tasks - ALT + F + N
To select file menu - ALT + F + O
To open a new file - ALT + F + S
To open an existing file - ALT + F + X
To save your program file - F5
To exit from QBASIC - ALT + H + V or F1
To run program Esc
To get help
To exit from help
POINTS TO REMEMBER
QBASIC is a high level programming language. It is simple and easy to learn.
If you are using QBASIC in Windows 7, 8 or 10, you might have to require emulator
program like DOSBox.
You can use QB64 with the same features of QBASIC to run in Windows 7, 8 or 10
without DOSBox.
IDE is an editor where the code you write is checked to find if there is any error.
Exercise
1. Answer the following questions:
a) Why is QBASIC called high level programming language?
b) List any four features of QBASIC.
c) List the steps of writing and executing a program in QBASIC.
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 210
Vedanta Let’s Log in Computer Science | Book 9
2. Write down the algorithm to do the following tasks:
a) To open an existing file
b) To save a program file
c) To exit form QBASIC editor
3. List the shortcut keys to do the following tasks:
a) To run the program
b) To open and existing file
c) To see the help of QBASIC program
d) To exit from QBASIC editor
Lab Activities
1. Run QBASIC in your computer and practice the following tasks using below shortcut
keys.
Tasks Shortcut Keys
To select file menu ALT + F
To open a new file ALT + F + N
To open an existing file ALT + F + O
To save your program file ALT + F + S
To exit from QBASIC ALT + F + X
To run program F5
To get help ALT + H + V or F1
To exit from help Esc
2. Run QBASIC and write a program to display your any two friend's name.
211 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
4.2.2 Vedanta Let’s Log in Computer Science | Book 9
Data Types in QBASIC
Chapter Includes:
Data and Data type in QBASIC
Elements of QBASIC
Variable and Constants
Data Type Declarations
Data and Data Type
Data is any value (raw or unprocessed) given to the program for processing. For
example, if you have to add any two numbers, you have to supply the numbers.
These numbers are data. These data are accepted by your program. With suitable
instruction, the numbers are added to give you back the sum as an output or
information.
Data Type
Computer’s memory has to hold data for calculation. You (programmer) must tell
the computer what type of data to hold. This is called a data type. 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:
Integer Non-floating-point numbers from -32,768 to 32,767
Long Integer Examples: -34, 987, 0
Single Precision Non-floating-point numbers from -2,147,483,648 to 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)
Examples: 3.45, 31.4567, -35.23, 0.45, -3.15, 890, -98
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 212
Vedanta Let’s Log in Computer Science | Book 9
Double Precision Floating-point numbers from -1.67x10^308 to 1.67x10^308 (up to
15 digits)
Examples:
723661.90226, -128.78, 185321.093423, 2345544, -123456897
Elements of QBASIC
QBASIC is a simple high-level programming language. It has the following
elements:
a) Character Set
Character Set is a group of valid characters and symbols supported by a
programming language.
Alphabets A,B,C, …., X,Y,Z
a,b,c, …., x,y,z
Digits 0,1,2,3,4,5,6,7,8,9
Special Characters , =, +, -, _, *, /, \, ^, %, $, &, !, #, “, ;, ., ‘, ,, ?, <, >
A character denotes any alphabet, digit, or special symbol used to represent
information. The table below shows the valid alphabets, numbers, and special
symbols supported by QBASIC.
Symbols Description
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.
.
Notes: Character sets are combined to form keywords, variables, operators, and functions.
213 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
Vedanta Let’s Log in Computer Science | Book 9
b) Keywords
Keywords are the special words, which have certain meanings in QBASIC.
It is also called Reserved Words. Keywords are statements, library functions and
name of operators. Some examples of keywords in QBASIC’s are CLS, INPUT,
AND, LEN, etc.
Notes: Keywords should not be used as the name of the variables or functions.
c) Constants
Constants are the values or data stored in some memory location that cannot
be changed during the program execution. A constant may be a letter, words, or
numbers. There are mainly two types of constants:
i) Numeric Constant
Numeric constant refers to any positive or negative number with or
without decimal point. It contains numeric digits only.
Some valid numeric constants are:
1238 a positive number without decimal point
14.64 a positive number with decimal point
-67 a negative number without decimal point
-987.32 a negative number with decimal point
Some invalid numeric constants are:
“67” a number enclosed in double quotes
345,489 a number with thousand separator
Notes: Numeric constants are not enclosed in double quotes (“”).
ii) String Constant
String constant refers to a letter, words, numbers or combination of
letters, numbers, and symbols (alphanumeric). Some valid string
constants are:
“Hello Nepal” alphabets and space in double quotes
“124” numbers in double quotes
“[email protected]” alphanumeric characters in double quotes
Notes: A sequence of characters is called a string.
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 214
Vedanta Let’s Log in Computer Science | Book 9
Numeric Constant String Constant
Contains only numeric digits. Contains alphanumeric values.
Should not be enclosed in double Must be enclosed in double
quotes (“ “). quotes (“ “).
Arithmetic operation like addition, Arithmetic operation cannot be
subtraction can be performed performed.
Examples: 23, -5, 67.2 Examples: “Sagarmatha”, “10
Oranges”
d) Variables
Your program requires data for calculation. You need to store these data
in memory (RAM). Microprocessor takes the data from memory and performs
calculations as instructed.
Variables are the names given to the memory locations, where you store the
required data for calculation during the run-time. It is called variable because
its value can be changed when the program is running. Variable allocates certain
space of memory and deallocates when your program is terminated.
Notes: The location of a variable is called its address or reference.
Variable Declaration
Variable allocates space of memory of computer to store data. You need to declare
its name and the type of data the variable holds. QBASIC supports two types of
variable declaration.
i) Implicit Declaration
In this type of declaration, the type of variable is defined by 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.
215 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
Vedanta Let’s Log in Computer Science | Book 9
Implicit Declaration of Variables
Symbol Data type Example
% Integer n%=5
(Percentage Symbol) INPUT “Type an Integer “;V%
& Long n&=5
(Ampersand) Integer INPUT “Type an Integer “;V&
! Single n!=5
(Exclamation Sign) Integer INPUT “Type any number “;V!
# Double n#=5
(Hash Symbol) Precision INPUT “Type any number “;V#
$ String n$=”Nepal”
(Dollar Sign) INPUT “Type your name “;V$
What will happen if you do not type any symbol after variable name?
QBASIC accepts it as a single precision variable.
Example:
INPUT “Type any number “;n
Here, there is no any symbol after the variable ‘n’. The default data date type
in QBASIC is Single Precision. So, you do not need to type exclamation sign ‘!’
after variable name if the data type is single precision.
ii) Explicit Declaration
In this type of declaration, instead of using special symbol:
We use DIM … AS statement.
DIM variable AS type
type, it defines the type of variable
We can use the following keywords:
Explicit Declaration of Variables
Keyword Data type Example
INTEGER
Integer DIM n AS INTEGER
LONG INPUT “Type an Integer “;n
DIM n AS LONG
Long Integer
INPUT “Type an Integer “;n
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 216
Vedanta Let’s Log in Computer Science | Book 9
SINGLE Single DIM n AS SINGLE
DOUBLE Integer
STRING Double INPUT “Type any number “;n
Precision DIM n AS DOUBLE
String INPUT “Type any number “;n
DIM n AS STRING
INPUT “Type your name “;n
Other Examples: DIM A AS STRING, B AS INTEGER
DIM M AS INTEGER, N AS INTEGER
Naming Rules for Variables
Variable name must start with an alphabet.
Variable name cannot contain any special character.
Variable name should not be more than 40 characters.
Keyword cannot be used as variable name.
Variable Name Valid Invalid Reason
1BC × First letter is numeric character
LET × Keyword
Starts with alphabets and $ sign
ABC$ indicates the data type string.
× Invalid symbol is used
BC+
Types of QBASIC Variables
You have to declare the variable according to the type of data the program requires.
Data Type Declaration Byte Length Maximum Minimum
String $ [STRING] 1 byte/char 32,767 chars 0 chars
% 2 bytes -32,768
Integer [INTEGER] 4 bytes 32,767 -2,147,483,648
& [LONG] 4 bytes 3.37x10^38
Long Integer 2,147,483,647
Single ! [SINGLE] 8 bytes -3.37x10^38 1.67x10^308
Precision up to 7 digits
Double # [DOUBLE] -1.67x10^308
Precision up to 15 digits
217 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
Vedanta Let’s Log in Computer Science | Book 9
Exercise
1. Answer the following questions.
a) Define data and data types.
b) What is QBASIC character set?
c) Differentiate between reserved word and user-defined word.
d) Define constant. List its types.
e) What is a variable? Why is it needed in programming?
f) Mention the rules for naming variables.
g) List the different types of numeric variables and describe each of them briefly.
h) What are implicit and explicit declarations of variables? How are they declared in
QBASIC?
2. Find the most suitable variable type for the below data:
Data Variable Type
Computer [STRING, INTEGER, LONG INTEGER, SINGLE
98412165333 PRICESION, DOUBLE PRECISION]
[email protected]
2567
34.12
78976.6782
47382
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 218
Vedanta Let’s Log in Computer Science | Book 9
Data types in QBASIC Worksheets
1. Type the following code in QBASIC and check whether they are valid. Write down the
reason on separate columns.
Code Valid/Invalid Reason
Invalid
A=”Ram” Error Message “Ram” is a string value and
we cannot assign this value
N$=”123” “Type Mismatch” to a numeric variable ‘A’
DIM A AS INTEGER
A = 25.43
num = 56.67
DIM S AS STRING
S=”Nepal”
P%=39000
B# = 728821
C$ = “[email protected]”
DIM Y AS DOUBLE
Y = 45
H% = 23
219 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
Vedanta Let’s Log in Computer Science | Book 9
4.2.3 Operators, Operands and
Expressions
Chapter Includes:
Operators and its types
Expressions and its types
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 ^
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 220
Vedanta Let’s Log in Computer Science | Book 9
D Division /, \, MOD
M Multiplication *
A Addition +
S Subtraction -
Multiplication and Division have the same hierarchy and hence the first come
from left to right has to be dealt first.
Similarly, Addition and Subtraction also have the same hierarchy.
In the expression A*B/C; A is divided by B first. Then, the result is
divided by C.
Similarly, in the expression X-Y+Z, Y is subtracted from X after which Z is
added to it.
ii) Relational (Comparison) Operators
Relational Operators are used to compare two values and return either true
or false.
Assuming that the values of a and b are 10 and 5 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
>= Greater than or equal to a >= b False
<> Not equal to a <> b True
True
iii) Logical Operator
Logical operators are used to combine 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).
221 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
Vedanta Let’s Log in Computer Science | Book 9
Truth Table: Output
X
Input
ABC 1
111 0
110 0
101 0
100 0
011 0
010 0
001 0
000
OR operator
An OR operator returns True (1) as 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).
Truth Table:
Input Output
ABC X
111
110 1
101 1
100 1
011 1
010 1
001 1
000 1
0
NOT operator
The NOT operator (Inverter) performs the logic function called inversion or
complementation. If the input signal is low, high output signal is obtained and vice
versa.
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 222
Vedanta Let’s Log in Computer Science | Book 9
Truth Table: Output
X
Input 0
A 1
1
0
iv) String operator
+(plus) Concatenation
Concatenation is the process of joining two string values.
A$ = “Namaste” The value of C$ is:
B$=”Nepal” NamasteNepal
C$=A$+B$
(b) Operands
Operands are the values on which the operator performs its operation.
For example, in the expression below
C=X+Y
The symbols ‘=’ and ‘+’ are the operators and the variables ‘C’, ‘X’, and
‘Y’ are the operands.
Operator
X+Y
Operand Operand
(c) Expressions
An expression is a set of variables and constants having a definite value
(operands) that is evaluated to get a result.
223 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
Vedanta Let’s Log in Computer Science | Book 9
Example: X = (a+b)^2-2*a*b
X$ = MID$(B$,C,3)
Types of expression
Algebraic Expression
The expressions that are used in mathematical and other fields are called
Algebraic expression.
Example: P = 2 (L+B)
QBASIC Expression
The expressions that can be used in QBASIC programs are called as QBASIC
expressions.
Example: P = 2 * (L+B)
Logical (Boolean) Expression
Any expression that yields true or false value (0 or 1) is known as Boolean expression.
The conditions we check in the programs are Boolean expression.
Example: X<=Y
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 used to evaluate such arithmetic expressions.
Algebraic Expression QBASIC Expression
A+B-C A+B-C
X×Y÷Z X*Y/Z
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 224
Vedanta Let’s Log in Computer Science | Book 9
I = PTR I=(P*T*R)/100
100
S = u*t + (a*t^2)/2
S = ut + 1 at2
2 X=(a+b)*(a-b)
Z=(a/b)*x^2
X=(a+b)(a-b)
m=(a+b+c+d+e)/5
z = a x2
b
m = a+b+c+d+e
5
String Expression
It is an expression where one or more string values with suitable
operator(s) are used to do a certain tasks.
Example: a$ = "QBASIC"+"Programming"
POINTS TO REMEMBER
Operators are the symbols used to represent a particular operation.
Arithmetic Operators: ^, /, \, *, +, -, MOD
Relational (Comparison) Operators: >, <, >=, <=, =, <>
Logical Operators: AND, OR, NOT
AND operators return True if all the conditions are true; otherwise, they returns false.
OR operators return True if any one of the conditions is true; otherwise, they returns
false.
Operands are the values on which the operator performs its operation.
An expression is a set of variables, constants, and operands which are evaluated to
get a definite result.
225 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
Vedanta Let’s Log in Computer Science | Book 9
Exercise
1. Answer the following questions:
a) What is an operator?
b) What are arithmetic operators? List their operations.
c) What are relational operators? List their operations.
d) What are logical operators? List their operations.
e) Write down the truth table of AND, OR and NOT operators.
f) What is an expression? List its types.
2. Write down the QBASIC expression of the following algebraic expression:
a) v= 4 S3 b) F = 9c +32 c) I= PTR
3 5 100
d) A = 2h(l+b) e) b=x3+x2y+xy2+y3 f) x = -b± √(b2-4ac)
20
g) P= a+b+c+d ×100 h) √3 5ab i) x= a2+2ab+b2
400
j) z = x+y + x×y k) P = 2(L+B) l) y=mx+b m) V = Sr2h
x–y x÷y
3. Tick (√) the correct option(s) [Choose all that apply]:
a) +(plus) is an …………………………. operator.
(i) relational (ii) arithmetic
(iii) logical (iv) string
b) AND operator is :
(i) It is an logical operator.
(ii) It returns TRUE if all the conditions are TRUE.
(iii) It combines two relational expressions.
(iv) It returns FALSE if all the conditions are FALSE.
c) To get a remainder, we use …………………………. operator .
i) / (ii) \ (iii) ^ (iv) MOD
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 226
Vedanta Let’s Log in Computer Science | Book 9
d) Which is not the relational operator of QBASIC?
i) >= ii) <= iii) <> iv) !=
e) Which is/are true about OR operator?
i) 1 + 1 = 1 ii) 1 + 0 = 0
iii) 0 + 1 = 1 iv) 0 + 0 = 0
Operators in QBASIC Worksheets
1. Fill the missing part of the tables below:
Let A=6, B=7 and C=8
First Operand Operator Second Operand Result
21 5 1
4 ^ 3
45 \ 10 False
7 7 True
<= B
A = B False
A>=B C<=A False
B<A AND True
OR B>=A True
B<>A AND Ram Shah
"Ram " "Shah" False
A > C True
B True
C>=B <> B>=A 2.5
5 2 True
C True
B>=C B>=A True
A B
227 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
Vedanta Let’s Log in Computer Science | Book 9
4.2.4 Programming in QBASIC
Chapter Includes:
Declaration Statement
Assignment Statement
Input/Output Statement
QBASIC Statements
The instructions within a program are called statements. Each QBASIC statement
uses some specific keyword to perform a specific task. You have to follow some
rules to use QBASIC statements in your program, known as SYNTAX. A QBASIC
statement is either executable or non-executable.
Note: Syntax is the grammar of the programming language.
Types of Statements Assignment Statements
Control Statements
Statements can be categorized in four groups:
Declaration Statements
I/O (Input/Output) 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
Syntax:
REM remark or ' remark
Example:
REM to find the area of a room
CLS
INPUT "Type length & breadth of a room: "; l, b
PRINT "Area = "; l * b
END
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 228
Vedanta Let’s Log in Computer Science | Book 9
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, sub procedure,
etc., which is 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 keyword CONST.
Purpose:
A non-executable statement that declares symbolic constants to use in place
of numeric or string values.
Syntax:
CONST constantname = expression
constantname 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
D=5 CONST D = 5
D = D+5 D=D+5
Possible to change the value of general Not possible to change the value of
constant. symbolic constant.
229 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
Vedanta Let’s Log in Computer Science | Book 9
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 is 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 C AS STRING, B AS INTEGER
Here, 'C' is declared explicitly as string variable and ‘B’ as integer 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 B = 75
y$ = “VEDANTA”
Here, B is a numeric variable and the value 75 is assigned to B. Similarly,
y$ is a string variable and the value “VEDANTA” is assigned to y$.
Note: In QBASIC, a value can be assigned to a variable without LET statement as well.
Example:
LET B=75
The above statement is equivalent to
B = 75
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 230
Vedanta Let’s Log in Computer Science | Book 9
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
x = 50: y = 100
SWAP x, y
PRINT "The value of x = "; x
PRINT "The value of y = "; y
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 every program with CLS statement so that the
previously displayed content is erased before executing new program.
231 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
Vedanta Let’s Log in Computer Science | Book 9
ii) INPUT statement
Purpose: To read input from the keyboard during program execution and
store it into a list of variables.
Syntax:
INPUT[;]["promp tstring"{;|,}]variable list
variable list is one or more variables separated by commas
prompt string, if used, is displayed on the screen to tell the user of the
program what to enter at the keyboard
the characters ";" and "," are the separators
Example:
INPUT “Please type any number “; d
Here, d is a numeric variable.
“Type First Number” is a prompt string
Note: You can also ask more than one value from single INPUT statement.
INPUT “Please type any two numbers:”; d, e
Here, comma (,) is used to separate the list of variables.
INPUT d, e
The above statement is also valid without 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[;] ["prompt string";] string variable
Example:
CLS
LINE INPUT "Type your full address "; a$
PRINT a$
END
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 232
Vedanta Let’s Log in Computer Science | Book 9
iv) PRINT statement
Purpose: To display the output on the screen.
Syntax:
PRINT [expression list][{,|;}]
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:
H = 15
PRINT “My Country My Pride.”
PRINT 51;62
PRINT (6+4 + H) / 2
Note: You must enclose string constant with double quotes (“ ”).
As a shortcut, use a question mark symbol (?) for PRINT.
Type this:
? "Programming is fun."
and press <Enter>.
Other Examples:
PRINT
A blank line is printed.
PRINT 3*5-4+6
Here the expression is 3*5-4+6 and the evaluated value of this expression
17 (3*5-4+6=17) is printed.
PRINT “Computer Science”
String literals “Computer Science” enclosed in quotation marks is printed.
Similarly, the values of variables can be displayed in the following ways.
INPUT “Type your first name ”; F$
PRINT “Your first name is ”;F$
INPUT “Type your age ”;age
233 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
Vedanta Let’s Log in Computer Science | Book 9
PRINT “Your age is ”;age
Or you can write this statement.
PRINT “Your first name is ”;F$; “ and your age is ”;age
Try these statements:
PRINT F$;age
PRINT F$,age
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 "###.##"; 456.239 456.24
PRINT USING "##.##"; .78 0.78
PRINT USING "**#.# "; 317.8; 82.09;-.6 317.8*82.1 *-0.6
PRINT USING "$$###.##"; 782.23 $782.23
PRINT USING "##.## "; 6.2; 15.3; 88.123; .456 6.20 15.3088.120.46
PRINT USING "+##.## "; 3.7;-98.64; 44.3; -.8 +3.70-98.64 +44.30
-0.80
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 234
Vedanta Let’s Log in Computer Science | Book 9
PRINT USING "**$##.##"; 2.45 ***$2.45
PRINT USING "##.##^^^^"; 672.35 6.72E+02
PRINT USING "####,.##"; 3152.7 3,152.70
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$ = "HELLO": 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:
HN
HELLNEPAL
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 "COMPUTER"; TAB(26); "SCIENCE"
235 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
Vedanta Let’s Log in Computer Science | Book 9
vii) INKEY$ statement
Purpose: To read a character from the keyboard.
Syntax:
INKEY$
Example:
PRINT "Press Esc to exit..."
DO
LOOP UNTIL INKEY$ = CHR$(27)
'27 is the ASCII code for Esc.
viii) 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 receives 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 x, y, z
PRINT x, y, z
DATA 14,26,98
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 236
Vedanta Let’s Log in Computer Science | Book 9
Example 2:
READ b$, c
PRINT b$, c
DATA "Computer Science",15
Example 3:
FOR i = 1 TO 5
READ P
S=S+P
NEXT i
PRINT "Sum = "; S
DATA 13,28,45,78,43
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 in
double quotes.
If number of variables in READ statement is more than number of data
in DATA statement, an error message “Out of DATA” is displayed.
Bug and Debug
Bug: A problem or an error in a program is called bug.
Debug: Debug means to solve the problem or make the program bug free.
Types of error or bug
a) Syntax Error
Syntax error is an error in the format of a statement in a computer program
that violates the rules of the programming. This type of error is traced by the
compiler or interpreter and provides the error message to the programmer.
237 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
Vedanta Let’s Log in Computer Science | Book 9
For example,
PRINT "NEPAL" – "Kathmandu"
Here, according to the syntax of PRINT statement, the word NEPAL should be
enclosed in double quotes.
Displays the status of code (Displays
error message for any syntax error)
Error Message
b) Logical Error
A logical error is a bug in a program that causes it to operate incorrectly, and
gives the wrong output. It cannot be traced by the compiler or interpreter.
For example,
INPUT "Type any two numbers "; a,b
c=a+b
PRINT "Product = ";c
END
Here, the program has to calculate the product but the output is their sum. So, the
program runs with incorrect output.
Sample Programs
1. REM asks first name, last name, age & class and displays them
CLS
INPUT "Type your first name "; F$
INPUT "Type your last name "; L$
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 238
Vedanta Let’s Log in Computer Science | Book 9
INPUT "Type your age "; age
INPUT “Type your class “;c
PRINT "Your first name is "; F$
PRINT "Your last name is "; L$
PRINT "Your age is “;age
PRINT “You study in class "; c
END
2. REM to find the product of any two integers
REM Implicit declaration
CLS
INPUT "Type first number "; a%
INPUT "Type second number "; b%
p% = a% * b%
PRINT "Product = "; p%
END
3. REM to find the circumference of a circle
' Symbolic constant
CONST pi = 3.14
INPUT "Radius of a circle "; r
c = 2 * pi * r
PRINT "Area = "; c
END
4. REM to explicit declaration
DIM a AS INTEGER, b AS INTEGER, c AS LONG
CLS
INPUT "Type first number "; a
INPUT "Type second number "; b
c=a*b
PRINT "Product = "; c
END
239 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
Vedanta Let’s Log in Computer Science | Book 9
5. REM to find average using READ ... DATA
CLS
READ p, q, r, s
avg = (p + q + r + s) / 4
PRINT "Average = "; avg
DATA 4, 14, 75, 35
END
POINTS TO REMEMBER
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
A problem or error in a program is called bug and the process of removing a bug is
called debug.
Two types of error: Syntax error and Logical error
Exercise
1. Calculate the output from the following QBASIC expression:
a) (5 * (5 / 5 + 5 * 5 + 5 - 5)
b) (8 + 6 / 3 * 2) * 3
c) 4 * 6 / 6+ 3- (16 * 3)
d) (8 * 4 / 2 * 5) * 7
e) (4 + 4 - 3 * 2 / 4) / 2
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 240
Vedanta Let’s Log in Computer Science | Book 9
2. Write down the syntax and use of the following QBASIC statements.
a) SWAP statement
b) CONST statement
c) PRINT statement
d) INPUT statement
e) READ … DATA statement
3. Debug the following programs.
a) CLS
r$ = Mount Everest
x# = 678
z = "245"
c% = 32.45
d% = 41500
PRINT r$, z, c%, x#, d%
END
b) CLS
p = Rs. 67500
t = 2.5 years
r = 7.5%
I = ptr / 100
PRINT "Simple Interest = "; I
END
c) CLS
READ z$
READ g, r
PRINT "Your good name is "; z$
PRINT "You study in grade "; g
PRINT "Your age is "; r
READ s$
PRINT "Your section is "; s$
241 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
Vedanta Let’s Log in Computer Science | Book 9
DATA Ranju
DATA 4
DATA 10,B,5
END
Lab Activities
1. Re-write the program below using explicit variable declaration.
INPUT "Any two integers ";a%,b%
INPUT "Type your name ";s$
c%=a%+b%
PRINT "The sum is ";c%
PRINT "Your name is ";s$
END
2. Type the statements below in QBASIC and write their output.
Statements Output
PRINT USING "##.##"; 1.36
PRINT USING "###.##"; 17.614
PRINT USING "##.## "; 187.2
PRINT USING "+##.## "; -168.9
PRINT USING "**#.# "; 45.4
PRINT USING "$$###.##"; 4125.18
PRINT USING "**$##.##"; 7.69
PRINT USING "##.##^^^^"; 758.56
PRINT USING "####,.##"; 1452.9
PRINT USING "!"; "Nepal"
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 242
Vedanta Let’s Log in Computer Science | Book 9
3. Write down the QBASIC code for the following problems:
a) Write a program that asks any two numbers and displays their sum, difference, and
product.
b) A salesman has purchased a pendrive at Rs. 700 and he sold it at Rs. 850. Write a
program to calculate profit percent on sale (use explicit declaration).
c) A computer book costs Rs. 340. Write a program that asks the number of books to be
purchased and calculates the total amount to be paid (use LET statement).
d) Write a program that asks radius of a circle and calculates its diameter, circumference,
and area (use symbolic constant).
e) Write a program that accepts the room temperature in Celsius and calculates its
32
equivalent in Fahrenheit. [Hint: F = 5 +32]
f) Write a program to convert the length from feet into centimeter. [Hint: 1 Feet = 30.48
cm]
g) Write a program that reads the data below and calculates their average value.
DATA 15,23,65,35,77,88
h) Write a program that asks length, breadth, and height of a room, and calculates the
volume and perimeter of the room.
243 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
4.2.5 Vedanta Let’s Log in Computer Science | Book 9
Program Flow and
Control Structures
Chapter Includes:
Sequential Structure
Selection Structure
Looping (iteration) Structure
We write several statement to make a program. These statements are executed to
perform calculation when we run the program. Normally, the order of the execution
of the program is from top to bottom. This order of execution of statements of a
program is known as flow of program or program control.
Control Structure
QBASIC provides three basic types of control structures. Control structures are
used to alter the way of execution of program or used to control the flow of a program.
a) Sequential Structure b) Selection Structure c) Looping Structure
(a) Sequential Structure
In sequential structure, the statements are executed one Statement 1
after another sequentially from top to bottom without changing Statement 2
the flow of program. The programs which you have written in
previous chapters are based on sequential structure.
A sample program of sequential structure:
CLS Statement 3
INPUT "Type any two numbers "; a, b
s=a+b Flowchart of a sequential
d=a-b structured program
p=a*b
PRINT "Sum = "; s
PRINT "Difference = "; d
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 244
Vedanta Let’s Log in Computer Science | Book 9
PRINT "Product = "; p
END
(b) Selection Structure
It is also called branching structure. In this structure, the control of the
program is transferred from one part of the program to another on the basis of
specified condition or without condition.
Branching or Jumping
The process of departure 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 unconditional statement. In QBASIC, GOTO
statement is the unconditional statement.
GOTO statement
Purpose: to execute 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 "NEPAL"
GOTO xyz
PRINT "BHUTAN"
PRINT "INDIA"
xyz:
PRINT "SRILANKA"
PRINT "BYE"
END
245 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
Vedanta Let’s Log in Computer Science | Book 9
Output:
NEPAL
SRILANKA
BYE
Here, xyz is a linelabel. After displaying “NEPAL”, the control is transferred to
“xyz” and displayed “SRILANKA” and “BYE” bypassing the statement PRINT
"BHUTAN" and PRINT “INDIA”.
Using linenumber
The program below gives the same output using linenumber.
CLS
PRINT "NEPAL"
GOTO 20
PRINT "BHUTAN"
PRINT "INDIA"
20 PRINT "SRILANKA"
PRINT "BYE"
END
Conditional Statement Condition True
Statement 1
a) IF .. THEN … ELSE statement False
Purpose: Statement 1
It is one of the control flow statements
available in QBASIC that allows
conditional execution or branching,
based on the evaluation of an
expression that must be either true or
false.
QBASIC supports variety of IF statement.
i) Single line IF statement
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 246
Vedanta Let’s Log in Computer Science | Book 9
Purpose: It allows conditional execution based on the evaluation of an
expression that must be true.
Syntax:
IF booleanexpression 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 in computer science ”;c
IF c>=40 THEN PRINT “You have passed.”
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 in computer science ”;c
IF c>=40 THEN PRINT “Pass” ELSE PRINT “You have failed.”
END
Note: statement1 is executed if the booleanexpression returns true; otherwise,
statement2 is executed.
247 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
Vedanta Let’s Log in Computer Science | Book 9
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 “Marks in Computer Science ”;c
IF c>=40 THEN
PRINT “You are Pass”
PRINT “Great Job! 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 “Your marks in computer science ”;c
IF c>=40 THEN
PRINT “You are Pass”
PRINT “Great Job! Congratulations!!”
ELSE
PRINT “You have failed”
PRINT “Sorry! Work hard!!”
END IF
END
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 248
Vedanta Let’s Log in Computer Science | Book 9
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 marks “; M
IF M >= 90 THEN
PRINT "A+"
ELSEIF M >= 80 THEN
PRINT "A"
ELSEIF M >= 70 THEN
PRINT "B+"
ELSEIF M >= 60 THEN
PRINT "B"
ELSEIF M >= 50 THEN
PRINT "C"
ELSEIF M >= 40 THEN
PRINT "D"
ELSE
PRINT "E"
END IF
END
249 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur
Vedanta Let’s Log in Computer Science | Book 9
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
Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 250