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

JBD Computer Studies Book 9

JBD Computer Studies Book 9

Keywords: JBD Publication, Kathmandu, Latest edition, Nepal, JBD Computer Studies Book 9, aayushbhatta62

4. Answer the following questions.
a. Define the following terms:
i. Web Site
ii. Web Server
iii. Web Browser
iv. Uniform Resource Locator (URL)
b. What is HTML and what are its features?
c. What are HTML tags and where are they used?
d. What is the difference between container tag and empty tag?
e. What are the three basic HTML document tags?
f. Write the function and syntax of the following HTML elements:
i. Line Break element
ii. Horizontal Rule element
iii. Anchor element
iv. Paragraph element
v. Image element
g. What is hyperlink?

In the Lab

Design a web page according to the following specifications:
a. Set the background colour of the web page as green.
b. Right align the paragraph.
c. Insert any suitable picture with a border.
d. Left align the image with respect to the surrounding text.
e. Centre the heading “Story for Kids”.
f. Set the font for the heading as Arial and font size as 3.

Hansel and Gretel
Hard by a great forest dwelt a poor wood-cutter with his wife and his two children.
The boy was called Hansel and the girl Gretel. He had little to bite and to break,
and once, when great dearth fell on the land, he could no longer procure even daily
bread. Now when he thought over this by night in his bed, and tossed about in his
anxiety. He groaned and said to his wife, “What is to become of us? How are we
to feed our poor children, when we no longer have anything even for ourselves?”

JBD
Computer Studies-9 151

Design a web page according to the following specifications:
a. An image related to the story.
b. A marquee
c. An ordered list
d. A hyperlink to learn more about the story
e. The text colour should be blue
f. The background colour should be yellow

Grow Your Own Gargoyle
This story begins with Once Upon A Time, because the best stories do, of course. So,
Once Upon A Time, and imagine if you can, a steep sided valley cluttered with giant,
spiky green pine trees and thick, green grass that reaches to the top of your socks so
that when you run, you have to bring your knees up high, like running through water.
Wildflowers spread their sweet heady perfume along the gentle breezes and bees hum
musically to themselves as they cheerily collect flower pollen. People are very happy here
and they work hard, keeping their houses spick and span and their children’s faces clean.
This particular summer had been very hot and dry, making the lean farm dogs sleepy and
still. Farmers whistled lazily to themselves and would stand and stare into the distance,
trying to remember what it was that they were supposed to be doing. By two o’clock in
the afternoon, the town would be in a haze of slumber, with grandmas nodding off over
their knitting and farmers snoozing in the haystacks. It was very, very hot. No matter how
hot the day, however, the children would always play in the gentle, rolling meadows.

Design a web page according to the following specifications:
a. The background colour of the page should be yellow.
b. The heading “Rapunzel”, should scroll from right of the page to the left and should

be coloured magenta.
c. The line separator should be blue.
d. The text colour should be red.

Rapunzel
There were once a man and a woman who had long, in vain, wished for a child. At length
it appeared that God was about to grant their desire. These people had a little window at
the back of their house from which a splendid garden could be seen, which was full of the
most beautiful flowers and herbs. It was, however, surrounded by a high wall, and no one
dared to go into it because it belonged to an enchantress, who had great power and was
dreaded by all the world.

JBD
152 Computer Studies-9

Chapter 12

PROGRAMS AND
PROGRAMMING

LANGUAGES

Objectives

After completing this chapter, you will be able to:
y Explain the roles of program in a computer.
y Explain the characteristics of a good program.
y Describe the different stages of program development cycle.
y Define programming language and explain the different types of programming

languages.

C Ooncept verview

Computer has emerged as the most useful device in recent times. It is a
sophisticated machinery tool that works according to the instructions provided
to it. A set of instructions that are grouped together to accomplish a task or tasks
is called program and the development of program is known as programming.
Computer programming involves much more than simply writing a list of
instructions. It is the process of writing, testing, debugging/troubleshooting and
maintaining the source code of computer programs. This source code is written
in a programming language by a computer programmer. The primary role of
a computer programmer is to write programs according to the instructions
determined primarily by computer software engineers and systems analysts.
Computer programmers are the ones that take the complete designs and convert
them into the instructions that the computer can actually follow. Without
programmers, the users of computers would have no programs, and without
programs, computers would have nothing to do.

JBD
Computer Studies-9 153

Characteristics of a Program
A program should be designed not only with the goal of providing solution to
a problem. A program should also be good in other aspects such as reliability,
efficiency, user-friendliness, accuracy, etc. Let us learn about the characteristics of
a good program.

Accuracy
Accuracy is one of the key features of computers on the basis of which they are
distinguished from other devices. Accuracy is often termed as integrity. A good
program should be accurate i.e. it should always produce the results that are
correct and accurate.

User-friendliness
User friendliness of a program refers to the ease with which a program can be
operated. It should minimize the number of tasks required on part of users.
The user should just input the data and get the desired output after processing.
The user should not be concerned with internal logic of the program or how
the program is working to get the results. The program should generate proper
messages and guide the user in using the program.

Self-documenting code
The program should be self-documenting in itself i.e. it should have proper
meaningful names for constants, variables and functions used in the program to
indicate their purpose. Their meaning and purpose should be reflected by their
names.

Reliability
Reliability of a program refers to its capability of handling the situations that
are less likely to arise. For example, user might input wrong data instead of the
desired one. We should make proper provisions in our program to handle such
situations.

Efficiency
Efficiency of a program refers to the speed at which the program operates. An
accurate, efficient and reliable program loses its importance if it is very time
consuming.

Portability
A good program should be capable of running on different platforms without
doing much change in the source code.

JBD
154 Computer Studies-9

Program Development Cycle

Program development cycle is a type of methodology used to describe the process
for building information systems, intended to develop information systems in a
very deliberate, structured and methodical way, reiterating each stage of the life
cycle.
The program development cycle can be divided into several stages:
• Analyze the problem
• Program design
• Coding the solution
• Test and debug programs
• Documentation
• Maintenance

Analysis

Maintenance Program Designing
Documentation Development Cycle Coding

Test and Debug

Phase 1 : Analyze the Problem

The first and the foremost phase is careful understanding of the problem.
Analyzing the problem involves identifying the program specifications and
defining each program’s input, output and processing components. Poor
understanding of the problem may lead to incorrect analysis and programming
resulting in unexpected output.

Phase 2 : Program Design

Program design is the first step in attempting to design a new piece of software,
whether it be an addition to an existing software, a new application, a new
subsystem or a whole new system. It is the stage where the solution to the
program is planned. There are various tools to develop the problem solving logic.
They are:

JBD
Computer Studies-9 155

Algorithm

Algorithm (pronounced AL-go-rith-um) is a procedure or formula for solving a
problem. It generally takes some input, carries out a number of effective steps in
a finite amount of time and produces some output.

Flowchart

Flow chart, or flow diagram, is a graphical representation of a process or system
that details the sequencing of steps required to create output. Flow charts can be
used to document virtually any type of business system, from the movement of
materials through machinery in a manufacturing operation to the flow of applicant
information through the hiring process in a human resources department.

Phase 3 : Coding the Solution
The third step is the process of transforming the program logic design into a
programming language. This stage translates the program design into computer
instructions. These instructions are the actual program or software product. This
step is considered complete when a clean compile walkthrough has been held
and the program coding has been approved.

