Flowchart Samples
Sample 1: Algorithm and Flowchart to find the sum of two numbers. Start
Algorithm
Step-1: Start Input value
Step-2: Input first number A of A
Step-3: Input second number B
Step-4: SUM = A + B Input value
Step-5: Display SUM of B
Step-6: Stop
SUM = A+B
Print SUM
Stop
Sample 2 : Algorithm and Flowchart to convert temperature from
Celsius to Fahrenheit.
C : Temperature in Celsius Start
F: Temperature in Fahrenheit Input value
Algorithm of C
Step-1: Start
Step-2: Input temperature in Celsius C F = (9.0/5.0 × C)+ 32
Step-3: F = (9.0/5.0 × C)+ 32
Step-4: Display temperature in Fahrenheit F Print F
Step-5: Stop
Stop
200 New Gateway to Computer Science Book -9
Sample 3: Algorithm and Flowchart to find the Area and Perimeter of Square.
L: Side Length of Square Start
Area : Area of Square
Algorithm Input value
Step-1: Start of L
Step-2: Input Side Length of Square L Area = L × L
Step-3: Area = L × L
Step-4: PERIMETER = 4 × L PERIMETER
Step-5: Display AREA, PERIMETER =4×L
Step- 6: Stop
Print AREA,
PERIMETER
Stop
Sample 4 : Algorithm and Flowchart to find Area and Perimeter of Rectangle.
L: Side Length of Rectangle Start
B: Breadth of Rectangle
Area: Area of Rectangle Input value
Perimeter : Perimeter of Rectangle of L, B
Algorithm Area = L × B
Step-1: Start
Step-2: Input Length and breadth L, B PERIMETER
Step-3: Area = L × B = 2 × ( L+B:
Step-4: PERIMETER = 2 × ( L+B)
Step-5: Display AREA, PERIMETER Print AREA,
PERIMETER
Step- 6: Stop Stop
New Gateway to Computer Science Book -9 201
Sample 5 :Algorithm and Flowchart to find the smallest number between two numbers.
Algorithm
Step-1: Start
Step-2: Input two numbers
NUM1, NUM2
Step-3: IF NUM1 < NUM2 THEN
PRINT smallest is NUM1
ELSE
PRINT smallest is NUM2
ENDIF
Step-4: Stop
Start
Input Value
of NUM1
Input Value
of NUM2
Print smallest No If Yes Print smallest
is NUM2 NUM1 < NUM2 is NUM1
Stop
202 New Gateway to Computer Science Book -9
Sample 6 : Algorithm and Flowchart to find the largest of three numbers .
Algorithm
Step-1: Start
Step-2: Read three numbers A, B, C
Step-3: BIG = A
Step-4: IF B > BIG THEN
BIG = B Start
ENDIF
Step-5: IF C > BIG THEN
BIG = C Input A, B, C
ENDIF
Step- 6: Write BIG BIG = A
Step- 7: Stop
is Yes BIG = B
B > BIG
No
is Yes
C > BIG BIG = C
No
Write BIG
Stop
New Gateway to Computer Science Book -9 203
Sample 7 : Algorithm and Flowchart to find Even number between 1 to 50.
Algorithm
Step-1: Start
Step-2: I = 1
Step-3: If ( I > 50) THEN Start
GO TO step 7
Step-4: IF( I mod 2 = 0) THEN Read N
Display I
ENDIF
Step-5: I = I + 1 D=1
Step- 6: GO To Step 3
Step- 7: Stop
If No
CTRL<N Stop
Yes
If Yes Print D
(N mod D=0)
No
D = D+ 1
Stop
204 New Gateway to Computer Science Book -9
Sample 8 : Algorithm and Flowchart to find print the multiplication table of a number.
Algorithm
Step-1: Start
Step-2: Input value of NUM
Step-3: I = 1
Step-4: IF( I > 10 ) = 0) THEN
GO To Step 9
ENDIF
Step-5: PROD = NUM* I
Step- 6: WRITE I "X" NUM " = " PROD Start
Step- 7: I = I+1
Step- 8: Go to step 4 I=1
Step- 9: Stop
is Yes
I > 50
No
Yes Print
IF(I mod 2 = 0)
No
I=I+1
Stop 205
New Gateway to Computer Science Book -9
Sample 9 : Algorithm and Flowchart to find all the divisor of a number.
Algorithm Start
Step-1: Start
Step-2: Read number N Input value
Step-3: D = 1 of NUM
Step-4: WHILE (D<N)
I=1
DO is 1 > 10
IF (N mod D = 0) THEN
PRINT D No
ENDIF PROD = NUM*1
D = D+1
Step-5: Stop WRITE I "X" NUM
"=" PROD
I=I+1
Stop
Let’s Review
There are four basic symbols in a program flowchart. They are start, process,
decision and end.
Program flowchart can help programmers to find the bug in the process
before carrying out the task.
The sequence control structure is defined as the straight forward execution of one
processing step after another.
Selection structure is used in flowchart to determine which particular step or set of
steps is to be executed.
When a program goes through a loop for certain times repeatedly, it is known as
repetition which is the process of repeating one or more steps in a process.
The step at which the body starts is known as the loop entrance and the step at
which the test to branch back fails is known as the loop exit.
206 New Gateway to Computer Science Book -9
Assignment
1. Fill in the blanks with suitable words.
a. An …………… is defined as a step-by-step procedure or method for solving a
problem by a computer in a finite number of steps.
b. Every step in an ……………. has its own logical sequence so it is easy to debug.
c. An ………….. must have step-by-step directions which is independent of any
programming code.
d. A ……………… is more understandable as compared to the algorithm.
e. There are no stringent rules implemented in the algorithms while the flowchart
is abided by …………. rules.
f. Aflow chart is a ……………. representation of a system that details the sequencing
of steps required to create output.
g. Each flow chart is ………………… with one particular process or system.
h. There are two types of flowcharts. They are ………………….. and program
flowchart.
i. There are four basic symbols in a program flowchart they are start, process
, ………. and ……..….
j. ………….. is used in flowchart to determine which particular step or set of steps
is to be executed.
2. State whether the given statements are true or false.
a. Algorithms can be expressed in many different notations, including natural
languages, pseudo code, flowcharts and programming languages.
b. An algorithm involves a combination of sequential steps to interpret the logic of
the solution.
c. The algorithm is written in a language that can be perceived by humans.
d. The algorithm is made up using different shapes and symbols.
e. Errors and bugs are very difficult to detect in the algorithm as compared to the
flow charts.
f. Flow charts are simple to create but the construction of the algorithm is complex.
g. With the help of the flowchart, the application designer can easily separate
different components of the process.
New Gateway to Computer Science Book -9 207
h. Program flowchart is the graphical representation of the flow of data in the
system, and represents the work process of the system.
i. The sequence control structure is defined as the straight forward execution of one
processing step after another.
j. The step at which the body starts is known as the loop entrance and the step at
which the test to branch back fails is known as the loop entrance.
3. Answer the following questions.
a. Define algorithm with its characteristics.
b. Write any four advantages of using an algorithm.
c. Define flowchart with its advantages.
d. What are the types of flowchart? Define each in short.
e. Draw the symbols used in flowchart.
f. What is a sequence structure in a flowchart?
g. Define selection and repetition structure with proper diagram.
4. Write an algorithm for the followings.
a. To add any three numbers entered by the users.
b. To calculate the simple interest.
c. To find the product of any three numbers.
d. To display whether the entered number is divisible by 3 or not.
e. To display the smallest number among three different numbers.
5. Draw a flowchart for the followings.
a. To find the product of any three numbers.
b. To convert the temperature from Celsius into Fahrenheit.
c. To find the area of 4 walls.
d. To find the area and perimeter of a square.
e. To find the even numbers from 1 to 100.
f. To display the multiplication table of any given number.
208 New Gateway to Computer Science Book -9
Lesson
12 Programming in QBASIC
Inside the Lesson
Introduction to Qbasic
Features of Qbasic and starting Qbasic
Qbasic Interface
Elements of Qbasic Programming
Qbasic Operators
Qbasic Expressions
QBasic is a high level programming language developed by Microsoft
Corporation for use in the MS-DOS operating system in 1985. It is the successor of
earlier version of BASIC (Beginners All-purpose Symbolic Instruction Code). It is a
programming language for beginners because of its in-built commands, simple structure
and flexibility. It is also called a modular programming language where program
is divided in different modules or procedures. It uses English like keywords and
mathematical symbols to write a program. QBAISC screen has two types of windows
they are Program Window and Immediate Window. Program window is the upper
window which is titled as ‘Untitled’ where we can write programs. To see the output
of the statements written in this window, we have to press Shift + F5 key. Like this,
immediate window is the lower window which is titled as ‘Immediate’. It is the window to
test commands, expressions, etc. When we press the Enter key, it displays the output on
the screen.
Features of QBASIC
It is simple, easy to learn and understand.
It automatically checks syntax.
It allows you to divide large programs into modules.
It has a dynamic program debugging feature.
It supports local and global variables.
New Gateway to Computer Science Book -9 209
Starting QBASIC
Turn on your PC and load an operating system.
Locate the QBASIC folder and double click on it.
Double click on QBASIC.EXE file to open the Qbasic editor screen.
Now, you can get the welcome screen as below.
Press Esc key to clear the dialogue box.
After this, you are ready to work with Qbasic programming.
210 New Gateway to Computer Science Book -9
Let’s Review
QBasic is a high level programming language developed by Microsoft Corporation.
BASIC is a programming language for beginners because of its in-built
commands, simple structure and flexibility.
QBASIC is also called a modular programming language where program is
divided in different modules or procedures.
QBAISC screen has two types of windows. They are Program Window and
Immediate Window.
Program window is the upper window which is titled as ‘Untitled’.
Immediate window is the lower window which is titled as ‘Immediate’.
QBASIC Interface
File Menu
New:This menu clears the current program.
Save: Save the current program in the storage location.
Open: It loads the program from the storage location.
Save as: Save the program file under new name.
Print: Print the selected text or entire program.
Exit:It helps to quit from QBASIC working program.
Edit Menu:The menu provides most of the operations for the Qbasic editor
such as opening a file, saving a file, and so many others. The details are given below.
Cut: It removes the selected text and stores it in the clipboard.
Copy: Copies the text instead of removing it.
Paste: Adds the text in the clipboard to the current position of the cursor.
Clear: Removes the text without storing it to the clipboard.
New Sub: Enables you to create a new subroutine.
New Function: Enables you to create a new function.
View Menu
SUBs: It shows the list of current subroutines and functions.
Split: Displays the contents of the current program in two windows. If the window
is already split, this hides the second window.
Output Screen: Shows the Qbasic output screen.
New Gateway to Computer Science Book -9 211
Search Menu
Find: It allows you to search for a string of text in the program.
Repeat Last Find: Continues the previous search operation.
Change: Replaces each instance of a string with another string.
Run Menu
Start: Executes the current program.
Restart: Starts from the beginning.
Continue: Continues execution at the current position.
Debug Menu
Step: Processes the next command.
Procedure Step: Processes the next command, but does not show QBasic going
inside a subroutine or function.
Trace On: Shows the command that is being executed while the program is
running.
Toggle Breakpoint: Sets or removes a breakpoint.
Clear All Breakpoints: Removes all breakpoints in the program.
Set Next Statement: Allows you to continue execution at the specified line
Elements of Qbasic Programming
QBASIC programming has some elements which are required to work with a program.
The element required to construct a QBASIC program consists of a set of characters,
keywords, constants, variables, operators and expressions. Let’s discuss them in details.
Character Set
The QBASIC Character Set consists of alphabets (both small and capital),
numbers (0 to 9) and special characters (; , = , + , / , ^ , (), % , $ , < , > , #). These
special characters have their own meaning and function. A set of characters
that are allowed to use in QBASIC is known as the QBASIC Character Set.
QBASIC keywords and variables are formed by using the characters defined in
the QBASIC Character Set.
212 New Gateway to Computer Science Book -9
Keyword
Keywords are those words which have special meanings in QBASIC. Keywords are
formed by using characters of QBASIC Characters Set. Keywords are statements,
commands, functions (built in functions) and names of operators. The keywords are also
called reserved words. Some reserved words are CLS, REM, INPUT, LET, PRINT, FOR,
DO, SELECT, MID$, ASC, SQR, LEN, LEFT$, TIME$, INT, etc.
Let’s Review
The element required to construct a QBASIC program consists of a set of
characters, keywords, constants, variables, operators and expressions.
The QBASIC Character Set consists of alphabets (both small and capital),
numbers (0 to 9) and special characters (; , = , + , / , ^ , (), % , $ , < , > , #).
Keywords are those words which have special meanings formed by using
characters of QBASIC characters set.
The keywords are also called reserved words.
Some reserved words are CLS, REM, INPUT, LET, PRINT, FOR, DO,
SELECT, MID$, ASC, SQR, LEN, LEFT$, TIME$, INT, etc.
Constants
Constants are defined as the values used in programs that do not change during the
execution of program. The constant data may be a letter, words, numbers, or special
characters. A constant can be stored in a variable when it is required to use in more
than one statement or expression. CONST statement is used to declare the constants
in QBASIC programming. For Eg: CONST pi=3.14. In QBASIC, these constants are
grouped into two main categories. They are sting constant and numeric constant.
String Constant
Sting Constant is a letter, words, numbers, combination of letters with numbers or special
characters enclosed in double quotes. Mathematical operations cannot be performed
on String Constants. “A”, “SHUBHARAMBHA”, “ROLL NO:10”, “ !Welcome to
Kathmandu !”, etc. are some examples of Sting Constants.
Numeric Constant
Numeric Constant refers to a number. A number with or without decimal point is a numeric
constant. Separators are not allowed to use in numeric constant. Numeric data should not
be enclosed in double quotes. Mathematical operations and logical operations can be
performed on the numeric constants. 110, 205.40, 420, 65503, etc. are some examples of
numeric constants. Numeric Constants may be integer, long integer, single precision or
double precision.
New Gateway to Computer Science Book -9 213
Integer: Integer is whole number between -32768 to 32767.
Long Integer: Long Integer is a large range of whole number.
Single Precision: Single Precision is seven digit or less than seven digit positive or
negative number that contain decimal point. Single Precision can be in the
exponential form using E or with a trailing exclamation point. (!). 612, 78.65, 1.2
E-06 and 12345.678! are some examples of single precision constants.
Double Precision: Double Precision is 17 digit or less than 17 digit positive or
negative numbers that contains decimal point. Double Precision can be in the
exponential form using D or with trailing hash sing (#). 9999.99D-12, 2345.786#
and 3456.7 are some examples of double precision constants.
Understanding literal constants and symbolic constants
A literal constant is just a value. For example, 50 is a literal constant. It does not have a
name; it is just a literal value. You cannot make the value 50 to represent any other value.
The value of 50 is always 50. You cannot assign a new value to 50.
Symbolic Constants assign a name to a constant value. The CONST keyword is followed
by a type, an identifier, the assignment operator (=), and the value to assign to the constant.
This is similar to declaring a variable except that you start with the keyword CONST and
symbolic constants must be initialized.
Let’s Review
Constants are defined as the values used in programs that do not change during the
execution of program.
CONST statement is used to declare the constants in QBASIC programming.
Sting constant is a letter, words, numbers, combination of letters with numbers or
special characters enclosed in double quotes.
A number with or without decimal point is a numeric constant. Separators are not
allowed to use in numeric constant.
Mathematical operations and logical operations can be performed on the numeric
constants.
A literal constant is just a value. It does not have a name; it is just a literal value.
Symbolic Constants assign a name to a constant value.
Variable
A variable is a quantity whose value changes during the execution of a program.
It is a quantity which can store value in computer memory. A program needs data to
214 New Gateway to Computer Science Book -9
produce information. A program can use data only when data are stored in the computer
memory (RAM). In a computer memory, there may be many data. So, you need to tell the
computer to use only those data which you want to use in a program. This is possible if
you assign a name to the place where you have stored data. Simply, we can say, a variable
is an entity that stores data needed to be used in a program. Each program defines different
number of variables. A value of a variable can be changed during the execution of the
program. In QBASIC, variables care grouped into two main categories. They are numeric
variable and string variable.
Numeric Variable
Numeric variable has a number as its value. Salary!, Age%, Mark, Number1, Number2,
FirstNum, RollNumber, etc., are examples of Numeric variables. A numeric variable can
be Integer, Long integer, Single precision or Double Precision variables.
An integer variable can store only an integer number. Its type declaration sign is
percentage (%).
A long integer variable can store a large range of whole number.
A single precision variable can store a whole number as well as number with
decimal. Its type declaration sign is exclamation sign (!).
A double precision variable also stores a whole number as well as number with
decimal point. Its type declaration sign is hash (#).
String Variable
A string variable stores sting data. Its type declaration sign is dollar ($).Naam$, Address$,
Bookname$, GameName$, etc., are the examples of sting varibales.
Declaration of Variable
Variables in QBASIC can be declared in two different ways. They are explained below.
Implicit Declaration: Implicit declaration is the default method of declaring
variable in QBASIC. In this declaration, type declaration character is used in an assignment
statement. Using this method, we can declare variable at the time of assigning value to
the variable. We should take care of type declaration symbol during the declaration and
use of variable.
Explicit Declaration: Explicit declaration of variable is done before the use
of variables. In this declaration, a value is not assigned to the variable. DIM
statement is used to declare variables explicitly in QBASIC. Eg: DIM a AS
INTEGER, DIM b AS STRING, etc. It’s not mandatory to use type declaration
symbol while declaring variables explicitly. Also, as all variables are declared at
the top, programmers are less prone to use same variable name for different purposes.
New Gateway to Computer Science Book -9 215
Rules for Naming a Variable
Variable names can have maximum of 40 characters.
Variable names can have alphabets, numbers and decimal points.
A variable name must begin with a letter.
Variable names cannot be reserved words.
Variable names may be ended with type declaration characters like $, %, &, !, and #.
Let’s Review
A variable is a quantity whose value changes during the execution of a program.
Numeric variable has a number as its value. Salary!, Age%, Mark, Number1,
Number2, FirstNum, RollNumber, etc., are the examples of Numeric Variables.
A numeric variable can be Integer, Long integer, Single precision or Double
precision variables.
A double precision variable also stores a whole number as well as number
with decimal point. Its type declaration sign is hash (#).
A string variable stores sting data. Its type declaration sign is dollar ($).
Implicit declaration is the default method of declaring variable in QBASIC.
Explicit declaration of variable is done before the use of variables. In this
declaration, a value is not assigned to the variable.
DIM statement is used to declare variables explicitly in QBASIC.
Variable names can have maximum 40 characters.
QBASIC Operators
Operators are the symbols that indicate the type of operation that has to be performed on
the data or on the values of variables.There are four types of operators in QBASIC. They
are Arithmetic Operators, Relational Operators, Logical Operators and Sting Operators.
Arithmetic Operators
Arithmetic Operators are used to perform mathematical calculations like addition,
subtraction, division, multiplication and exponential. The following table shows arithmetic
operators used in QBASIC.
216 New Gateway to Computer Science Book -9
Operation Operator Example Output
Addition + Sum = 3 + 4 Sum = 7
Subtraction - Diff = 10-5 Diff = 5
Multiplication * Product = 10*10 Product = 100
Floating Point Division / Div = 5/2 Div = 2.5
Integer Division \ Div = 11\5 Div = 2
Exponential ^ Exp = 2 ^ 5 Exp = 32
Modulus Division Mod Mod = 5 MOD 2 Mod = 1
Relational Operators
Relational operators are used to perform comparisons on two values of same type. A
comparison of sting data with numeric data cannot be done. The comparison of sting data
is done on the basis of ASCII value. The result of comparison is either true or false. The
following table shows the relational operators used in QBASIC
Name of operator Relation Example
= equal to 5 = 5 , A = B , A$=B$
> greater than 10>5, A>B
< less than 5<10, B<A
>= greater than or equal to 10>=5, A>=B
<= less than or equal to 5<=10, B<=A
<> not equal to 6<>5 , A<>D
Logical Operators
Logical Operators combine two or more relational expressions to evaluate a single value
as True or False. The result of evaluation is used to make decisions about the program
flow. The commonly used logical operators in QBASIC are AND, OR and NOT.
AND Operator
AND operator returns ‘True’ when all the results returned from individual relational
expressions are ‘True’ otherwise it returns ‘False’.The Truth Table of AND operator is
given below.
Input A Input B Output A AND B
False False False
False True False
True False False
True True True
New Gateway to Computer Science Book -9 217
OR Operator
OR Operator return ‘True’ if any one of the relational expressions returns ‘True’. If all
the relational expressions returns ‘False’ then only the combined result returned by OR
operator will be ‘False’.The Truth table of OR operator is as given below.
Input A Input B Output A OR B
False False False
False True True
True False True
True True True
NOT Operator
NOT Operator operates on one operand and returns ‘True’ if the logical operation returns
‘False’. The NOT truth table is as given below
Input A Output NOT A
False True
True False
Let’s Review
Operators are symbols that indicate the type of operation that has to performed
on the data or on the values of variables.
There are four types of operators in QBASIC. They are arithmetic operators,
relational operators, logical operators and sting operators..
Arithmetic operators are used to perform mathematical calculations like addition,
subtraction, division, multiplication and exponential.
Relational operators are used to perform comparisons on two values of same type.
Logical operators combine two or more relational expressions to evaluate a single
value as True or False.
AND operator returns ‘True’ when all the results returned from individual
relational expressions are ‘True’ otherwise it returns ‘False’.
OR operator returns ‘True’ if any one of the relational expressions returns ‘True’.
NOT operator operates on one operand and returns ‘True’ if the logical operation
returns ‘False’.
String Operators (+)
A string expression consists of string constant, string variables and other string
expressions combined by string operators. There are two classes of string operations.
218 New Gateway to Computer Science Book -9
They are concatenation and string function. The act of combining two strings is called
concatenation. The plus (+) symbol is the concatenation operator for string. For example,
the following program fragment combines the string variables A$ and B$ to produce the
value ShubharambhaPublication.
A$ = “Shubharambha” : B$ = “Publication”
Print A$+B$
Print “Our” + A$+B$
The output is :
Form Print A$+B$: ShubharambhaPublication
Print “Our” + A$+B$: OurShubharambhaPublication
Assignment Operators (=)
The assignment operator evaluates an expression on the right of the expression and
substitutes it to the value or variable on the left of the expression. For Example : x = a +
b, Here the value of a+b is evaluated and substituted to the variable x.
Expressions
An expression is the combination of operators, constants and variables that is evaluated
to get a result. The result of the expression is string data, numeric data or logical value and
can be stored in a variable. Algebraic expression cannot be used directly in programming.
It must be converted into QBASIC expression. Let’s discuss some algebraic and QBASIC
expressions in table below.
Algebraic Expression Qbasic Expression
A^2 + 2*a*b+b^2
a2 + 2ab + b2
(x^2 + y^2)/z^2
x2 + y2
z2
2x + 3y – 4c 2*x + 3*y – 4*c
2�(r+h) 2*3.141*r*(r+h)
ax2 + bx + c a*x^2 +b*x +c
Arithmetic Expression
An arithmetic expression is a syntactically correct combination of numbers, operators,
parenthesis, and variables. Arithmetic expressions are the parts of statements, so they
must follow syntax rules in order to be correct.
New Gateway to Computer Science Book -9 219
Logical (Boolean Expression)
A Boolean expression is a logical statement that is either TRUE or FALSE. Boolean
expressions can compare data of any type as long as both parts of the expression have the
same basic data type. You can test data to see if it is equal to, greater than, or less than
other data. A Boolean expression can consist of Boolean data, such as the following:
BOOLEAN values (YES and NO, and their synonyms, ON and OFF, and TRUE and
FALSE).
BOOLEAN variables or formulas.
Functions that return BOOLEAN results.
BOOLEAN values calculated by comparison operators.
String Expression
Any text to be written to a file or displayed on the screen is created with a string expression.
String expressions are created from a combination of the following:
Character strings
Concatenation operators "$"
Numeric Expressions
CHAR( )
A string expression must begin with a concatenation operator ($) or with a string constant
in quotes (i.e., "..."). The concatenation operator combines components together, and
must be between all components of a string expression.
Let’s Review
A string expression consists of string constant, string variables and other string
expressions combined by string operators.
The assignment operator evaluates an expression on the right of the expression
and substitutes it to the value or variable on the left of the expression.
An expression is the combination of operators, constants and variables that is
evaluated to get a result.
A Boolean expression is a logical statement that is either TRUE or FALSE.
Boolean expressions can compare data of any type as long as both parts of the
expression have the same basic data type.
A string expression must begin with a concatenation operator ($) or with a string
constant in quotes (i.e., "...").
The concatenation operator combines components together, and must be between
all components of a string expression.
220 New Gateway to Computer Science Book -9
Assignment
1. Fill in the blanks with suitable words.
a. QBasic is a ………….programming language developed by Microsoft
Corporation.
b. QBASIC is also called a …………. language where program is divided in
different modules or procedures.
c. Immediate window is the lower window which is titled as ‘……………..’.
d. The QBASIC ………. consists of alphabets (both small and capital), numbers (0
to 9) and special characters (; , = , + , / , ^ , (), % , $ , < , > , #).
e. …………….. are those words which have special meanings that are formed by
using characters of QBASIC Characters Set.
f. A ……………. is a quantity whose value changes during the execution of a
program.
g. A numeric variable can be Integer, Long Integer, Single Precision or ……………
variables.
h. ……………. is the default method of declaring variable in QBASIC.
i. ……………. can have maximum 40 characters.
j. ……………. are used to perform mathematical calculations like addition,
subtraction, division, multiplication and exponential.
k. An ……………… is the combination of operators, constants and variables that is
evaluated to get a result.
l. A ……………….. is a logical statement that is either TRUE or FALSE.
2. State whether the given statements are true or false.
a. QBAISC screen has two types of windows. They are Program Window and System
Window.
b. The keywords are also called library words.
c. Constants are defined as the values used in programs that change during the
execution of program.
d. A number with or without decimal point is a numeric constant.
e. Mathematical operations and logical operations can be performed on the numeric
constants.
f. Symbolic constants assign a name to a string constant.
g. A single precision variable also stores a whole number as well as number with
decimal point. Its type declaration sign is hash (#).
New Gateway to Computer Science Book -9 221
h. Explicit declaration of variable is done before the use of variables.
i. DIM statement is used to declare variables explicitly in QBASIC.
j. There are two types of operators in QBASIC. They are arithmetic operators and
relational operators,
k. Relational operators are used to perform comparisons on two values of same type.
l. Logical operators combine two or more relational expressions to evaluate a single
value as True or False.
m. AND operator return ‘True’ if any one of the relational expressions returns ‘True’.
n. An arithmetic expression is a syntactically correct combination of numbers,
operators, parenthesis, and variables.
o. The concatenation operator combines components together, and must be between
all components of a string expression.
3. Answer the following questions.
a. What is QBASIC programming? Write its features.
b. What are the various options with edit menu of QBASIC screen? List out it in
details.
c. What are the elements of QBASIC programming? Define each in short.
d. What is a variable? Define its types.
e. What are ways of declaring variables? Write in short.
f. Write down the rules for naming a variable.
g. What are the operators? Write its types in detail.
h. Prepare a truth table for AND, OR and NOT operator.
i. What is a string operator? Write with examples.
j. What is an expression? Write about an arithmetic and a Boolean expression.
4. Convert the following algebraic expression into QBASIC expression.
a. a2 + 2ab + b2
b. x2 + y2
z2
c. 2x + 3y – 4c
d. 2�(r+h)
e. ax2 + bx + c
222 New Gateway to Computer Science Book -9
Lesson
13 QBASIC Statements
Inside the Lesson
Introduction to Qbasic statement
Explanation of Qbasic statements
REM, CLS, LET, INPUT, PRINT, END
PRINT USING, LINE INPUT, LPRINT
LPRINT USING, READ….DATA
RESTORE, LOCATE, SWAP
A statement is a computer instruction written in a source language, which is converted
into one or more machine code instructions by a compiler. The command or the set of
instructions in the QBASIC is called a statement. Statements are first stored in the memory
and executed only when the command is given. Qbasic statements are classified into
two statements. They are executable and non-executable. Executable statement specifies
actions where as non-executable statements specify characteristics, arrangements and
initial values of data.
The commonly used QBASIC statements are: LET, PRINT, INPUT, CLS, END, REM,
etc. Let’s discuss various Qbasic statements.
QBASIC Statements
REM statement: REM is a non-excutable statement that is used to insert explanatory
remarks for the program that we are writing. It can be used at any part of the program. The
general format of REM statement is as given below.
REM <remark> Here, remark may be any statement or phrase to define the program.
CLS Statement: This statement is used to clear the previous output from the display
screen and makes the screen blank. This command is given before the starting of any new
program. The general format of CLS statement is as given below.
<CLS>
New Gateway to Computer Science Book -9 223
LET Statement: This statement is used to assign the value in the variable. It is also called
an assignment statement. This is also an optional statement is Qbasic. The general format
of CLS statement is as given below.
LET <variable> = <expression> Here, variable is the name of a numeric or string variable
which receives the value and expression in a numeric or string expression whose value
will be assigned to a variable.
INPUT Statement: This statement is used to receive input data from the keyboard during
the execution of the program. It will prompt the user to input data and then place the
input data into a variable. INPUT string and the variable must be separated by either a
comma or a semicolon. The semicolon will print a question mark at the end of the text.
The general format of INPUT statement is as given below.
INPUT [“Message”] ; list of Variables Here, message is a string constant enclosed in
double quotes and list of variables contain the numeric or string variable which stores the
data input by the user.
PRINT Statement: This statement is used to display the output on the screen. Question
mark (?) also can be used instead of PRINT statement to display on the screen. The
general format of PRINT statement is as given below.
PRINT[list of expressions][{; | ,}]
END Statement:This statement is used to indicate the end of the QBASIC program to
terminate it.The general format of END statement is as given below.
<END>Practical Assignment
a. REM 'To add the number 30 and 40
CLS
LET a = 30
LET b = 40
sum = a + b
PRINT "The Sum is"; sum
END
b. REM 'To find the sum of any two numbers
CLS
INPUT "Enter the first number"; a
INPUT "Enter the second number"; b
sum = a + b
PRINT "The sum is"; sum
END
224 New Gateway to Computer Science Book -9
PRINT USING Statement: This statement is used to display string or number using a
specified format. The general format of PRINT USING statement is as given below.
PRINT USING string expressions;list of expressions[;] Here, string expressions
is a string literal or variable consisting of special formatting characters. The formatting
characters determine the field and the format of printed strings or numbers and list of
expressions consists of the string or numeric expressions separated by semicolons.
The string formatting characters are given in the table below.
! displays only the first character of string
\n\ prints first n+2 characters from the string
& displays all the string
# digit position is specified
&& prints leading & sign
^^^^ prints the number in exponential format
. decimal point position specification
Let’s Review
A statement is a computer instruction written in a source language, which is
converted into one or more machine code instructions by a compiler.
The command or the set of instructions in the QBASIC is called a statement.
Qbasic statements are classified into two statements.They are executable
and non-executable.
Executable statement specifies actions whereas non-executable statements
specify characteristics, arrangements and initial values of data.
REM statement is used to insert explanatory remarks for the program that
we are writing.
REM is a non-executable statement where single quotation (‘) is used.
CLS statement is used to clear the previous output from the display screen and
makes the screen blank.
LET statement is used to assign the value in the variable. It is also called as
an assignment statement.
INPUT statement is used to receive input data from the keyboard during the
execution of the program.
PRINT statement is used to display the output on the screen.
END statement is used to indicate the end of the QBASIC program to terminate it.
New Gateway to Computer Science Book -9 225
Practical Assignment
a. CLS
x$ = "Dinesh"
y$ = "Prasad"
z$ = "Adhikari"
PRINT USING "!"; x$; ".";
PRINT USING “!”; y$; ".";
PRINT USING "&";z$;
END
LINE INPUT Statement : This statement allows to input line of data at a time and assign
in to single variable.The general format of LINE INPUT statement is as given below.
LINE INPUT “prompt”; variable Here, prompt is a string constant that will be displayed
when the statement is executed and variable in the name of a string variable or array
element to which the line will be assigned.
Practical Assignment
a. CLS b. CLS
LINE INPUT "Enter the data"; n$ LINE INPUT "Enter a sentence"; n$
PRINT n$ PRINT n$
END END
LPRINT and LPRINT USING Statement: The LPRINT statement is used to print the
text to a printer and the LPRINT USING statement is used to print formatted output to a
printer. The general format of LPRINT and LPRINT USING statement is as given below.
LPRINT [expression [, \; expression]……] [;]
LPRINT USING “string”: expression [, \;expression]…... [;] Here, expression is a numeric
or string to be printed and string is a string constant or variable that identifies the format
in which expression is to be printed.
READ….DATA Statement: This statement is used to read values from the DATA
statement and assign them to variables. It is used mostly when dealing with large quantities
of data in program.The DATA statement is to store the numeric and string constants that
are accessed by the READ statement. The general format of READ…..DATA statement
is as given below.
226 New Gateway to Computer Science Book -9
READ [Variable1, variable 2, variable 3…..]
------
------
------
DATA [constant1, constant2, constant 3….
Practical Assignment
a. CLS b. CLS
READ a, b, c READ A, B, C, D
PRINT a, b, c Total = A + B +C + D
DATA 100,200,300 Avg = Total / 4
END PRINT "The total value is"; Total
PRINT "The average value is"; Avg
DATA 80,50,40,70
END
RESTORE Statement: The RESTORE statement is used to reset the DATA
pointer to the beginning of the data. Once the data element is read, it cannot be read
again until the program resets it. The RESTORE statement is used to reset the data in the
program. The general format of RESTORE statement is as given below.
RESTORE[line number] Here, If line number is specified, the next READ statement
accesses the first item in the specified DATA statement and If line number is omitted, the
next READ statement accesses the first item in the first DATA statement.
Practical Assignment
a. CLS
READ a, b, c
PRINT a, b, c
RESTORE
READ d, e, f
PRINT d, e, f
DATA 10,20,30
END
New Gateway to Computer Science Book -9 227
LOCATE Statement: This statement is used to move the cursor to the specified position
on the screen.
LOCATE [row][,[col][,[cursor][,[start] [,stop]]]] Here, row is the screen line number, col
is the screen column number,cursor is a boolean value indicating whether the cursor is
visible or not. Start is the cursor start scan line, stop is the cursor stop scan line.
Practical Assignment
a. CLS
LOCATE 5, 30
PRINT "New Gateway to Computer"
END
SWAP Statement :This statement is used to exchange the values of two variables. The
general format of SWAP statement is as given below.
SWAP variable1, variable2
Practical Assignment
a. CLS
INPUT "Enter the first number"; a
INPUT "enter the second number"; b
PRINT "Before using SWAP”; a, b
SWAP a, b
PRINT "After SWAP"; a, b
END
Let’s Review
PRINT USING statement is used to display string or number using a specified
format.
LINE INPUT statement allows inputting line of data at a time and assigning
in to single variable.
READ….DATA statement is used to read values from the DATA statement
and assign them to variables.
The RESTORE statement is used to reset the DATA pointer to the beginning
of the data.
LOCATE statement is used to move the cursor to the specified position on the
screen.
SWAP statement is used to exchange the values of two variables.
228 New Gateway to Computer Science Book -9
Practical Section
Program 1: Write a program to input any three numbers and display its sum
and average.
REM 'to calculate sum and average of three numbers
CLS
INPUT "Enter first number"; A
INPUT "Enter second number"; B
INPUT "Entre third number"; C
SUM = A + B + C
AVG = (A + B + C) / 3
PRINT "The sum is"; SUM
PRINT "The average value is"; AVG
END
Program 2: Write a program to display the area of a rectangle.
REM PROGRAM TO DISPLAY AREA OF RECTANGLE
CLS
INPUT "ENTER LENGTH"; L
INPUT "ENTER BREADTH"; B
A=L*B
PRINT "AREA OF RECTANGLE "; A
END
Program 3: Write a program to display the area of a square.
REM PROGRAM TO DISPLAY AREA OF SQUARE
CLS
INPUT "ENTER LENGTH"; L
A=L^2
PRINT "AREA OF SQUARE"; A
END
Program 4: Write a program to display the perimeter of a rectangle.
REM PROGRAM TO DISPLAY PERIMETER OF RECTANGLE
CLS
New Gateway to Computer Science Book -9 229
INPUT "ENTER LENGTH"; L
INPUT "ENTER BREADTH"; B
P = 2 * (L + B)
PRINT "PERIMETER OF RECTANGLE"; P
END
Program 5: Write a program to display the area and perimeter of a rectangle.
REM 'TO DISPLAY AREA AND PERIMETER OF RECTANGLE
CLS
INPUT "ENTER LENGTH"; L
INPUT "ENTER BREADTH"; B
A= L * B
P = 2 * (L + B)
PRINT "AREA OF RECTANGLE" ; A
PRINT "PERIMETER OF RECTANGLE"; P
END
Program 6: Write a program to display the area and circumference of a circle.
CLS
LET PIE = 3.14
INPUT "Enter radius of circle"; R
A = PIE * R ^ 2
C = 2 * PIE * R
PRINT "The area of the circle is"; A
PRINT "The circumference of the circle is"; C
END
Program 7: Write a program to exchange the value of any two variables.
CLS
INPUT "Enter the first number"; a
INPUT "Enter the second number"; b
PRINT "Before using SWAP"; a, b
SWAP a, b
PRINT "After SWAP"; a, b
END
230 New Gateway to Computer Science Book -9
Assignment
1. Fill in the blanks with suitable words.
a. The command or a set of instructions in the QBASIC is called a …………...
b. ………… specifies actions whereas non-executable statements specify
characteristics, arrangements and initial values of data.
c. ……………. is a non-executable statement where single quotation (‘) is used.
d. …………. statement is used to assign the value in the variable.
e. …………..statement is also called an assignment statement.
f. …………….. statement is used to display the output on the screen.
g. …………….. statement is used to display string or number using a specified
format.
h. ……………. statement is used to read values from the DATA statement and assign
them to variables
i. The RESTORE statement is used to reset the ………….. pointer to the beginning
of the data.
2. State whether the given statements are true or false.
a. A statement is a computer instruction written in a source language, which is
converted into one or more machine code instructions by a compiler.
b. QBASIC statements are classified into two statements and they are executable
and non-executable.
c. CLS statement is used to insert explanatory remarks for the program that we are
writing.
d. REM statement is used to clear the previous output from the display screen and
makes the screen blank.
e. INPUT statement is used to receive input data from the keyboard during the
execution of the program.
f. END statement is used to indicate the end of the QBASIC program to terminate
it.
g. LINE INPUT statement allows inputting line of data at a time and assigning in to
single variable.
h. SWAP statement is used to move the cursor to the specified position on the screen.
i. LOCATE statement is used to exchange the values of two variables.
New Gateway to Computer Science Book -9 231
3. Answer the following questions.
a. What is a QBASIC statement? List out some QBASIC statements.
b. What is the use of CLS and LET statements?
c. Define locate statement with syntax.
d. Write down the function and syntax of the following QBASIC statements.
i. INPUT statement
ii. PRINT statement
iii. PRINT UNING statement
iv. LINE INPUT statement
v. READ…..DATA statement
4. Write a QBASIC program for the followings.
a. To input any three numbers and display its sum.
b. To display the area of a rectangle.
c. To display the area of a circle.
d. To display the area and perimeter of a rectangle.
e. To exchange the value of any two variables.
5. Debug the following programs.
a. CLS
REM To find Sum of two numbers b. CLS
INPUT "Enter the first number"; a REM To find Area of Rectangle
INPUT "Enter the second number"; b INPUT "Enter the length" ; l
SUM = a + c INPUT “Enter the breadth” ; b
PRIT "The sum of two number is"; S AR = l * b
END PRINT "The area of rectangle is AREA
c. CLS END
REM To find area of circle
LET r = 6
LET P = 3.14
AREA = PAI * N ^ 2
PRINT "The area of circle is" ARE
END
232 New Gateway to Computer Science Book -9
d. CLS 233
INPUT "Enter the name of student" ; n$
INPUT "Enter the marks of English"; e
INPUT "Enter the marks of Nepali" ; n
INPUT "Enter the marks of Computer"; c
TO = e + n - c
PER = e + n + c / 300 * 100
PRINT "Student’s name" ; n
PRINT "Total marks of student" ; TOTAL
PRINT " Percentage of student"; PR
END
6. Write down the output of the following programs.
a. CLS
x = 5
y = 3
LET c = (x2+3x-y)
PRINT " The value of c is " ; c
END
b. CLS
READ A, B, C, D
Total = A + B +C + D
Avg = Total / 4
PRINT "The total value is"; Total
PRINT "The average value is"; Avg
DATA 90,110,50,130
END
c. CLS
x$ = "Ram"
y$ = "Krishna"
z$ = "Malakar"
PRINT USING "!"; x$; ".";
PRINT USING “!”; y$; ".";
PRINT USING "&";z$;
END
New Gateway to Computer Science Book -9
7. Study the following programs and answer the given questions.
a. CLS
INPUT "Enter the first number"; a
INPUT "enter the second number"; b
PRINT "Before using SWAP”; a, b
SWAP a, b
PRINT "After SWAP"; a, b
END
i. If the user input the first number as 50 and second number as 70, what will be
the output?
ii. What are the variables use in the above program?
iii. What is the statement use in above program to exchange the value?
b. CLS
INPUT "ENTER LENGTH"; L
INPUT "ENTER BREADTH"; B
P = 2 * (L + B)
PRINT "PERIMETER OF RECTANGLE"; P
END
i. If the user input the length 150 and breadth 60, what will be the output?
ii. What are the operators use in the above program?
iii. List out the variables use in the above program.
234 New Gateway to Computer Science Book -9
Lesson
14 Program Flow and Control Structures
Inside the Lesson
Introduction to program flow and control structure
Types of control statements
Branching statement
Conditional branching statement
Unconditional branching statement
Looping statement
Practice section
Statements that specify the order in which actions are executed are called control
statements. A program consists of a number of statements which are usually executed
in sequence. Control statements allow you to change the computer's control from
automatically reading the next line of code to reading a different one. The control
statements are used for controlling the execution of the program. In a program, a control
structure determines the order in which statements are executed. Mainly, there are two
types of control statements in QBASIC. They are Branching Statement and Looping
Statement.
Types of Control Statements
Branching statement
Branching statement is a decision making statement. Depending upon the decision,
branching statement can change the order of execution. Branching statements are divided
into two types.
Conditional Branching Statement
This statement allows the selective execution of statements based on a particular condition.
This statement directs the computer to another part of the program based on the results
New Gateway to Computer Science Book -9 235
of a compare. High-level language statements, such as IF….. THEN, IF…..THEN…..
ELSE, IF ... THEN ... ELSE IF, SELECT ... CASE, are the popular conditional branching
statements. We are going to discuss more about it.
IF ... THEN Statement
This statement is a decision making conditional branching statement which
executes a block of statements only if the given expression or condition is true. If
the condition is false, then the IF blocks will be skipped and execution continues
with the rest of the program.
Syntax :IF [conditional expression] THEN [statement block]
Practical Assignment
1. CLS
INPUT "Enter your marks in computer : "; C
IF C>= 40 THEN PRINT "You are passed”
IF C<40 THEN PRINT "You are failed"
END
2. CLS
INPUT "Enter a number "; N
IF N MOD 2 = 0 THEN PRINT "It is Even Number"
IF N MOD 2 = 1 THEN PRINT "It is Odd Number"
END
3. CLS
INPUT "Enter first number "; A
INPUT "Enter second number "; B
INPUT "Enter third number "; C
IF A>B AND A>C THEN G=A
IF B>A AND B>C THEN G = B
IF C>A AND C>B THEN G= C
PRINT “The greatest number is:”; G
END
236 New Gateway to Computer Science Book -9
IF ... THEN .... ELSE Statement
It is also a control statement which is used to execute the multiple statements depending
on the condition. It is also called two way decision making statement. In this statement if
the condition is true, the statements after THEN will be executed and if the condition is
false, the statements in the ELSE block will be executed.
Syntax :IF [conditional] THEN[statement 1]ELSE[statement 2]
……………….
END IF
Practical Assignment
1. CLS
INPUT "Enter your marks in computer"; C
IF C >= 40 THEN
PRINT "You are passed."
ELSE
PRINT "You are failed ."
END IF
END
2. CLS
INPUT "Enter first number "; A
INPUT "Enter second number "; B
INPUT "Enter third number "; C
IF A> AND A>C THEN
PRINT "The greatest number is"; A
IF B>A AND B>C THEN
PRINT "The greatest number is"; B
ELSE
PRINT "The greatest number is:"; C
END
3. CLS
INPUT "Enter a number "; N
New Gateway to Computer Science Book -9 237
IF N MOD 2 = 0 THEN
PRINT "It is an even Number"
ELSE
PRINT "It is an odd Number"
END IF
END
IF ... THEN ... ELSEIF Statement
This statement is another form of IF….THEN…..ELSE statement. The conditions are
evaluated from top to buttom. The syntax is given below.
Syntax :
IF [condition] THEN
[statement 1]
ELSEIF
[statement 2]
ELSEIF
[statement n]
........................
.......................
ELSE
[statement n]
END IF
Practical Assignment
1. CLS
INPUT "Enter First Number "; A
INPUT "Enter Second Number "; B
INPUT "Enter Third Number "; C
IF A>B AND A>C THEN
PRINT "The greatest number is "; A
ELSEIF B> A AND B>C THEN
PRINT "The greatest number is "; B
ELSE
PRINT "The greatest number is "; C
END IF
END
238 New Gateway to Computer Science Book -9
2. CLS
INPUT "Enter your percentage "; P
IF P>= 80 THEN
Result$ = "Distinction"
ELSEIF p >= 60 THEN
Result$ = "First Division"
ELSEIF p >= 40 THEN
Result$ = "Second Division"
ELSEIF p >= 35 THEN
Result$ = "Third Division"
ELSE
Result$ = "Sorry! No result found"
END IF
PRINT "Division ::: "; Result$
END
SELECT CASE Statement
This statement is a multi-way branching statement which makes the control jump to one
of the several statements based on the value of an expression.
Syntax : 1. CLS
SELECT CASE INPUT "Enter your percentage "; P
CASE test 1 SELECT CASE P
[statement1] CASE 80 TO 100
CASE test 2 div$ = "Distinction"
[statement 2] CASE 60 TO 80
........................ div$ = "First Division"
....................... CASE 40 TO 60
CASE ELSE div$ = "Second Division"
[statements] CASE 35 TO 40
END SELECT div$ = "Third Division"
CASE ELSE
div$ = "Fail"
END SELECT
PRINT "Division :: "; div$t
New Gateway to Computer Science Book -9 239
Practical Assignment
1. CLS
INPUT "Enter your choice (1 – 3)"; N
SELECT CASE N
CASE 1
PRINT "Baishak"
CASE 2
PRINT "Jestha"
CASE 3
PRINT "Ashad"
CASE ELSE
PRINT "Invalid Value"
END SELECT
END
2. CLS
INPUT "Enter First Number "; A
INPUT "Enter Second Number "; B
PRINT "****** MENU *******"
PRINT "1. Addition"
PRINT "2. Subtraction"
PRINT "3. Multiplication"
PRINT "4. Division"
INPUT "Enter your choice! (1/2/3/4) "; CH
SELECT CASE CH
CASE 1
PRINT "Sum = "; A + B
CASE 2
PRINT "Difference = "; A - B
CASE 3
PRINT "Product = "; A * B
CASE 4
PRINT "Quotient = "; A / B
CASE ELSE
PRINT "InvalidData!”
END SELECT
240 New Gateway to Computer Science Book -9
Let’s Review
A program consists of a number of statements which are usually executed in
sequence.
Control statements allow you to change the computer's control from automatically
reading the next line of code to reading a different one.
Branching statement is a decision making statement.
Conditional branching statement allows the selective execution of statement
based on a particular condition.
IF... THEN statement is a decision making conditional branching statement
which executes a block of statements only if the given expression or condition is
true.
IF...THEN...ELSE statement is also called two way decision making statements.
The IF….THEN…..ELSE statement is another form of IF….THEN…..ELSE
statement in which the conditions are evaluated from top to buttom.
SELECT CASE statement is a multi-way branching statement which makes the
control jump to one of the several statements based on the value of an expression.
Unconditional Branching Statement
Unconditional branching statement is used when the programmer forces the execution of
a program to jump to another part of the program. This can be done using the combination
of loops and if statements. You can avoid such unconditional branching and use this
technique only when it is very difficult to use a loop.
GOTO Statement
This statement in QBASIC is used to transfer the program control from one statement to
another. It is an unconditional branching statement.
Syntax : GOTO [line number / line label]
Practical Assignment 2. CLS
n=1
1. CLS top:
AA: PRINT n;
INPUT "Enter the first Number"; A n=n+1
INPUT "Enter the second Number"; B IF n <= 20 THEN GOTO top
Sum = A + B END
PRINT "The sum is"; Sum
GOTO AA
END
New Gateway to Computer Science Book -9 241
Looping Statement
Looping statement is the statement that executes one or more statements repeatedly in a
several number of times. In QBASIC there are three types of loops they are for, while and
do-while.When you need to execute a block of code several number of times then you
need to use looping. Let’s discuss the major looping statements in QBASIC.
Statement before FOR
FOR
COUNTER_VARIABLE = STARTVALUE
COUNTER_VARIABLE FALSE
<END_VALUE
COUNTER_VARIABLE = Statement of Loop
COUNTER_VARIABLE + NEXT
STEPVALUE
Statement after NEXT
FOR……NEXT Statement
It is the fundamental looping statement in QBASIC. It provides a way to repeat a block
of statements over a predetermined number of times. The FOR…..NEXT loop is used
when we are required to perform repetitive tasks, such as to display addition of all values
between 100 to 200, display all prime numbers between 1 to 100, etc.
Syntax
For counter variable = <initial value>To <Ending Value> [step increment / decrement]
[statements]
[Exit For]
[statements]
Next
Let’s observe the given table to understand the parts of the syntax.
242 New Gateway to Computer Science Book -9
Part Description
counter
Numeric variable used as a loop counter. The variable can't be an array
initial element or an element of a user-defined type.
ending
step Starting value of counter.
statements Final value of counter.
Amount counter is changed each time through the loop. If not specified,
step defaults to one.
One or more statements between FOR and NEXT that are executed at
the specified number of times.
Practical Assignment
1. CLS 2. CLS
FOR I = 1 to 10 FOR I = 1 to 10 STEP 2
PRINT I; PRINT I;
NEXT NEXT
END END
3. CLS 4. CLS
FOR I = 10 to 1 STEP -2 FOR I = 1 to 10
PRINT I; PRINT "Computer";
NEXT NEXT
END END
5. CLS
FOR I = 1 to 3
FOR J = 1 to 3
PRINT "Computer"
NEXT
NEXT
END
New Gateway to Computer Science Book -9 243
WHILE……WEND Statement Statement before WHILE
For
The WHILE…WEND loop is the fundamental
loop statement in QBASIC. It provides a way CHECK CONDITION
to repeat a block of statements, as long as the
condition is true. It is used to perform a repetitive
task based on a condition. The condition usually
results from a comparison of two values, but it
can be any expression that evaluates to a Boolean
value (TRUE or FALSE).
Syntax TRUE
WHILE (TEST CONDITION) Statement of Loop
(Statements OF Loop)
WEND WEND
Statement after WEND
Let’s observe the given table to understand the part of the syntax.
Part Description
TEST CONDITION It is a numeric or string expression that evaluates whether
the statement is TRUE or FALSE.
Statements OF Loop It is a set of one or more QBASIC statements.
WHILE Statement It always checks the condition before it begins the loop.
LOOP It continues till the condition remains TRUE.
Practical Assignment 2. REM to print series from 1 to 20
CLS
1. REM to print series from 1 to 10 C=1
CLS WHILE C <= 20
C=1 PRINT C
WHILE C <= 10 C=c+1
PRINT C WEND
C=c+1
WEND END
END
244 New Gateway to Computer Science Book -9
DO….WHILE LOOP Statement before DO
WHILE
A DO….WHILE loop is used when we want
to repeat a set of statements as the long as DO WHILE
condition is true.The condition usually results
from a comparison of two values, but it can CHECK CONDITION FALSE
be any expression that evaluates to a Boolean
value (TRUE or FALSE). The condition may be TRUE
checked at the beginning of the loop or at the end Statement of Loop
of the loop.
LOOP
Syntax
Statement After LOOP
DO WHILE (TEST CONDITION)
(Statements Of Loop)
LOOP
Let’s observe the given table to understand
the part of the syntax.
Part Description
TEST CONDITION
It is numeric or string expression that evaluates whether the con-
Statement of Loop dition is TRUE or FALSE.
LOOP • It is a set of one or more QBASIC statements.
• If the condition is TRUE, all statements in statement in loop
are executed until the loop statement is encountered.
• Control then returns to the WHILE statement and Condition is
again checked.
• If the condition is still TRUE, then the process is repeated.
If the Condition is FALSE, then the loop statements are skipped
and the statement after LOOP statement is executed.
Practical Assignment
1. REM to print the series from 1 to 10 2 REM to print the series from 1 to 20
CLS CLS
c=1 C=1
DO WHILE c <= 10 DO WHILE C <= 20
PRINT c PRINT C
c=c+1 C=C+1
LOOP LOOP
END END
New Gateway to Computer Science Book -9 245
DO….UNTIL LOOP Statement before DO
UNTIL
A DO….UNTIL loop is used when you want to
repeat a set of statements as long as the condition DO UNTIL
is false. The condition may be checked at the
beginning of the loop or at the end of the loop. CHECK CONDITION FALSE
Syntax
DO UNTIL (TEST CONDITION) TRUE
(Statements of Loops) Statement of Loop
REPORT THIS AD
LOOP
LOOP
Statement After LOOP
Let’s observe the given table to understand the part of the syntax.
Part Description
TEST CONDITION
It is a numeric or string expression that evaluates
Statement of Loop whether the statement is TRUE or FALSE.
It is a set of one or more QBASIC Statements.
Practical Assignment
1. REM a program to print the series from 1 to 10.
CLS
C=1
DO UNTIL C = 11 (this number will not be displayed)
PRINT C
C=C+1
LOOP
END
2. REM a program to print the series from 1 to 20.
CLS
C=1
246 New Gateway to Computer Science Book -9
DO UNTIL C = 21(this number will not be displayed)
PRINT C
C=C+1
LOOP
END
EXIT Statement: Statement before FALSE
DO UNTIL
The EXIT Statement is used to terminate DO UNTIL
the loop based on certain criteria before
the expected number of iterations. CHECK CONDITION
When the EXIT Statement is executed,
the control jumps to next statement
immediately after the loop. We can use
the EXIT Statement in all types of loops.
Syntax: EXIT DO / FOR
TRUE EXIT
Statement of Loop DO
LOOP 247
Statement After LOOP
Practical Assignment
1. CLS
X=1
DO
prod = x* 5
IF prod >50 THEN EXIT DO
PRINT prod
X=X
LOOP WHILE X<=10
END
New Gateway to Computer Science Book -9
Let’s Review
Unconditional branching is when the programmer forces the execution of a
program to jump to another part of the program.
The GOTO statement in QBASIC is used to transfer the program control from
one statement to another.
Looping statement is the statement that executes one or more statements
repeatedly in a several number of times.
The FOR…..NEXT loop is used when we are required to perform repetitive
tasks.
A DO….WHILE loop is used when we want to repeat a set of statements as
long as the condition is true.
A DO….UNTIL loop is used when you want to repeat a set of statements as long
as the condition is false.
The EXIT Statement is used to terminate the loop based on certain criteria before
the expected number of iterations.
Practical Section
Program 1: Write a Qbasic program to find out the simple Interest and the
Amount.
CLS
Input "Enter the Principal";P
Input "Enter the Rate";R
Input "Enter the Time";T
Let I = P*T*R/100
Let A= P + I
Print "The simple Interest = ";I
Print "The amount=";A
End
Program 2: Write a Qbasic program to find out the area of four walls of a
room.
CLS
Input "Enter the height";H
248 New Gateway to Computer Science Book -9
Input "Enter the length"; L
Input "Enter the Breadth";B
Let A= 2 * H * (L+B)
Print "The area of four walls =";A
End
Program 3: Write a Qbasic program to enter any three numbers and calculate
their sum and average.
CLS
Input "Enter any number" ;A
Input "Enter any number" ;B
Input "Enter any number" ;C
Let Sum = A+B+C
Let Average =Sum/3
Print "The sum=" ;Sum
Print "The Average is" ;Average
End
Program 4: Write a Qbasic program to input student's name,marks obtained
in five different subjects and find the total and average marks.
CLS
Input "Enter the name" ;N$
Input "Enter the marks in English" ;E
Input "Enter the marks in Computer" ;C
Input "Enter the marks in Maths" ;M
Input "Enter the marks in Science" ;S
Input "Enter the marks in Nepali" ;N
Let S=E+C+M+S+N
Let A=S/5
Print "The name of the student is" ;N$
Print "The total marks is" ;S
Print "The Average marks" ;A
End
New Gateway to Computer Science Book -9 249