Phase 4 : Test and Debug Programs
This stage is the testing and debugging of programming errors. Testing is the
process of finding errors in a program, and debugging is the process of correcting
errors found during the testing process. Testing is very important because once
the program is put into use, users will rely on the program and its output, to
support daily activities and decisions. Thus, the goal of program testing is to
ensure that the programs runs correctly and is error free.

Phase 5 : Documentation
This phase is important in the maintenance and redesign stages. Documentation is
intended to allow another person, or the programmer at later date, to understand
the program. Internal documentation consists of statements in the program that
are not executed, but point out the purposes of various parts of the program.
Documentation consists of a detailed description of what the program does and
how to use the program.

Phase 6 : Maintenance
Maintaining programs involves modifying the programs to remove previously
undetected errors, to enhance the program with different features or functionality,
or to implement the changed requirements of users.

JBD
156 Computer Studies-9

Programming Language

Programming language is a set of words, symbols and codes that enables
a programmer to communicate a solution-algorithm to the computer. Each
programming language has a particular syntax, or a set of grammar and rules,
that specifies how to write the instructions in a solution-algorithm.
Programming languages are classified as under, varying with their closeness to
the actual hardware of the machine.
• Machine level language
• Assembly language
• High level language
• Fourth generation language
• GUI based language

Machine Level Language
Machine level language is the native binary language recognized and executed by
a computer’s central processing unit (CPU). It is a low-level language symbolised
by 0s and 1s that corresponds to the on and off electrical states of a computer.
It has the advantage of very fast execution speeds and efficient use of primary
memory. Machine language programs run only on the computer for which they
are developed, i.e. they are machine dependent. They are not portable. Machine
language programs cannot be easily transferred from one machine to another as
the user find it difficulty to understand the language.

Advantages:
• Program execution is faster.
• Machine language do not require translating programs such as assemblers,

compilers and interpreters.
• Machine language instructions can be used to manipulate the individual bits

in a byte of computer storage.

Disadvantages:
• Machine language is difficult to program as it uses 0’s and 1’s.
• Machine language is time consuming and difficult to update or edit.
• Machine language is machine dependent. Programs written in machine

language in one machine cannot be easily transferred to other machines.

JBD
Computer Studies-9 157

Assembly Language

Assembly language is an intermediate-level computer language that is less
complex to use than a machine language. Assembly languages use abbreviations
or mnemonic codes to replace the 0s and 1s of machine language. A translator is
required to convert the assembly language program into machine language that
can be executed by the computer. This translator is the assembly program. Every
command in assembly language has a corresponding command in machine
language. The assembly language differs among computers, and thus these
programs are not easily transferable to machines of a different type from the one
on which they were written.

Advantages:
• Simple to understand and use.
• Less time is required to write the program.
• Program debugging is easier.

Disadvantages:
• It is machine dependent i.e each design of the machine has a different

assembly language.
• Assembly language is difficult to understand.
• Assembly language must be translated into machine language before the

computer can understand it.

High Level Language

High level language is a programming language that uses instructions closer to
English. These languages were designed to make programming far easier, less
error-prone and to remove the programmer from having to know the details of
the internal structure of a particular computer. They are machine independent
programs and are problem oriented. High-level languages must also be translated
into machine language by a program before the instructions are executed. A
program called a compiler or interpreter does the translation.

Advantages:
• High level language is machine independent. That is, a program written in a

high-level language can be easily transferred and executed on any computer.
• High level language do not require the programmer to know the internal

structure of the computer.
• High level language enables the programmers to write instructions using

English words and familiar mathematical symbols and expressions.

JBD
158 Computer Studies-9

Disadvantages:
• Expensive hardware supports are required.
• A program in high level langauge occupies a large memory.
• The speed of operating the high level language is slow.

Fourth Generation Language 4GL
Fourth generation language is a programming environment language designed
with a specific purpose in mind, such as the development of commercial business
software. It is more oriented towards problem solving and systems engineering.
They allow multiple common operations to be performed with a single
programmer-entered command. They are designed to reduce programming
effort and the cost of software development. Such a language typically provides
easy ways of designing screens and reports, and of using databases. Some of the
common fourth generation languages are dBase, FoxPro, Microsoft Access and
Oracle.

Fifth Generation Language 5GL
5GL or fifth-generation language is programming that uses a visual or graphical
development interface to create source language that is usually compiled with a
3GL or 4GL language compiler. Microsoft, Borland, IBM, and other companies
make 5GL visual programming products for developing applications in Java,
for example. Visual programming allows you to easily envision object-oriented
programming class hierarchies and drag icons to assemble program components.
The fifth generation languages, or 5GL are programming languages that contain
visual tools to help develop a program. A good example of a fifth generation
language is Visual Basic.

JBD
Computer Studies-9 159

C Dompu- ictionary

Algorithm : A set of well-defined rules for solving a problem in a
finite number of times.
Assembly language :
A programming language, which allows instructions
Compiler : and storage location to be represented by letters and
symbols instead of numbers.
Flowchart :
A special program that translates a high level language
High-level language : program into a machine code in a single operation.

Interpreter : A pictorial representation of input, processing and
output.
Recap
A programming language whose structure is
application oriented, and is independent of the
structure of the computer.

A program that translates one statement of a high-level
language program into machine codes and executes it.

• A set of instructions that are grouped together to accomplish a task or tasks is
called program and the development of program is known as programming.

• The primary role of a computer programmer is to write programs according
to the instructions determined primarily by computer software engineers
and systems analysts.

• A good program is more readable and easier to understand.

• Program development cycle is a type of methodology used to describe the
process for building information systems, intended to develop information
systems in a very deliberate, structured and methodical way, reiterating each
stage of the life cycle.

• An algorithm (pronounced AL-go-rith-um) is a procedure or formula for
solving a problem.

• A flow chart, or flow diagram, is a graphical representation of a process or
system that details the sequencing of steps required to create output.

• Programming language is a set of words, symbols and codes that enables a
programmer to communicate a solution-algorithm to the computer.

• Machine level language is the native binary language recognised and executed
by a computer’s central processing unit (CPU).

Fourth generation language is a programming environment language
designed with a specific purpose in mind, such as the development of
commercial business software.

JBD
160 Computer Studies-9

Review Yourself

1. State whether the following statements are true or false.
a. The program should produce accurate results quickly and should use the
least amount of memory.
b. A flowchart is a procedure or formula for solving a problem.
c. Assembly language is a low-level language symbolised by 0s and 1s that
corresponds to the on and off electrical states of a computer.
d. Every command in assembly language has a corresponding command in
machine language.
e. High level language do not require the programmer to know the internal
structure of the computer.

2. Fill in the blanks.
a. A set of instructions that are grouped together to accomplish a task or
tasks is called _________________.
b. A _________________ is a graphical representation of a process or system
that details the sequencing of steps required to create output.
c. _____________________ consists of a detailed description of what the
program does and how to use the program.
d. _______________ is a set of words, symbols and codes that enables a
programmer to communicate a solution-algorithm to the computer.
e. ________________is the native binary language recognised and executed
by a computer’s central processing unit (CPU).
f. _________________ is a programming language that uses instructions
closer to English.

3. Write the technical term for the following statements.
a. A set of instructions that tells the computer how to solve a problem or
perform a task.
b. A set of well-defined rules for solving a problem in a finite number of
times.
c. A pictorial representation of input, processing and output.
d. The native binary language recognised and executed by a computer’s
central processing unit (CPU).
e. A program which has to be program translated into machine language.

JBD
Computer Studies-9 161

4. Match the following.

High-level language A set of well-defined rules for solving a problem in a

finite number of times.

Compiler A programming language, which allows instructions

and storage location to be represented by letters and

symbols instead of numbers.

Flowchart A special program that translates a high level language

program into a machine code in a single operation.

Assembly language A pictorial representation of input, processing and

output.

Algorithm A programming language whose structure is

application oriented, and is independent of the

structure of the computer.

5. Answer the following questions.

a. What is a computer program?
b. What are the characteristics of a good program?
c. What are the different phases of the program development cycle? Explain

them in brief.
d. Draw a neat diagram of program development cycle?
e. What is a programming language? Name the three categories of

programming language and explain them in short.
f. What is machine language?
g. List any two advantages and disadvantages of machine language.
h. What is source program and object program?
i. Explain the differences in the way that compiler and interpreter translate

instructions.
j. What is assembly language?
k. List any two advantages and disadvantages of assembly language.
l. What is high-level language? List any two advantages of high-level

language.
m. What is fourth generation language?
n. What is fifth generation language?

JBD
162 Computer Studies-9

Chapter 13

PROGRAMMING
TECHNIQUES

Objectives

After completing this chapter, you will be able to:
y Explain the most commonly used techniques in programming.
y Define algorithm and flowchart.
y List the advantages and disadvantages of using a flowchart.
y Explain the rules for designing a flowchart.
y Explain the three main types of flowchart.

C Ooncept verview

Computers are problem solving
devices. To facilitate a computer to
solve problems, effectively, clear and
concise instructions must be provided
to it. For this purpose, the logical
solution to any problem is first outlined
as a series of steps in a convenient
form. Any confusions and errors in the
solution are sorted out at this stage.
The solution is then converted into
a computer. There are basically two
ways of specifying a logical solution
to a problem. They are algorithm and
flowchart.

JBD
Computer Studies-9 163

Algorithm

The word algorithm originated from the name of a famous Arab mathematician,
“Abu Jafar Mohammad Ibn Musa Al-Khowrizmi”. The last part of his name
“Al-Khowarizmi” was converted and a new term was coined as “Algorithm”.
Algorithm is a logical sequence of discrete steps that describe a complete solution
to a given problem in a finite amount of time. The success of a program depends
largely upon the clarity and efficiency of algorithm. If the algorithm is not properly
designed, it makes the program prone to errors. It is composed of a finite set of
steps, each of which may require one or more operations.

The characteristics of algorithm are:
• Each step of the algorithm should be simple.
• The logic of each step should be clear.
• It should be effective i.e. it must lead to unique solution of the problem.
• An algorithm must come to an end after a finite number of steps.
• Every step of the algorithm must be accurate and precise. At the same time, it

must have the time limit for execution.
• The output must be logically correct.

After analyzing the problem correctly, the programmer has to understand the
problem in order to develop a logic or method that can solve the problem. A
computer program is a sequence of instructions outlining the steps to be performed
by a computer. Translating an algorithm into a programming language is known
as coding. The logic of the program should be simple, easy to understand and
one that can run smoothly on a computer.
The steps used to develop an algorithm are:
• The problem has to understood thoroughly.
• The expected output has to be identified.
• The inputs required by the problem, to achieve the desired output, have to be

identified.
• A logic or process, that will produce the required output from the given input,

has to be developed.
• The algorithm should be tested for accuracy for a given set of input data.
• The steps given above are repeated till the algorithm produces desired results.

JBD
164 Computer Studies-9

Solved Examples

Write an algorithm to print the multiples of a given number.
Solution:
Step 1 : Start
Step 2 : c = 0
Step 3 : Read the value of a number in n.
Step 4 : Compute and display n * c
Step 5 : c = c + 1
Step 6 : Is c <=10
{ If yes: Goto step 3
no: Goto step 7}
Step 7 : Stop

Flowchart
Flowchart is a pictorial representation of step by step solution of a problem. It is
a process of breaking down the design specifications into the specific input and
output, text manipulation and calculation, logic and comparison, storage and
retrieval to satisfy the different users. It helps the programmer in developing the
program logic and to serve as documentation for future reference for a completed
program. It uses different boxes linked by arrows. In each box, the instruction to
be carried out are mentioned. Arrows on the lines connecting the boxes indicate
the direction in which we should proceed. The process of drawing a flowchart is
known as flowcharting.

Advantages of a flowchart
The following advantages may be obtained when flowcharts are used for the
purpose of program planning.
• Flowchart helps the programmer to explain the logic of a program to some

other programmer easily.
• Flowchart provides valuable documentation support.
• Once a flowchart is ready, programmers find it very easy to write the

corresponding program.
• Flowchart is very helpful in detecting, locating and removing bugs in a

program in a systematic manner.
• Flowchart proves to be very helpful in designing the test data for systematic

testing of program.
• Flowchart helps to detect deficiencies in the problem statement.
• Flowchart serves as a guide for program coding.

JBD
Computer Studies-9 165

Disadvantages of a flowchart

In spite of their advantages, flowcharts have some limitations, which are as
follows:
• Flowcharts are very time consuming, and laborious to draw with proper

symbols and spacing especially for large complex programs.
• Flowcharts are difficult to amend. Re-drawing of flowchart is required.
• When there are complex branches and loops, flowchart becomes complicated.

Flowchart rules

The American Standard Institute (ANSI), has recommended some general
principles to help standardize the designing of a flowchart. Some of the rules and
guidelines are as follows:

• The direction of flowchart is from top to bottom or from left to right.

• Convention flowchart symbols can be used. Each symbol should have one
entry point. Entry point is a must for the decision symbol. Each symbol
should have one exit point, except the decision symbol.

• The flow of control can be shown with the help of arrows. But the flow lines
should not cross each other.

• The instructions written within the various flowchart symbols are independent
of programming languages.

• When drawing a chart on more than one sheet of paper, connectors can be
used to avoid crossing of flow lines.

• The flowlines, coming out of the decision symbol, should be properly labelled.

• Every flowchart must have start and end points.

Flowchart symbols
Flowchart consists of simple geometric symbols for different functions. Some of
the basic flowchart symbols are given below:

Symbol Meaning
Terminal box
Terminal box marks the start and end points of a flowchart.
It usually contains the words “start” or “stop”.

Input/Output Input/Output indicates box an input or output operation. It
will appear at any point where data is to be entered into the
computer or at times when data is to be obtained from the
computer.

JBD
166 Computer Studies-9

Processing box Processing box indicates any calculation or manipulation.
Decision box
Decision box is used to show the steps involving comparison
and decision making steps. It has one entry and two exit
points which shows true and false.

Comment box Comment box can be used to include any comments for
better documentation, clarity and in case of explanatory
notes.

Connector Connectors are used for long flowchart that does not fit in
the same page. In this condition the flowchart can be broken
into parts and an arrow is drawn into a connector symbol
containing a unique number or letter.

Flow lines Flow lines are used to connect the flowchart symbols.

Flowchart structure
The flowchart uses three types of logics. They are:

Sequence structure
Sequence structure is a series of actions or steps performed in sequential order.
In this type of structure, all instructions are executed in a strict sequence of their
order of appearance in a program. The flowchart can be drawn as below:

Start

Step A
Step B
Step C

Stop

JBD
Computer Studies-9 167

Algorithm
Step 1 : Start
Step 2 : Read two numbers and store them in variables a and b.
Step 3 : Add a and b and store the result in variable c.
Step 4 : Display the value of variable c.
Step 5 : Stop

Flowchart Start
ReaSdteapaAnd b

c=a+b

Display c

Stop

Decision structure

In a decision structure, one of two possible actions is performed, depending on
a condition. In a decision structure, a new symbol, the diamond, represents a
yes/no question. If the answer to the question is “yes” the program flow follow
the path. If the answer to the question is “no”, the program flow follows another
path.

Entry

True Condition False

Step A Step B

Exit

JBD
168 Computer Studies-9

Algorithm
Step 1 : Start
Step 2 : Read two numbers and store them in variables a and b.
Step 3 : Compare a and b. If a is greater than b then print a otherwise print b.
Step 4 : Stop

Flowchart Start
Read a and b

True Is a > b? False

Display c Display c

Stop

Repetition structure

Repetition structure represents part of the program that repeats for a certain
number of times. The repetition structure also uses a diamond symbol for
specifying condition. Repetition structure tests a condition and if the condition
is true, it performs an action. Then it tests the condition again if the condition
is still true, the action is repeated. This process continues until the condition is
false.

False
Condition

True
Step A

Exit

JBD
Computer Studies-9 169

Algorithm
Step 1 : Start
Step 2 : Initialize the variable count to one.
Step 3 : Display the variable count.
Step 4 : Increase variable count by one.
Step 5 : Check whether count variable exceeds 10.
{If yes, go to step 3.
If no, go to step 6.}
Step 6 : Stop

Start

Count = 1

Display Count

Count = Count + 1

Is
Count <=10?

Stop

Pseudocode
Pseudocode is an outline of a program, written in a form that can easily be
converted into real programming statements. It cannot be compiled or executed,
and there are no real formatting or syntax rules. It is simply one step - an important
one - in producing the final code. The benefit of pseudocode is that it enables the
programmer to concentrate on the algorithms without worrying about all the
syntactic details of a particular programming language. In fact, you can write
pseudocode without even knowing what programming language you will use
for the final implementation.

JBD
170 Computer Studies-9

C Dompu- ictionary

Algorithm : A logical sequence of discrete steps that describe
a complete solution to a given problem in a finite
amount of time.

Decision box : A symbol used to show the steps involving comparison
and decision making steps. It has one entry and two
exit points which shows true and false.

Decision Structure : A logic that consists of a test for some conditions
followed by two alternative program control paths.

Flowchart : A pictorial representation of step by step solution of a
problem.

Pseudocode : An outline of a program, written in a form that can
easily be converted into real programming statements.

Repetition Structure : A logic that is used to execute an instruction or group
of instructions more than once without having to
recode them.

Recap

• The word algorithm originated from the name of a famous Arab
mathematician, “Abu Jafar Mohammad Ibn Musa Al-Khowrizmi”.

• Algorithm is a logical sequence of discrete steps that describe a complete
solution to a given problem in a finite amount of time.

• A computer program is a sequence of instructions outlining the steps to be
performed by a computer.

• Flowchart is a pictorial representation of step by step solution of a problem.

• Flowchart helps the programmer to explain the logic of a program to some
other programmer easily.

• Flowcharts are very time consuming, and laborious to draw with proper
symbols and spacing especially for large complex programs.

• Sequence structure is a series of actions or steps performed in sequential order.

• In a decision structure, one of two possible actions is performed, depending
on a condition.

• Repetition structure represents part of the program that repeats for a certain
number of times.

• Pseudocode is an outline of a program, written in a form that can easily be
converted into real programming statements.

JBD
Computer Studies-9 171

Review Yourself

1. State whether the following statements are true or false.
a. Flowchart is a logical sequence of discrete steps that describe a complete
solution to a given problem in a finite amount of time.
b. Decision box is used to show the steps involving comparison and decision
making steps.
c. In a sequence structure, one of two possible actions is performed,
depending on a condition.
d. Repetition structure represents part of the program that repeats for a
certain number of times.
e. Pseudocode is an outline of a program, written in a form that can easily
be converted into real programming statements.

2. Fill in the blanks.
a. ___________ is a logical sequence of discrete steps that describe a complete
solution to a given problem in a finite amount of time.
b. ___________ is a pictorial representation of step by step solution of a
problem.
c. _________________ box indicates any calculation or manipulation.
d. ______________ box is used to show the steps involving comparison and
decision making steps.
e. _________________ logic means the statements are being executed
sequentially.
f. _________________ is an outline of a program, written in a form that can
easily be converted into real programming statements. e. A program
which has to be program translated into machine language.

3. Answer the following questions.

a. What is an algorithm? What are the features of an algorithm?
b. What do you understand by the term flowchart?
c. State the advantages and disadvantages of a flowchart.
d. What are the rules for construction of a flowchart?
e. What are the different flowchart structures? Define each of them.
f. What is pseudocode?

JBD
172 Computer Studies-9

4. Write an algorithm for the following problems.
a. To calculate the volume of a box.
b. To find the smallest number among three supplied numbers.
c. To print the first ten natural numbers.
d. To display the multiplication table of 5.
e. To print the numbers divisible by 2 from 10 to 30.
f. To compute the sum of the first 10 natural numbers.

5. Draw a flowchart for the following problems
a. To calculate the area of a circle.
b. To calculate the volume of a box.
c. To input two numbers and print the greatest among them.
d. To input an integer and find whether it is an even or an odd.
e. To generate the first ten odd numbers.
f. To find the sum of multiple of 5 from 10 to 100.
g. To find the sum of first 50 even numbers.
h. To print squares of first 10 natural numbers.

6. Draw a flowchart after reading the following algorithm.
a.
Step 1 : Start
Step 2 : Initialize the variable count to one.
Step 3 : Display the variable count.
Step 4 : Increase variable count by one.
Step 5 : Check whether count variable exceeds 10.
{If yes, goto step 3.
If no, goto step 6.}
Step 6 : Stop

JBD
Computer Studies-9 173

b.
Step 1 : Start
Step 2 : Initializing COUNT to zero.
Step 3 : Read the number in N.
Step 4 : Incrementing count by 1.
Step 5 : Compute N * COUNT;
Store in MUL variable and print MUL.
Step 6 : Is count<10
{Yes: go to step 4
No: go to step 7}
Step 7 : Stop

c.
Step 1 : Start
Step 2 : Read Principle, Rate, Time
Step 3 : Multiply Principle x Rate x Time
Step 4 : Divide by 100
Step 5 : Write the answer
Step 6 : Any more calculations?

{Yes: goto step 2
No: goto step 7}
Step 7 : Stop

d.
Step 1 : Start
Step 2 : Read the telephone number from the directory or diary.
Step 3 : Lift the receiver.
Step 4 : Is there a dial tone?
{Yes, then dial the number.
No, then put down the receiver and goto step 3.}
Step 5 : Speak to your friend.
Step 6 : Put down the receiver.
Step 7 : Stop

JBD
174 Computer Studies-9

Chapter 14

BASICS OF
QBASIC

PROGRAMMING

Objectives

After completing this chapter, you will be able to:
y Explain the salient features of QBASIC.
y Explain the elements of QBASIC programming.
y Differentiate between constants and variables.
y Define operator and explain the different types of operators supported by QBASIC.
y Identify the various methods of declaring variables.

C Ooncept verview

BASIC stands for Beginners’ All Purpose Symbolic Instruction Code. It is one of
the most popular high level language. It was developed in 1964 at Dartmouth
College, U.S.A. by Professors John Kemeny and Thomas Kurtz. It allows the usage
of English-like language and use mathematical notation. Since, its development
BASIC has undergone many modifications and improvements and now many
versions of BASIC are available, each version having some extra features.

QBASIC is a high level computer language published by Microsoft. It is provided
with MS-DOS and has two important files: QBASIC.EXE and QBASIC.HLP. Since
then, nearly every PC user owns their own copy of QBASIC, making it a widely
known language. QBASIC environment includes a full screen syntax checking,
multi-file and multi-window editing, full debugging facilities, pull-down menus,
syntax-checking editor and a simple yet a powerful menu structure that can be
driven through either by the keyboard or a mouse. Once you are in the QBASIC
environment you can edit, run, debug, and rerun the program without switching
programs.

JBD
Computer Studies-9 175

The salient features of QBASIC are:
• Suitable for conversational programming
• Facility of manipulation of character strings
• Dynamic program debugging ability to carry out arithmetic operations
• Simplified grammar
• Facility for allowing more sophisticated formats for results
• Alphanumeric labels can be used making the programs more readable and
writable because line numbers are not required in QBASIC for its program
statements.
• Facility for real-time execution and task scheduling
• Suitable for both mathematical and business problems
• Easy to understand and learn

Starting QBASIC
To load QBASIC, you must load it into the memory of your computer. The
procedure of starting QBASIC is given below.
• Turn on your computer to get DOS prompt.
C: \>
• Change the directory to QBASIC directory.
C: \>CD QBASIC
• Type QB and press Enter.
C: \QBASIC>QB
Or
• Turn on the computer to load Windows Operating System and locate the
QBASIC folder.
• Double click on the QBASIC folder.
• Double on the QB.EXE icon.

Now, you are presented with a welcome screen. Press Esc to clear the dialog
box. The editor screen appears. The editor screen is available for you to enter
a program and run it. It is divided into four parts: menu bar, edit window,
immediate window and status bar.

JBD
176 Computer Studies-9

Using QBASIC Menus and Commands
Menu bar is a one-line, horizontal menu at the top of the edit window. It contains
the various choices that you can select from. The selection can be done using the
mouse or the keyboard.

Menu action With the mouse With the keyboard
Display menu
Move the mouse pointer Press ALT to highlight
to the menu name, then menu letters, then press the
press and release the letter key.
mouse button.

Choose a command Click the command name. Press the letter key that
Canceling a command Click outside the menu. matches the highlighted
letter on the command.

Press ESC.

The menu bar contains the following options:

Commands Use

FILE Loads existing program from the disk into the
Open memory.
Save the current program into the disk.
Save Save the current file under another name.
Save As Prints the program.
Print Quit QBASIC editor and returns to DOS/Microsoft
Exit Windows.

EDIT Deletes selected text; save in clipboard.
Cut (Shift+Del) Copies selected text; save in clipboard.
Copy (Ctrl+Ins) Insert from clipboard.
Paste (Shift+Ins) Deletes selected text; not saved in clipboard.
Clear (Del) Opens window for a new SUB program.
New Sub Opens a window for a new FUNCTION procedure.
New Function
VIEW Lists the loaded files (SUB, FUNCTION, module).
SUBs (F2) Splits the screen into two view windows.
Split View the output screen.
Output Screen (F4)

JBD
Computer Studies-9 177

SEARCH

Find… Finds selected text.

Repeat Last Find (F3) Repeats last find command.

Change… Finds and replaces.

ALIGN It is used to control the vertical alignment of the text
adjacent

RUN

Start (Shift+F5) Start program execution from the beginning.

Restart Clears the variables in preparation.

Continue (F5) Continues with program execution.

DEBUG

Step (F8) Executes next program statement as a single step.

Procedure Step (F10) Executes next statement and traces around procedure.

Trace On Causes the program to be executed step-by-step while
highlighting the statement being executed.

Toggle Breakpoint (F9) Turns breakpoint on and off.

Clear All Breakpoints Removes all breakpoints from your program.

Set Next Statement Changes the program execution sequence so the next
statement executed is the one the cursor is on.

OPTIONS

Display… Changes the display settings.

Help Path… Set search path for the QBasic help files.

Syntax checking To turn syntax checking on and off.

HELP

Index To display the help index.

Contents To display the help table of contents.

Topic (F1) To display information on a topic determined by the
current location of the cursor.

Using help (Shift+F1) To display the use of help topic.

About To display the version number and copyright information
for QBASIC.

JBD
178 Computer Studies-9

Elements of QBASIC programming
he important elements of QBASIC language are:
• Character set
• Constants
• Variables
• Operators and expressions
• Reserved words
• Syntax

Character set
The QBASIC character set is a set of valid characters that a language can recognise.
A character includes alphabetic, numeric and special characters.

Constants
The constants are data items that never change their value during a program
execution. There are two types of constants:
• Numeric constant
• String constant

Numeric constant
Numeric constants are positive or negative numbers on which mathematical
operations such as addition, subtraction, multiplication, and division can be
performed. In BASIC, they cannot contain commas. They are entered in the same
form that they are in. Some of the valid numeric constants are 150 and -980.

String constant
String constant is a set of alphanumeric or special characters enclosed within
double quotes. Blank spaces can also be used in a string. These type of data
cannot be used for mathematical calculations but can be used for comparison
and references.

Variables
Variables refer to a storage area whose contents can vary during processing. All
the variables are created in Random Access Memory. The data stored in a variable
can change during the execution of the program.
The variable names have the following properties:

JBD
Computer Studies-9 179

• Any variable name may be up to 40 characters long.
• Variable names must begin with a letter ( A..Z, a..z ).
• Cannot contain characters other than letters, numbers, period, and the type

declaration characters ($, %, #, &, and !).
• Cannot be a reserved words, although embedded reserved words are allowed.
• Variables may represent either numeric values or strings.
• Cannot begin with “FN” unless it is a function call.
The variables are of two types:numeric variable and string variable depending on
the kind of data item they represent.

Numeric variable
Numeric variable can be any single letter of the alphabet followed by one of the
numerals. Numeric variables may be declared as:
i. Integers and long integers
Integer variable is declared by using a percent sign (%) as the last character of

the variable name. Integers are stored as 2 bytes ranging in value from -32768
to +32767.
Long integer variable is declared
by using ampersand sign (&) as
the last character of the variable name. Long integers are stored as 4 bytes
ranging in value from -2,147,483,648 to +2,147,483,647.
For examples,
A% = 16.5
would cause the value of A% to be 16.

ii. Single precision variable
Single precision variable is declared by using an exclamation point (!) as
the last character of the variable
name.
For example,
C! = 123.456789
would cause the value of C! to be 123.4568.

iii. Double precision variable
Double precision variable is declared by using a number sign (#) as the last

character of the variable name.

JBD
180 Computer Studies-9

String variable
The string variable has a string of characters or alphanumeric as its value. It must
begin with an alphabet and end with dollar($) sign. Mathematical calculations
cannot be performed using string variables. Some of the valid string variables are
A$, B3$, C$ and TL$.
String variables are of two types:
i. Variable-length string
Variable-length string variables contain character data of arbitrary length.

Internally, each string variable uses four bytes that contain a handle number,
which is used to identify and locate information about a string. The type-
specifier character for a dynamic string is: $.
String variables are designated by following the variable name with a dollar
sign ($) or the DEFSTR type definition.
You can also declare dynamic string variables using the STRING keyword
with the DIM statement. For example:
DIM MyStr AS STRING

ii. Fixed-length string
As their name implies, fixed-length strings have a pre-defined length, and

any attempt to assign a string longer than the defined length will result in
truncation. If you assign a string to a fixed-length string that is shorter than
the defined length, the string will be padded on the right with spaces. The
major difference between dynamic strings and fixed-length strings is that
once defined, the length of a fixed-length string cannot be changed. It is
“fixed” for the duration of program execution.
You declare fixed-length string variables using the STRING * x format with
the statement. For example:
DIM MyStr AS STRING * 10

Variable Declaration
(Implicit and Explicit)
Broadly, variable declaration in QBASIC can be classified as:

Explicit declaration
The DIM statement is one of the most powerful statements for declaring explicit
variables.
The general format is:
DIM variable AS type

JBD
Computer Studies-9 181

Where,
The variable component is a valid QBASIC variable name. The AS type component
defines the type of the variable. The type can be a simple type (Integer, Long,
Single, Double, and String) or a user defined type.
Example:
DIM FirstName AS String
FirstName=”Tony”

Implicit declaration

Type declaration character is used in an assignment statement to declare implicit
declaration. A type declaration character is a symbol that specifies the data type
of the variable and eliminates the use of the AS clause with the DIM statement.

The different type of declaration in QBASIC are:

Type Declaration Data Type
$ String
% Integer
& Long Integer
# Double Precision
! Single Precision

Symbolic Constants

Symbolic constants are used when a value is not expected to change during the
execution of the program and the value is used more than once in the program.
The CONST statement is a non-executable statement that declares symbolic
constants to use in place of numeric or string values. The general format of
CONST is:

CONST constantname = expression

[, constantname = expression]

Where,

constantname is a name following the same naming rules as a BASIC variable.

expression consists of literals, other constants or any of arithmetic and logical
operators except exponentiation (^).

Operators and Expressions
An expression is a programming language statement that has a value. Usually,
an expression consists of an operator and its operands. The operations (specific
tasks) are represented operators and the objects of the operations(s) are referred
to as operands.

JBD
182 Computer Studies-9

Operators perform mathematical or logical operations on values. BASIC supports
the following types of operators: arithmetic, relational, logical and string
operators.

• Arithmetic operators

Arithmetic operators are used for various mathematical calculations.
Arithmetic operators perform arithmetic operations such as addition,
subtraction, negation, division, multiplication and exponentiation. A numeric
expression is any collection of operators and operands that can arithmetically
evaluate to produce a single numeric result.

The following are the arithmetic operators used in QBASIC. They appear in
order of precedence.

Operator Operation Example
^ Exponential A^B
- Negation -B
* Multiplication A*B
/ Floating-point A/B
division
\ Integer Division A\B
MOD Modulus A MOD B
Division
+ Addition A+B
- Subtraction A-B

Operations within parenthesis are performed first. Inside the parenthesis, the
usual order of precedence is maintained. The table given below shows some
expressions with their values:

Operator Operation
3+2*2 7
12 / 2 - 3 3
(2 + 2) * 4 16
(9 - 3) / 3 2
6 - 3 * 2 + 7 -1 6

The table given below shows sample algebraic expressions and their BASIC
counterparts:

JBD
Computer Studies-9 183

Algebraic Expression BASIC Expression
2*a*c+4*t
2ac+4t (3*x+5)/4
3x + 5
2 * x^2 + 5
4
2x2+5 6*a^2


6a2

Solved Examples

Let the value of w=5, x=4, y=8 and z=2. What value will be stored in result
in each of the following statements?
result = x + y
result = x * 2
result = y / x
result = y - z
result = w mod 2
Solution:
result = 4 + 8 = 12
result = 4 * 2 = 8
result = 8 / 4 = 2
result = 8 - 2 = 6
result = 5 mod 2 = 1

• Relational operators

Relational operators are used to compare two values of same type, either
both numeric or both string. The relational operators supported by QBASIC
are:

Operator Name Example
= Equal A=B
< Less than A<B
> Greater than A>B
<= Less than or equal to A<=B
>= Greater than or equal to A>=B
<> Not equal to A<>B

JBD
184 Computer Studies-9

Solved Examples

Illustrate the usage of relational operators by using a and b as operands. The
values of a = 10 and b = 20.

Solution:

Expression Result

a>b False

a + 10>b False

a<b True

a<>b True

• Logical operators

QBASIC supports logical operators to perform logical operations on numeric
values. Logical operators are used to connect two or more relations and return
a TRUE or FALSE value to be used in a decision.

The logical operators supported by QBASIC are:

Operator Name Example
And Conjunction A>B and A>C
Or Disjunction A>B or A>C
Not Negation A not B

Rules of logical operators
• The output of the logical AND operations is true if BOTH the logical

expressions are individually TRUE.
• The output of the logical OR operations is true if EITHER of the logical

expressions is TRUE.
• The logical NOT is a negative check operator. The output of logical NOT

operation is true if the expression being tested is false.

Solved Examples

Demonstrate the use of logical operators for variables: a = 5 and b = 10.
Evaluate the following expressions:

i. (a>=5) and (b<=15)

ii. (a=5) and (b>15)

iii. (a=5) or (b<15)
iv. (a>10) or (b>20)

JBD
Computer Studies-9 185

Continued

Solution:

Expression Result

(a>=5) and (b<=15) True

(a=5) and (b>15) False

(a=5) or (b<15) True

(a>10) or (b>20) False

• String operator

The joining of two or more strings is called concatenation of strings. The
strings are connected by the string operator, which is the plus sign (+).
Solved Examples

A$ = “Enjoy”

B$ = “Programming”

C$ = A$ + B$

PRINT C$

END

Reserved words
Reserved words are those that have special meaning with reference to QBASIC
and are used to perform certain specific tasks. These words cannot be used for
any purpose other than the one specified in QBASIC. They are also known as
keywords. Some of the keywords of QBASIC programming are LET, INPUT,
PRINT, IF...THEN and so on.

Syntax

Syntax is the set of rules governing the construction of valid statements in a
programming language. It dictates how keywords and operators may be used.
QBASIC is very rigid about these rules. Forgetting a comma or a space or a small
spelling mistake will make the QBASIC to produce “Syntax error” message and
stop program execution.

JBD
186 Computer Studies-9

C Dompu- ictionary

Constants : The data items that never change their value during a
program execution.

String constant : A set of alphanumeric or special characters enclosed
within double quotes.

Variables : A storage area whose contents can vary during
processing.

Symbolic constants : The constants that is used when a value is not expected
to change during the execution of the program and the
value is used more than once in the program.

Expression : A programming language statement that has a value.

Recap

• BASIC was developed in 1964 at Dartmouth College, U.S.A. by Professors
John Kemeny and Thomas Kurtz.

• The QBASIC character set is a set of valid characters that a language can
recognise.

• The constants are data items that never change their value during a program
execution.

• The numeric constants are positive or negative numbers on which
mathematical operations such as addition, subtraction, multiplication, and
division can be performed.

• The string constant is a set of alphanumeric or special characters enclosed
within double quotes.

• Variables refer to a storage area whose contents can vary during processing.

• Symbolic constants are used when a value is not expected to change during
the execution of the program and the value is used more than once in the
program.

• An expression is a programming language statement that has a value.

• Arithmetic operators are used for various mathematical calculations.

• Relational operators are used to compare two values of same type, either
both numeric or both string.

• The joining of two or more strings is called concatenation of strings.

• Reserved words are those that have special meaning with reference to
QBASIC and are used to perform certain specific tasks.

• Syntax is the set of rules governing the construction of valid statements in a
programming language.

JBD
Computer Studies-9 187

Review Yourself

1. Answer the following questions.
a. What do you understand by the language BASIC?
b. What are the features of BASIC language?
c. What are the important elements of QBASIC programming language?
d. What is a variable? How does it differ from constant?
e. What are rules for naming a variable in QBASIC?
f. What do you understand by explicit and implicit variable declaration?
g. What is a relational operator?
h. What is a logical operator? Mention the rules of logical operator.
i. What are reserved words?
j. What is syntax?

2. Write down the appropriate variables for the following.
a. ______________ = “Computer Studies”
b. ______________ = 2*(L+B)
c. ______________ = “14 April 2015”
d. ______________ = 1/2*b*h
e. ______________ = 22.99
f. ______________ = “45”

3. Write BASIC expressions for the following algebraic expressions.

i. a2+b2 ii. 25 (F-32)

iii. nr2+KT iv. P(1+r/100)n

v. ut +1/2gt2 vi. (2u+b)2t

vii. 2(x+y)/z viii. (9/5)c+32

ix. 2(l+b) x. 5L(2b+3)

JBD
188 Computer Studies-9

4. Correct the following expressions.
a. B$ = Kathmandu
b. A = 4Q + 5P
c. “Mount Everest”= D$
d. $$A = “Himalayas of Nepal”
e. 4BT = “Nepal”

5. Rewrite the given instructions in QBASIC.
a. The product of p, q and r is divided by 100.
b. m raised to the power 2 plus n raised to the power 3.
c. The sum of a and b is divided by the product of a and b.
d. Subtract 5 from m and the result is multiplied by 10.
e. The sum of p and q is multiplied by 2.
f. A is greater than or equal to B.
g. The sum of A and B is less than the product of A and B.
h. Twice the product of A plus thrice the product of B is greater than or
equal to 50.

3. Complete the table below by writing the value of each expression in the “Value” column.
Expression Value
(5 + 3) / 2
12 / (6 - 3)
(10+ 12) * (8 - 3)
(12 + 17) mod 5
(3 - 1) * (6 + 3) / 5
9+4/2
10+4/2*3+5
2+3^2*5

JBD
Computer Studies-9 189

Chapter 15

QBASIC
STATEMENTS

Objectives

After completing this chapter, you will be able to:
y Define statement and explain the difference between executable and non-execut-

able statements.
y Explain the function and syntax of LET, INPUT and READ...DATA statements.
y Explain the function and syntax of PRINT statement.
y Explain the function and syntax of TAB and SPC functions.
y Explain the function and syntax of LOCATE and SWAP statements.

C Ooncept verview

Every command or instruction in QBASIC is called a statement. The statement or
instruction should be given according to the rules of the language in which the
program is written. The QBASIC statements are first stored in the memory of the
computer and executed only when the command RUN is given. Each statement
is classified as executable or nonexecutable. Executable statements specify actions
and form an execution sequence in an executable program. Nonexecutable
statements specify characteristics, arrangement, and initial values of data; contain
editing information; specify statement functions; classify program units; and
specify entry points within subprograms. Nonexecutable statements are not part
of the execution sequence.
The statements available in BASIC can be divided into six functional groups: data
type definition, assignment and allocation, control, non-I/O and I/O, and colour
and graphics.

JBD
190 Computer Studies-9

Data Type Definition Statements Size Description
Suffix DEFtype Variable Type 2 bytes
% DEFINT Integer Declares
4 bytes variable(s) as
& DEFLNG Long Integer integer data type.
4 bytes
! DEFSNG Single Precision Declares
variable(s) as long
# DEFDBL Double Precision 8 bytes integer.

$ DEFSTR Variable length string 4 bytes Declares
variable(s) as
single precision
numeric data type.

Declares
variable(s) as
double precision
numeric data type.

Declares
variable(s) as
string data type.

Solved Examples

DEFSTR n
DEFINT c,s
INPUT “Enter a name”;n
INPUT “Enter total sales amount”;s
c = s * 10/100
PRINT “The commission is Rs.”; c
END

REM statement

The REM statement is a short form for ‘remarks’. The computer ignores anything
following the word REM and it is used to put explanatory notes in the program.
You may use a single quotation mark ( ‘ ) instead of REM. The general format of
REM statement is:
REM <remark>
OR
‘ <remark>

Where,

remark may be any series of characters

JBD
Computer Studies-9 191

CLS statement
The CLS statement is used to clear the screen and leaves the cursor at the top left
corner of the screen. The general format of CLS statement is:
CLS

LET statement
The LET is used for storing a value in a variable. The value can be an expression
or a literal and must be the same type as the variable. The general format of LET
statement is:
LET <variable> = <expression>
Where,
variable is the name of a numeric or string variable that is to receive the
value.
expression is a numeric or string expression whose value will be assigned to a
variable.
The QBASIC keyword LET is optional in the syntax; the equal sign is sufficient.
The value of the expression on the right is assigned to the variable name on the
left.

Solved Examples

CLS
LET Num1 = 20
LET Num2 = 30
LET Num3 = 5
Ans = (Num1+Num2)/Num3
PRINT “The answer is “; Ans
END

INPUT statement

The INPUT statement is used to receive input from the keyboard during program
execution. The general format of INPUT statement is:

INPUT [“definer”; | ,] list of variables

Where,

definer is a string constant, enclosed in quotation marks, that will be displayed
when the statement is executed.

list of variables contain the numeric or string variable that stores the data.

JBD
192 Computer Studies-9

Solved Examples
CLS
REM “To find perimeter of rectangle”
INPUT “Enter length of a rectangle”; l
INPUT “Enter width of a rectangle”; w
p = 2*(l + w)
PRINT “Area of the rectangle is:::”; p
END

LINE INPUT statement
The LINE INPUT statement is used to read an entire line (up to 254 characters),
regardless of delimiters, from the keyboard into a string variable. The general
format of INPUT statement is:
LINE INPUT [“prompt”;] variable
Where,
prompt is a string constant that will be displayed when the statement is executed.
variable is the name of a string variable or array element to which the line will be
assigned.

Solved Examples
CLS

LINE INPUT “Type a sentence:”; n$

PRINT n$

END

Program output with example input
Type a sentence:: Do not look where you fell, but where you slipped.
Do not look where you fell, but where you slipped.

READ DATA statement
The READ statement is used to read data from DATA statement into variables.
The purpose of DATA statement is to store the numeric and string constants that
are accessed by the program’s READ statement(s). The general format of INPUT
statement is:
READ variable1[,variable2] ...
...
...
DATA constant1 [,constant2]...

JBD
Computer Studies-9 193

Where,

variable is a numeric or string variable that is to receive the value read from the
DATA statement.

constant may be any type of numeric constant or a string constant.

Solved Examples

READ s, c
DATA 900, 200
profit = s - c
percent = (profit / c) * 100
PRINT “Actual profit =”; profit
PRINT “Profit percent=”; percent; “%”
END

PRINT statement
BASIC divides the line into print zones of 14 spaces. The position of each item
displayed on the screen is determined by the punctuation used to separate the
items in the list. The separator and print position is described below:

Separator Print Position
, Zone wise
; Side by side
space(s) Immediately after the
last value

Solved Examples
INPUT “Enter marks in subject1”; m1
INPUT “Enter marks in subject2”; m2
INPUT “Enter marks in subject3”; m3
total = m1 + m2 + m3
avg = total / 3
PRINT “The marks in subject1:”;m1
PRINT “The marks in subject2:”;m2
PRINT “The marks in subject3:”;m3
PRINT “The average score is:”;avg
END

JBD
194 Computer Studies-9

PRINT USING statement

The PRINT USING statement is used to format string or numeric output in a
specified manner. The general format of the PRINT USING statement is:

PRINT USING “string”; expression [, | ; expression]...[ , | ; ]

Where,

string is a string constant or variable that consists of special formatting characters.
It determines the field and format of the displayed string or number.

expression is the numeric or string expression to be displayed. Multiple
expressions must be separated by commas or semicolons.

String formatting characters

Character Result
! Displays only the first character of the string.
\ n spaces \ Displays 2 + n characters from the string.
& Displays string exactly

Solved Examples
CLS

A$ = “Tripti”

B$ = “Shrestha”

PRINT USING “!” ; A$;”.”;

PRINT USING “$” ; B$

END

Character Result
# Displays a digit.
+ Displays the sign of the number before or after the number.
- Displays a trailing minus sign.
** Fills leading spaces with asterisks.

$$ Displays a $ to the immediate left of the number.
**$ Fills leading spaces with asterisks and displays a $ to the left of
the number.
_ Underscores displays the next character as a literal character.
^^^^ Displays the number in exponential format.

JBD
Computer Studies-9 195

Solved Examples

number1 = 124.564
number2 = 16.82
quotient = number1 / number2
PRINT USING “#.#####”; quotient
PRINT USING “#.####”; quotient
PRINT USING “#.###”; quotient
PRINT USING “#.##”; quotient
PRINT USING “#.#”; quotient
PRINT USING “#”; quotient
END

LPRINT AND LPRINT USING statement
The LPRINT and LPRINT USING statements print data on the printer. The
statements are very similar to the PRINT and PRINT USING statements, except
that the output goes to the printer instead of the screen. The general format is:
LPRINT [ expression [ , | ; expression]...][;]
LPRINT USING “string” ; expression [, | ; expression ] ...[;]
Where,
expression is a numeric or string expression to be printed.
string is a string constant or variable that identifies the format in which expression
is to be printed.
LPRINT assumes that the printer uses an 80-character line. This means that
BASIC inserts a RETURN/line feed sequence
after 80 characters have been printed. Consequently, if you print a line that is
exactly 80 characters long, two lines will be skipped after the line unless you end
the LPRINT statement with a semicolon.

Solved Examples
CLS
LET x = 125
LET a$ = “Kiran”
LPRINT USING “\ \:###.##”; a$; x
END

JBD
196 Computer Studies-9

TAB function
The TAB function causes program output to shift to the right a specified number
of spaces. TAB is used only with PRINT and LPRINT statements. The general
format is:
TAB (n)
Where,
n is an integer expression in the range
0-32767. If n is negative, it produces the same result as TAB (1). If n is out of range,
an overflow error occurs.

Solved Examples

INPUT “Enter movie name”; n$
INPUT “Enter adult tickets sold”; a
INPUT “Enter child tickets sold”; c
gross = (a * 6) + (c * 3)
net = 20 / 100 * gross
amount = gross - net
PRINT “Movie Name:”; TAB(30); n$
PRINT “Adult Tickets Sold:”; TAB(30); a
PRINT “Child Tickets Sold:”; TAB(30); c
PRINT “Gross Box Office Profit:”; USING“$$####.##”; TAB(30); gross
PRINT “Net Box Office Profit:”; USING“$$###.##”; TAB(30); net
PRINT “Amount Paid to Movie Co.:”; USING “$$####.##”; TAB(30); amount
END

SPC function
The SPC function skips over the specified number of spaces in a PRINT or LPRINT
statement. The general format is:
SPC (n)
Where,
n is an integer expression in the range
0-32767.

Solved Examples

ar1$ = “Good”
var2$ = “Morning”
PRINT var1$, SPC (15);var2$
END

JBD
Computer Studies-9 197

LOCATE statement
The LOCATE statement is used to position the cursor on the screen and optionally
to define cursor attributes. It is commonly used with PRINT statements. The
general format is:
LOCATE[row][,[col][,[cursor][,[start][,stop]]]]
Where,
row is the screen line number, a numeric expression within the range of 1 to
25.
col is the screen column number, a numeric expression within the range of 1 to
40, or 1 to 80, depending upon screen width.
cursor is a boolean value indicating whether the cursor is visible;zero is off, non-
zero is on.
start is the cursor start scan line.
stop is the cursor stop scan line. A scan line is a line which defines the thickness
of the cursor.

Solved Examples

CLS

FOR i = 1 TO 5

x = 40 - i

LOCATE i, x

FOR j = 1 TO i

PRINT “* “; ‘ insert space after the asterisk

NEXT

NEXT

END

SWAP statement
The SWAP statement exchanges the contents of the two parameter variables. The
general format is:
SWAP variable1, variable2
Where,
variables may be of any type: integer, single precision, double precision and
string.

JBD
198 Computer Studies-9

C Dompu- ictionary

Constants : The data items that never change their value during a
program execution.

String constant : A set of alphanumeric or special characters enclosed
within double quotes.

Variables : A storage area whose contents can vary during
processing.

Symbolic constants : The constants that is used when a value is not expected
to change during the execution of the program and the
value is used more than once in the program.

Expression : A programming language statement that has a value.

Recap

• Every command or instruction in QBASIC is called a statement.

• Executable statements specify actions and form an execution sequence in an
executable program.

• Nonexecutable statements are not part of the execution sequence.

• DEF statements are used declare variable types as integer, single precision,
double precision, or string.

• The LET is used for storing a value in a variable.

• The INPUT statement is used to receive input from the keyboard during
program execution.

• The LINE INPUT statement is used to read an entire line (up to 254 characters),
regardless of delimiters, from the keyboard into a string variable.

• The READ statement is used to read data from DATA statement into variables.

• The purpose of DATA statement is to store the numeric and string constants
that are accessed by the program’s READ statement(s).

• The PRINT statement is used to display numeric or string data on the screen.

• The PRINT USING statement is used to format string or numeric output in a
specified manner.

• The LPRINT and LPRINT USING statements print data on the printer.

• The TAB function causes program output to shift to the right a specified
number of spaces.

JBD
Computer Studies-9 199

Review Yourself

1. Answer the following questions.
a. What is a statement? What is the difference between executable and non-
executable statement?
b. What is the function of DEF type statement in a program?
c. What is the difference between LET and READ...DATA statements in
QBASIC program?
d. What is the main difference between PRINT statement and PRINT
USING statement?
e. Write the function and syntax of the following statements:
i. LET
ii. INPUT
iii. READ...DATA
iv. PRINT
v. LOCATE

2. Find error, if any in the following LET statements.
a. LET salary = 350
b. LET pay, sal = 5000
c. LET name$ = “Suresh”
d. LET 3000 = salary
e. LET q = r(t+30)s

3. Write assignment statements to evaluate the following:
a. x = a + bc
b. a = nr2+KT
c. r = P(1+r/100)n
d. n = ut +1/2gt2
e. m = (u+r)2t
f. k = 2(x+y)/z
g. f = (9/5)c+32

JBD
200 Computer Studies-9


Click to View FlipBook Version