Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
43 views

CS121 Lecture Note

Uploaded by

Salmanu Dalhatu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

CS121 Lecture Note

Uploaded by

Salmanu Dalhatu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

ABUBAKAR TAFAWA BALEWA UNIVERSITY BAUCHI, BAUCHI STATE

FACULTY OF SCIENCE
MATHEMATICAL SCIENCE DEPARTMENT

CS 121

Introduction to Computer Science 11

Lecture note

Lecture note Page 1


CS121 – INTRODUCTION TO COMPUTER SCIENCE II

Overview of fundamental concept of Computer Science.Problem solving using computer;


Algorithm, pseudo code, flowchart.Programming, program control/logic
structure.Programming paradigms (Unstructured, structured and OOP).Definition of the
following terms: bits byte, word, word length, data, information, records, fields, files, and
database.

Basic Data Structures: Meaning of data structure. Brief discussion on Array, Linked lists,
stack, queues, tree: tree traversal, use of

Lecture note Page 2


1.0 INTRODUCTION

A computer is a programmable device machine that accepts data, stores and manipulates data,
and provides output in a useful format.

At the early stage of its development, computer was seen as just a mere tool to assist in
computation. However, computer has now grown to be a sophisticated tool that can be
programmed to do variety of tasks. In fact, it’ll be hard to think of any field or area of human
endeavour where computer is not employed and heavily relied upon.

Most of the computers that we see and used today are based on Von Neumann Architecture.
The basic idea behind the Von Neumann architecture is the concept of ‘stored program’.
That is, computer works under the control of some instructions called program/software
stored in its memory.The CPU fetch (read) both the data and the instructions from the main
memory, decode (interpret)the instructions and finally execute it. This constitute what is
called fetch-execute cycle, based on which the CPU speed measured.

Computer is organized as a system comprising of two major components: hardware and


software. It is these two components that work together to achieve the task of transforming
input data into meaningful information.

Our focus as students Computer Science is notonly limited toknowing the principles behind
how computer operate, but to be able to create and analyse computational procedures that
when implemented will cause computer to carryout meaningful tasks. The fields of Computer
Science and Software Engineering primarily focus on the design and implementation of
software

2.0 PROBLEM SOLVING

The goalComputer Scientist is to solve problems using computers.The problems that


Computer Scientist solves areusually real-world problem involving some computations – eg,
students’ results computation, payroll computation etc. Problems are formulated as
computational processes called algorithms.Recall that computer is a programmable, which
means it follow instructions or computational steps to accomplish any task. Computer
scientist create, study, analyse those computational steps with theultimate view of
implementation on the machine. In fact this is the reason why Computer Science is
sometimes regarded as the study of algorithms.

There are many definitions for “problem solving”. Here is one: Problem Solvingis the process
of analysing a given situation and generating appropriate response or solution. In the context
Computer Science, problem solving can be defined as the process of analysinga given
problem and producing its solution in form of algorithm that can be implemented using a
programming language on the computer.

Lecture note Page 3


In essence, problem solving using computer is all about producing/developingsolution to a
problem in form of program that can be executed on a computer. There is need to have a
standard and systematic approach to solving problems.

Program is a sequence of instructions that can be executed by a computer to solve


some problem or perform a specified task.

Producing/developing program is a systematic task. Some steps needs to be followed. If you


opt to do it any how, you’d certainly get rubbish at the end!

2.1 Problem Solving Steps

The following are problem solving steps are expected to be adhere strictly. As you progress
in studying Computer Science, you would see that it is these steps that mutate into what is
called Software Development Life Cycle (SDLC), or Software process models such as Water
fall model, V-model, Agile etc

i. Understand the Problem


ii. Problem analysis & specification

Problem analysis deals with determining how the solution is going to be by


identifying:
 Inputs (the data to be processed),
 Output (the desired result; in what format is it required), and
 Any additional constraint on the solution.
Having known the input and the kind of output needed, it is hope that you will gain an
idea of the approach to use in solving the problem.
Broadly speaking, program specification defines the input, the processing and the
output required. A good specification specifies what processing is needed by given
the exact relationship between input and output rather than prescribing how the
program should be written
iii. Program design& algorithm development
Design involves breaking the problem into small manageable units/modules, establish
relationship between them, and then develop precise steps for accomplishing the tasks
in the units identified. Therefore, at the end of this step/phase, computational steps or
algorithm is expected to be produced.
iv. Coding
Coding is the task of transforming the algorithm into an executable computer
program. At this step, you have to choose a programming language. This is where the
knowledge of programming language comes in.
v. Testing

Lecture note Page 4


Is the process of running program with different set of input with the intension of
revealing error.The essence of running it with different sets of data is to test whether it
produces the expected result and ultimately meet its specification or not.
When a program is run, if all is well, you should see correct output. However, it is
possible for the program to produce wrong results (output) or failed to run at all. In
that case, it is possible that the algorithm is not properly converted into a proper
program. Therefore, the program has bugs or errors. Thus, the program must be
debugged.
Debugging is the process of finding and fixing errors or bugs in programs. It is often
a very time – consuming task, since the programmer must read the code line by line in
an attempt to uncover error.
During testing, the programmer will be looking for both syntax errors and logic
errors, as well as exploring other section that may cause the program to either not
work properly or not run at all.
vi. Document the program
Documentation involves writing explanations of the problem being solved and the
organization of the solution, comments embedded within the program itself, and user
manuals that describe how to use the program. Documentation is a task that should be
done right from the first stage of program development. Many different people are
likely to work on a program over a long period of time. Good documentation will
simplify the work for them.
vii. Maintenance
Once a program has been put into use, it is often necessary to modify it. Modification
may involve fixing an error that didn’t show up during development but appear during
usage, or changing the program in response to changes in the user’s requirements.
Each time the program is modified, it is necessary to repeat the problem-solving steps.
This phase of the programming process is known as maintenance and actually
accounts for the majority of the effort expended on most programs.

2.2 Algorithm

An algorithm is a precise and unambiguous sequence of instructions for solving a problem.


Algorithm can also be defined as a finite and unambiguous set of procedures to accomplish a
task. The term algorithm applies to any method of solving a problem, and it is not restricted
to the field computer science alone. When we write computer program, we are implementing
algorithm.

When you start your car, you follow a step-by-step procedure – i.e, the algorithm might look
something like this:

Lecture note Page 5


1. Insert the key.
2. Make sure the transmission is in Park (or Neutral).
3. Turn the key to the start position.
4. If the engine starts within six seconds, release the key to the ignition position.
5. If the engine doesn’t start in six seconds, release the key and gas pedal, wait ten
seconds, and repeat Steps 3 through 5, but not more than five times.
6. If the car doesn’t start, call the garage.

Attributes of Algorithm

i. Precision – algorithm must be clear and give correct solution in all case.
ii. Unambiguity – ambiguity in algorithm means having more than one step performing
the same task. An algorithm is unambiguous if it doesn’t have series of steps
performing the same task.
iii. Finite – an algorithm should not be run forever. It must have an end.
iv. Efficiency – efficiency of algorithm has to do with how much of various types of
resources it consumes. For example, processing time, memory. Some algorithms
consume much memory and time when executed. In computer science, we are
interested in algorithms that are time and space (memory) efficient.

As far as problem solving using computer is concern, the aim is to implement algorithm using
a programming language. To have a better understanding of the steps in algorithm so as to
make implementation easier, algorithm can be represented in two forms (1) Pseudocode (2)
flowchart.

2.3 Pseudo code

Pseudo code is used as a way of describing a computer program without completely


following the syntax of programming language. Alternatively, a pseudo code can be seen as
an informal set of instructions that imitate the structure of a program but does not adhere
completely to the syntax of programming language.

Lecture note Page 6


When learning to program, it is important to write pseudocode because it helps you clearly
understand the problem that you are trying to solve. It also helps you avoid getting bogged
down with syntax details (i.e., like spelling mistakes) when you write your program.

Although flowcharts can be visually appealing, pseudocode is often the preferred choicefor
algorithm development because of the following reasons:

i. Pseudocode fits more easily on a page of paper.


ii. Pseudocodeis written in a way that is very close to real program code, thus making it
easier when writing the program (coding).
iii. Pseudocode takes less time to write than drawing a flowchart.
iv. Pseudocode easily accommodate changes/alteration than flowchart which is difficult
to be redrawn when mistakes are made.

Pseudocode will vary according to whoever writes it. That is, one person’s pseudocodeis
often quite different from that of another person. However, there are some commoncontrol
structures (i.e., features) that appear whenever we write pseudocode.

Control structures Description Example


Sequence Sequence of executable age = 20
statements without branching input gradelevel
or loops pay = neyPay + bonus
print pay
Decision Selecting one option from if lamp is not plugged in
several other options. then plug it in
Example, Doing taskA or ………OR…………..
taskB depending on if bulb is burned out
condition. then replace bulb
otherwise buy new lamp
Repetition/loop Repeatedly doing some tasks repeat
several times until a get a new light bulb
condition is satisfied. . put it in the lamp
until lamp works or no more
bulbs left

Lecture note Page 7


The above are the control structures that are used to control flow of control in computer
programs. There every programming language its own syntax of these control structures.

The point is that there are a variety of ways to write pseudocode. The important thing to
remember is that your algorithm should be clearly explained with no ambiguity as to what
order your steps are performed in

2.4 Flowchart

A flowchart is used to represent algorithm ina diagrammatic form. Flow chart uses symbols
to show the operations and decisions to be followed by a computer in solving problem. A
flowchart is a convenient technique to show the flow of control in a program.Flowchart is one
of the programming tools that help programmers understand algorithm better

In fact, flowcharts are the plan to be followed when the program is written. Expert
programmers may write programs without drawing the flowcharts. But for a beginner it is
recommended that a flowchart should be drawn before writing a program. This reduces the
number of errors and omissions that might occur during coding/program creation. The
flowchart symbols are as follows:

Symbol Symbol name Meaning


Start/Stop symbol . Denotes the beginning and end of a
program. There must be two set of
this symbol in your flowchart
Flow line This symbol is used to indicate the
direction of the flow of control in a
flowchart. This means the flow lines
indicate the next statement to be
executed.
Input/output symbol It is used to denote entering data
through keyboard or Displaying
output.
Processing symbol It is used to denote processing
activity to be carried out such as

Lecture note Page 8


assignment and data-movement
instructions. Therefore, all arithmetic
processing of adding, subtracting,
multiplying, and dividing are
represented with a processing
symbol.
Decision symbol It is used to denote a decision.This
symbol must be used whenever
decision has to be taken. Decision
symbol has one entry point and at
least two exit points (paths)
depending upon the decision taken
inside the symbol. When a condition
is tested, if the condition is true, the
path marked “true or yes” is
followed. If the condition is false, the
path for “false or no” is followed.
Connector symbol It is used to connect one part of the
flowchart to another. If a flowchart is
too big to be accommodated on a
page, a connector symbol can be used
to indicate the continuation in another
page. It is a circle with a number
written inside it. If a flowchart is
discontinued at some point, a circle is
drawn pointing away from the chart.
Another circle with the same number
inside is placed where the flowchart
is continued

Rules/Guidelines for drawing flowchart

Lecture note Page 9


The following are some guidelines or rules that are very important when drawing flowcharts

 Only conventional flowchart symbols should be used.


 Arrows can be used to indicate the flow of control in the problem. However, flow
lines should not cross each other.
 Processing logic should flow from top to bottom and from left to right.
 Words in the flowchart symbols should be common statements and easy to
understand. These should be independent of programming languages.
 Be consistent in using names and variables in the flowchart.
 If the flowchart becomes large and complex then connector symbols should be used to
avoid crossing of flow lines.
 Properly labelled connectors should be used to link the portions of the flowchart on
different pages.
 Flowcharts should have start and stop points.

Advantages of Flowchart

i. Flowcharts guide or help programmer in writing the actual code in a high level
language.
ii. A flowchart is an excellent communication technique that explains the logic of a
program to other programmers/people.
iii. A flowchart is an important tool in the hands of a programmer, which helps him in
designing the test data for systematic testing of programs
iv. Flowcharts are used as working models in designing new programs and software
systems

Disadvantages

Some of the disadvantages of flowchart include:

i. The drawing of flowcharts is a very time-consuming process and laborious especially


for large and complex problems.

Lecture note Page 10


ii. It is very difficult to include any new step in the existing flowchart; redrawing of the
flowchart is the only solution and it is even more difficult and time consuming.
iii. If an algorithm has complex branches and loops, flowcharts become very difficult to
draw.

Whether using a flow chart of pseudocode, you should test your algorithm by manually going
through the steps in your head to make sure that you did not forget a step or a special
situation. Often, you will find a flaw in your algorithm because you forgot about a special
situation that could arise. Only when you are convinced that your algorithm will solve your
problem, should you go ahead to the next step.

Example 1

Write an algorithm/pseudocode to calculate the area of rectangle. Represent the algorithm in


a flowchart.

Problem analysis

This problem requires the length and breadth of the rectangle to be specified. Hence it
requires input. The length and breadth can be integer or floating point numbers. So the output
(area of the rectangle)should be floating point number (to generalize the case).

Algorithm Pseudo code


1. Start Start
2. Print “please enter the length and breadth of the Print “please enter the length, and breadth”

rectangle ” length = number entered by user


breadth = number entered by user
3. input a number and assign it to a variable length
IFlength >0 and breadth >0THEN
4. input a number and assign it to a variable breadth
area = length * breadth
5. Iflength is greater than 0 and breadth is greater than 0
Print area.
then do step 7, 8 and 9, ELSEPrint “invalid input” and
ELSE
gotostep 2.
Print “invalid input” and goto step 2.
6. Multiply length by breadth and assign the result to a endIF
variable area Stop
7. Print area.
8. Stop

Lecture note Page 11


start

print“enter length”
inputlength
print“enter breadth”
inputbreadth

N print“invalid input. Please


length> 0 and try again”
breadth>0

area = length * breadth

print area

Example 2 stop

Write an algorithm/pseudocode to find the average of positive integers from 1 to 20. (ie,
1,2,3,4,….., 20). Represent the algorithm in aflowchart.

Problem analysis

This problem does not require any input.All that is needed is to have a variable initialized to 1
and then continuously incrementthe variableuntil it reaches 20 while accumulating the
sum.Theoutput should be a floating point number.
Algorithm
1. Start
2. Declare a variable n and initialize it to 1(ie, n = 1)
3. Declare a variable sum and initialize it to 0 (ie, sum = 0)
4. Repeat steps 5 and 6 while n is less than or equal to 20, otherwise goto step 7
5. Add n to sum (sum = sum + n)
6. Increment n by 1 (ie, add 1 to n so that n will point to the next integer in the sequence)
7. Divide sum by 20 and assign the result to average.
8. Print average
9. Stop

Lecture note Page 12


Pseudo code start

start
n=1 n=1
sum = 0
sum = 0
average
REPEATwhile n ≤ 20
sum = sum + n N
n = n+ 1 n<= 20 Y
endREPEAT
average = sum / 20
printaverage
sum= sum + n
stop
n=n+1

average= sum / 20

print average

stop
Example 4

Write an algorithm/pseudocode to count even numbers from 1 to 500

Problem analysis - This problem does not require any input. The output should be an integer
number. We first need to know what is an even number?

EvenNumberCounter = 0
Y
Number = 1
REPEAT while Number <= 1000
Remainder = Number % 2.
IF Remainder == 0THEN
EvenNumberCounter = EvenNumberCounter + 1
Number = Number + 1
ELSE
Number = Number + 1
endIF
End REPEAT
Print EvenNumberCounter
Exit.
Lecture note Page 13
Example 5: Write an algorithm that read an integer value and determine whether the integer
is even or odd.
Start
inputnumber
IF numberis integer THEN
remainder = number% 2
IF remainder == 0 THEN
Print “the number is even”
ELSE
Print “the number is odd”
endIF
ELSE
Print “please enter an integer value”
Goto step 2
endIF
Stop

start
start

EvenNumberCounter = 0
input number
Number = 1

numberin
Print ‘number is not an no Number ≤ no
t?
integer’ 1000

yes yes
rem = number%2
Remainder = Number % 2

no rem == yes no Remainder


0 Number ++ == 0

Print ‘the number is an Print ‘the number is an


yes
odd number’ even number’
EvenNumberCounter ++
Number ++

stop
Print
EvenNumberCounter

Lecture note Page 14

stop
Flowchart to count even numbers from 1 to 1000

Flowchart to check whether a number is even or odd

So where do algorithm come from? Usually they have to be developed often with a lot of
thought and hard work. Skill in algorithm development is something that comes with
practice, but there are techniques and guidelines that help.

EXERCISES

1. Write an algorithm or pseudo code that display “Hello world” ten times, and represent
the algorithm in a flowchart.

2. Given the following algorithm, draw its flowchart

1. Start
2. Print “please enter your age”
3. Input a number and assign it to variable age
4. IF age >= 18 THEN
Print “You are qualified to vote.”
5. ELSE
Print “You are not qualified to vote”
endIF
6. Stop

3. Given the following algorithm

1. Start
2. Declare a variable N and assign 1 to it (ie, N=1).
3. While N is less than or equal to 10, do steps 4 and 5 and return to step 3, else go
to step 6
4. Print N
5. Increment N by 1(ie, N = N +1)
6. Stop

(a) Explain what the algorithm does.


(b) Draw the flowchart for the algorithm.

Lecture note Page 15


3.0 Programming Paradigms

Different approaches to programming have developed over time. Notable, an early approach
is structured programming which was advocated since the mid-1960s. The concept of a
"programming paradigm" as such dates at least to 1978.

In Computer Science, programming paradigms can be seen as a pattern that serves as a school
of thoughts for programming of computers Programming paradigms are ways to classify
programming languages based on their features [Wikipedia]. Each paradigm supports a set of
concepts that makes it the best for a certain kind of problem.

Just as software engineering (as a process) is defined by differing methodologies, so the


programming languages (as models of computation) are defined by differing paradigms.
Some languages are designed to support one paradigm (Smalltalk supports object-oriented
programming, Haskell supports functional programming), while other programming
languages support multiple paradigms (such as Object Pascal, C++, Java, C#, Scala, Visual
Basic, Common Lisp, Scheme, Perl, PHP, Python, Ruby, and F#). For example, programs
written in C++, Object Pascal or PHP can be purely procedural, purely object-oriented, or can
contain elements of both or other paradigms. Many programming paradigms are as well-
known for the techniques they forbid as for those they enable. For instance, pure functional
programming disallows use of side-effects, while structured programming disallows use of
the goto statement [Wikipedia].

The concept of Structured Programming

The major programming paradigms are:

i. Procedural/imperative paradigm
ii. Functional paradigm
iii. Logic paradigm
iv. Object Oriented paradigm

Procedural programming paradigm

Imperative programming paradigm describes computation in terms of statements that change


the program state. The program state means the values stored in variables in the memory. The
program is built from one or more procedures or subroutine or functions. The fundamental
feature of programming languages in this category is the explicit variable declarations and
assignment on those variables. Examples are Fortran, BASIC, Pascal, C. The following code
is section of C program which uses loop construct to modify the variables x and sum.
int x=1, sum= 0;
while (x<=10){
sum = sum + x;
x = x+1;
}
printf(“Sum is %d”, sum);

Lecture note Page 16


Functional Programming paradigm

In functional programming, programs are treated as a sequence of stateless function


evaluations. A purely functional programming language does not use variables or assignment
statements, thus freeing the programmer from concerns related to the memory cells, or state,
of the program. In this paradigm, a program is thought of as a sequence of stateless function
evaluations. All computation in a purely functional program is accomplished by applying
arguments to functions. Neither the assignment statements nor the variables that abound in
imperative language programs are necessary in functional language programs. Furthermore,
repetitive processes can be specified with recursive function calls, making iteration (loops)
unnecessary. The fundamental characteristic of the languages in this paradigm is precisely
that of not possessing the concept of memory.Example of functional programming languages
are: LISP, Scheme, Haskel.

Logic Programming paradigm

Logic programming is rule-based. The program consists of axioms and inference rules. Logic
programming is the use of a formal logic notation to communicate computational processes
to a computer. Predicate calculus is the notation used in current logic programming
languages.Logic programs are declarative rather than procedural, which means that only the
specifications of the desired results are stated rather than detailed procedures for producing
them.Logic programming paradigm fits well to problem domains that deal with the extraction
of knowledge from basic facts and relations. Program execution becomes a systematic search
in asset of facts, making use of a set of inference rules. Example is Prolog.

Object Oriented Programming (OOP)paradigm

In object-oriented programming, programs are treated as a set of interacting objects.The


program is composed with collection of interaction objects. Program is organised into some
units called classes. The class houses the procedures or methods that will perform the
computation. An object is nothing but an instance of a class. Today, the most significant
software design methodologies and programming languages are object oriented.Example
object oriented programming languages include Smalltalk, C++, Ruby, Java, PHP etc. The
following is an example of OOP program

class A { class B extends A {


int x; ……
intgetX(){ …….
return x; …….
} }
}

Lecture note Page 17


USEFUL TERMS

Bit – stands forbinary digit (ie, 0 or 1). Bit is the basic unit of information in digital
computers. Data in digital computers are represented as sequence of bits (0s and 1s) which
correspond of electrical states of off or on OR high or low. When a character is pressed on
keyboard, the computer internally recognizes the character as sequence of bits.Electrical
signals such as voltage exist throughout the computer in either one of two recognizable
states(0 or 1). The manipulation of binary information is done by logic circuits called gates.
Gates are blocks of hardware that produce signals of binary 1 or 0 when input logic
requirements are satisfied. A variety of logic gates are used in digital systems. Each gate has
distinct graphic symbol and its operation can be described by means of algebraic expression.

Byte – is a group of eight bits.Manufacturers of computer storage devices (such as Memory


cards, Flash drives, RAM and Hard Disk Drive) express the capacity of such devices in terms
of the number of bytes they can hold. Today, even the bandwidth for internet connectivity is
measured in bytes hence the need to understand the following:

 1 Byte = 8 bits
 1KB = 210bytes = 1024 bytes i.e, thousands of bytes
 1MB = 210 KB = 1024KB = (1024 × 1024 bytes) i.e, millions of bytes
 1GB = 1024MBi.e, billions of bytes
 1TB = 1024GB

The memory of 640KB has 655,360 bytes.

640KB = 640x1024 = 655,360 bytes

A RAM whose capacity is 256MB roughly has 256 million bytes.

256MB = 256 × 1024 = 262144 KB

262144 KB = 262144 × 1024 = 268,435,456 bytes

Exercise: Work out the number of bytes of a memory card whose capacity is 1.5GB.

Word– is a sequence of bits of a particular size or length. Words can be of 16 bits, 32 bits, 64
bits, or any other size that makes sense within the context of a computer’s organization. [A
computer whose word size is 64 bits, typically called 64 bit machine, is capable of fetching
64 bit of data/instruction from main memory].Operating systems (OS), antivirus and other
application software are designed based on the word length of microprocessor. 32bits OS can
be installed on 64bits machine, but 64bits OS can never be installed on 32 bits machine.

File –

Database – Database is an organized collection of related records. Alternatively, Database


is a collection of related data organised in a way that permit easy access, and
management.Database is like a repository that houses data in an organized fashion so that
addition, update (editing) retrieval, and deletion can be done in an efficient way. Databases

Lecture note Page 18


are used to support internal operations of organizations such as Banks, Universities, Telecom
companies, Government agencies etc. Managing database require efforts of expert called
Database Administrator. Knowledge of Database is very important becausewe often write
programs that interact with one or more databases.

Database usually comes as part of application software called DBMS (Database Management
System). A BDMS is software that consists of integrated set of computer programs that allow
users to interact with one or more databases and provides access to the data contained in the
database. A DBMS is a generalized software system for manipulating databases. A DBMS
supports a logical view (schema, subschema); physical view (access methods, data
clustering); data definition language; data manipulation language; and important utilities,
such as transaction management and concurrency control, data integrity, crash recovery, and
security. Examples DBMS are, MySql, Microsoft Access, Microsoft SQL server, and IBM
Dbase II.

DATA STRUCTURES
Data structure is a particular way of organizing data in a computer so that it can be used
efficiently. Data structures can implement one or more particular abstract data types (ADT),
which specify the operations that can be performed on a data structure and the computational
complexity of those operations. In comparison, a data structure is a concrete implementation
of the specification provided by an ADT.
Different kinds of data structures are suited to different kinds of applications, and some are
highly specialized to specific tasks. For example, relational databases commonly use B-tree
indexes for data retrieval, while compiler implementations usually use hash tables to look up
identifiers.
Data structures provide a means to manage large amounts of data efficiently for uses such as
large databases and internet indexing services. Usually, efficient data structures are key to
designing efficient algorithms. Some formal design methods and programming languages
emphasize data structures, rather than algorithms, as the key organizing factor in software
design. Data structures can be used to organize the storage and retrieval of information stored
in both main memory and secondary memory.
There are numerous types of data structures, generally built upon simpler primitive data
types:
 An array is a number of elements in a specific order, typically all of the same type.
Elements are accessed using an integer index to specify which element is required
(Depending on the language, individual elements may either all be forced to be the
same type, or may be of almost any type). Typical implementations allocate
contiguous memory words for the elements of arrays (but this is not always a
necessity). Arrays may be fixed-length or resizable.
 A linked list (also just called list) is a linear collection of data elements of any type,
called nodes, where each node has itself a value, and points to the next node in the
linked list. The principal advantage of a linked list over an array, is that values can

Lecture note Page 19


always be efficiently inserted and removed without relocating the rest of the list.
Certain other operations, such as random access to a certain element, are however
slower on lists than on arrays.
 A record (also called tuple or struct) is an aggregate data structure. A record is a
value that contains other values, typically in fixed number and sequence and typically
indexed by names. The elements of records are usually called fields or members.
 A union is a data structure that specifies which of a number of permitted primitive
types may be stored in its instances, e.g. float or long integer. Contrast with a record,
which could be defined to contain a float and an integer; whereas in a union, there is
only one value at a time. Enough space is allocated to contain the widest member
datatype.
 A tagged union (also called variant, variant record, discriminated union, or disjoint
union) contains an additional field indicating its current type, for enhanced type
safety.
 A class is a data structure that contains data fields, like a record, as well as various
methods which operate on the contents of the record. In the context of object-oriented
programming, records are known as plain old data structures to distinguish them from
classes.

ARRAY
An array data structure, or simply an array, is a data structure consisting of a collection of
elements (values or variables), each identified by at least one array index or key. An array is
stored so that the position of each element can be computed from its index tuple by a
mathematical formula. The simplest type of data structure is a linear array, also called one-
dimensional array.
The memory address of the first element of an array is called first address or foundation
address.
The term array is often used to mean array data type, a kind of data type provided by most
high-level programming languages that consists of a collection of values or variables that can
be selected by one or more indices computed at run-time. Array types are often implemented
by array structures; however, in some languages they may be implemented by hash tables,
linked lists, search trees, or other data structures.
Arrays are used to implement mathematical vectors and matrices, as well as other kinds of
rectangular tables. Many databases, small and large, consist of (or include) one-dimensional
arrays whose elements are records.
When data objects are stored in an array, individual objects are selected by an index that is
usually a non-negative scalar integer. Indexes are also called subscripts. An index maps the
array value to a stored object.
There are three ways in which the elements of an array can be indexed:
 0 (zero-based indexing): The first element of the array is indexed by subscript of 0.

Lecture note Page 20


 1 (one-based indexing): The first element of the array is indexed by subscript of 1.
 n (n-based indexing): The base index of an array can be freely chosen. Usually
programming languages allowing n-based indexing also allow negative index values
and other scalar data types like enumerations, or characters may be used as an array
index.
Arrays can have multiple dimensions, thus it is not uncommon to access an array using
multiple indices. For example, a two-dimensional array A with three rows and four columns
might provide access to the element at the 2nd row and 4th column by the expression A[1, 3]
in the case of a zero-based indexing system. Thus two indices are used for a two-dimensional
array, three for a three-dimensional array, and n for an n-dimensional array.
A one-dimensional array (or single dimension array) is a type of linear array. Accessing its
elements involves a single subscript which can either represent a row or column index.
As an example consider the C declaration int anArrayName[10];
Syntax : datatype anArrayname[sizeofArray];
In the given example the array can contain 10 elements of any value available to the int type.
In C, the array element indices are 0-9 inclusive in this case. For example, the expressions
anArrayName[0] and anArrayName[9] are the first and last elements respectively.
For a vector with linear addressing, the element with index i is located at the address B + c ×
i, where B is a fixed base address and c a fixed constant, sometimes called the address
increment or stride.

LINKED LIST
a linked list is a linear collection of data elements, in which linear order is not given by their
physical placement in memory. Each pointing to the next node by means of a pointer. It is a
data structure consisting of a group of nodes which together represent a sequence. Under the
simplest form, each node is composed of data and a reference (in other words, a link) to the
next node in the sequence. This structure allows for efficient insertion or removal of elements
from any position in the sequence during iteration. More complex variants add additional
links, allowing efficient insertion or removal from arbitrary element references.

A linked list whose nodes contain two fields: an integer value and a link to the next node. The
last node is linked to a terminator used to signify the end of the list.
The principal benefit of a linked list over a conventional array is that the list elements can
easily be inserted or removed without reallocation or reorganization of the entire structure
because the data items need not be stored contiguously in memory or on disk, while an array
has to be declared in the source code, before compiling and running the program. Linked lists
allow insertion and removal of nodes at any point in the list, and can do so with a constant
number of operations if the link previous to the link being added or removed is maintained
during list traversal.

Lecture note Page 21


On the other hand, simple linked lists by themselves do not allow random access to the data,
or any form of efficient indexing. Thus, many basic operations — such as obtaining the last
node of the list (assuming that the last node is not maintained as separate node reference in
the list structure), or finding a node that contains a given datum, or locating the place where a
new node should be inserted — may require sequential scanning of most or all of the list
elements. The advantages and disadvantages of using linked lists are given below.

Advantages

 Linked lists are a dynamic data structure, which can grow and be pruned, allocating
and deallocating memory while the program is running.
 Insertion and deletion node operations are easily implemented in a linked list.
 Dynamic data structures such as stacks and queues can be implemented using a linked
list.
 There is no need to define an initial size for a linked list.
 Items can be added or removed from the middle of list.
 Backtracking is possible in two way linked list.

Disadvantages
 They use more memory than arrays because of the storage used by their pointers.
 Nodes in a linked list must be read in order from the beginning as linked lists are
inherently sequential access.
 Nodes are stored incontiguously, greatly increasing the time required to access
individual elements within the list, especially with a CPU cache.
 Difficulties arise in linked lists when it comes to reverse traversing. For instance,
singly linked lists are cumbersome to navigate backwards and while doubly linked
lists are somewhat easier to read, memory is consumed in allocating space for a back-
pointer.

Basic concepts and nomenclature

Each record of a linked list is often called an 'element' or 'node'.

The field of each node that contains the address of the next node is usually called the 'next
link' or 'next pointer'. The remaining fields are known as the 'data', 'information', 'value',
'cargo', or 'payload' fields.

The 'head' of a list is its first node. The 'tail' of a list may refer either to the rest of the list after
the head, or to the last node in the list. In Lisp and some derived languages, the next node
may be called the 'cdr' (pronounced could-er) of the list, while the payload of the head node
may be called the 'car'.

Singly linked list

Singly linked lists contain nodes which have a data field as well as a 'next' field, which points
to the next node in line of nodes. Operations that can be performed on singly linked lists
include insertion, deletion and traversal.

Lecture note Page 22


A singly linked list whose nodes contain two fields: an integer value and a link to the next
node

Doubly linked list

In a 'doubly linked list', each node contains, besides the next-node link, a second link field
pointing to the 'previous' node in the sequence. The two links may be called 'forward('s') and
'backwards', or 'next' and 'prev'('previous').

A doubly linked list whose nodes contain three fields: an integer value, the link forward to the
next node, and the link backward to the previous node

A technique known as XOR-linking allows a doubly linked list to be implemented using a


single link field in each node. However, this technique requires the ability to do bit operations
on addresses, and therefore may not be available in some high-level languages.

Many modern operating systems use doubly linked lists to maintain references to active
processes, threads, and other dynamic objects. A common strategy for rootkits to evade
detection is to unlink themselves from these list.

Multiply linked list

In a 'multiply linked list', each node contains two or more link fields, each field being used to
connect the same set of data records in a different order (e.g., by name, by department, by
date of birth, etc.). While doubly linked lists can be seen as special cases of multiply linked
list, the fact that the two orders are opposite to each other leads to simpler and more efficient
algorithms, so they are usually treated as a separate case.

Circular Linked list

In the last node of a list, the link field often contains a null reference, a special value used to
indicate the lack of further nodes. A less common convention is to make it point to the first
node of the list; in that case the list is said to be 'circular' or 'circularly linked'; otherwise it is
said to be 'open' or 'linear'.

A circular linked list

Lecture note Page 23


In the case of a circular doubly linked list, the only change that occurs is that the end, or
"tail", of the said list is linked back to the front, or "head", of the list and vice versa.

Sentinel nodes

In some implementations an extra 'sentinel' or 'dummy' node may be added before the first
data record or after the last one. This convention simplifies and accelerates some list-handling
algorithms, by ensuring that all links can be safely dereferenced and that every list (even one
that contains no data elements) always has a "first" and "last" node.

Empty lists

An empty list is a list that contains no data records. This is usually the same as saying that it
has zero nodes. If sentinel nodes are being used, the list is usually said to be empty when it
has only sentinel nodes.

Hash linking

The link fields need not be physically part of the nodes. If the data records are stored in an
array and referenced by their indices, the link field may be stored in a separate array with the
same indices as the data records.

List handles

Since a reference to the first node gives access to the whole list, that reference is often called
the 'address', 'pointer', or 'handle' of the list. Algorithms that manipulate linked lists usually
get such handles to the input lists and return the handles to the resulting lists. In fact, in the
context of such algorithms, the word "list" often means "list handle". In some situations,
however, it may be convenient to refer to a list by a handle that consists of two links, pointing
to its first and last nodes.

Combining alternatives

The alternatives listed above may be arbitrarily combined in almost every way, so one may
have circular doubly linked lists without sentinels, circular singly linked lists with sentinels,
etc.

STACK

A stack is an abstract data type that serves as a collection of elements, with two principal
operations: push, which adds an element to the collection, and pop, which removes the most
recently added element that was not yet removed. The order in which elements come off a

Lecture note Page 24


stack gives rise to its alternative name, LIFO (for last in, first out). Additionally, a peek
operation may give access to the top without modifying the stack.

Example of stack

The name "stack" for this type of structure comes from the analogy to a set of physical items
stacked on top of each other, which makes it easy to take an item off the top of the stack,
while getting to an item deeper in the stack may require taking off multiple other items first.

Considered as a linear data structure, or more abstractly a sequential collection, the push and
pop operations occur only at one end of the structure, referred to as the top of the stack. This
makes it possible to implement a stack as a singly linked list and a pointer to the top element.

A stack may be implemented to have a bounded capacity. If the stack is full and does not
contain enough space to accept an entity to be pushed, the stack is then considered to be in an
overflow state. The pop operation removes an item from the top of the stack.
The push operation adds an element and increments the top index, after checking for
overflow:

procedure push(stk : stack, x : item):


if stk.top = stk.maxsize:
report overflow error
else:
stk.items[stk.top] ← x
stk.top ← stk.top + 1

Similarly, pop decrements the top index after checking for underflow, and returns the item
that was previously the top one:

procedure pop(stk : stack):


if stk.top = 0:
report underflow error
else:
stk.top ← stk.top − 1
r ← stk.items[stk.top]

Lecture note Page 25


Applications of Stack
 Expression evaluation and syntax parsing
 Backtracking
 Compile time memory management
 Efficient algorithms

QUEUE
A queue is a particular kind of abstract data type or collection in which the entities in the
collection are kept in order and the principle (or only) operations on the collection are the
addition of entities to the rear terminal position, known as enqueue, and removal of entities
from the front terminal position, known as dequeue. This makes the queue a First-In-First-
Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will
be the first one to be removed. This is equivalent to the requirement that once a new element
is added, all elements that were added before have to be removed before the new element can
be removed. Often a peek or front operation is also entered, returning the value of the front
element without dequeuing it. A queue is an example of a linear data structure, or more
abstractly a sequential collection.

Queues provide services in computer science, transport, and operations research where
various entities such as data, objects, persons, or events are stored and held to be processed
later. In these contexts, the queue performs the function of a buffer.

TREE
A tree structure or tree diagram is a way of representing the hierarchical nature of a
structure in a graphical form. It is named a "tree structure" because the classic representation
resembles a tree, even though the chart is generally upside down compared to an actual tree,
with the "root" at the top and the "leaves" at the bottom.
A tree structure is conceptual, and appears in several forms. For a discussion of tree
structures in specific fields, see Tree (data structure) for computer science: insofar as it
relates to graph theory, see tree (graph theory), or also tree (set theory).

Terminology and properties


The tree elements are called "nodes". The lines connecting elements are called "branches".
Nodes without children are called leaf nodes, "end-nodes", or "leaves".
Every finite tree structure has a member that has no superior. This member is called the
"root" or root node. The root is the starting node. But the converse is not true: infinite tree
structures may or may not have a root node.
The names of relationships between nodes model the kinship terminology of family relations.
The gender-neutral names "parent" and "child" have largely displaced the older "father" and

Lecture note Page 26


"son" terminology, although the term "uncle" is still used for other nodes at the same level as
the parent.
 A node's "parent" is a node one step higher in the hierarchy (i.e. closer to the root
node) and lying on the same branch.
 "Sibling" ("brother" or "sister") nodes share the same parent node.
 A node's "uncles" are siblings of that node's parent.
 A node that is connected to all lower-level nodes is called an "ancestor". The
connected lower-level nodes are "descendants" of the ancestor node.

In the example, "encyclopedia" is the parent of "science" and "culture", its children. "Art"
and "craft" are siblings, and children of "culture", which is their parent and thus one of their
ancestors. Also, "encyclopedia", as the root of the tree, is the ancestor of "science", "culture",
"art" and "craft". Finally, "science", "art" and "craft", as leaves, are ancestors of no other
node.

Tree structures can depict all kinds of taxonomic knowledge, such as family trees, the
biological evolutionary tree, the evolutionary tree of a language family, the grammatical
structure of a language (a key example being S → NP VP, meaning a sentence is a noun
phrase and a verb phrase, with each in turn having other components which have other
components), the way web pages are logically ordered in a web site, mathematical trees of
integer sets, et cetera.
The Oxford English Dictionary records use of both the terms "tree structure" and "tree-
diagram" from 1965 in Noam Chomsky's Aspects of the Theory of Syntax.
In a tree structure there is one and only one path from any point to any other point. Computer
science uses tree structures extensively
For a formal definition see set theory, and for a generalization in which children are not
necessarily successors, see prefix order.
Examples of Tree Structures
 Internet:
o usenet hierarchy
o Document Object Model's logical structure, Yahoo! subject index, DMOZ
 Operating system: directory structure
 Information management: Dewey Decimal System, PSH, this hierarchical bulleted list
 Management: hierarchical organizational structures
 Computer Science:
o binary search tree
o Red-Black Tree
o AVL tree
o R-tree
 Biology: evolutionary tree
 Business: pyramid selling scheme
 Project management: work breakdown structure
 Linguistics:
o (Syntax) Phrase structure trees
o (Historical Linguistics) Tree model of language change

Lecture note Page 27


 Sports: business chess, playoffs brackets
 Mathematics: Von Neumann universe
 Group theory: descendant trees

BINARY TREE
A binary tree is a tree data structure in which each node has at most two children, which are
referred to as the left child and the right child. A recursive definition using just set theory
notions is that a (non-empty) binary tree is a triple (L, S, R), where L and R are binary trees or
the empty set and S is a singleton set. Some authors allow the binary tree to be the empty set
as well.

Types of Binary Tree


Tree terminology is not well-standardized and so varies in the literature.
 A rooted binary tree has a root node and every node has at most two children.
 A full binary tree (sometimes referred to as a proper or plane binary tree) is a tree in
which every node in the tree has either 0 or 2 children.
 A perfect binary tree is a binary tree in which all interior nodes have two children and
all leaves have the same depth or same level. (This is ambiguously also called a
complete binary tree.) An example of a perfect binary tree is the ancestry chart of a
person to a given depth, as each person has exactly two biological parents (one
mother and one father).
 In a complete binary tree every level, except possibly the last, is completely filled,
and all nodes in the last level are as far left as possible. It can have between 1 and 2 h-1
nodes at the last level h. An alternative definition is a perfect tree whose rightmost
leaves (perhaps all) have been removed. Some authors use the term complete to refer
instead to a perfect binary tree as defined above, in which case they call this type of
tree an almost complete binary tree or nearly complete binary tree. A complete
binary tree can be efficiently represented using an array.
 In the infinite complete binary tree, every node has two children (and so the set of
levels is countably infinite). The set of all nodes is countably infinite, but the set of all
infinite paths from the root is uncountable, having the cardinality of the continuum.
These paths correspond by an order-preserving bijection to the points of the Cantor
set, or (using the example of a Stern–Brocot tree) to the set of positive irrational
numbers.
 A balanced binary tree has the minimum possible maximum height (a.k.a. depth) for
the leaf nodes, because for any given number of leaf nodes, the leaf nodes are placed
at the greatest height possible.

A full binary tree A


complete binary tree
Lecture note Page 28
Properties of Binary Tree
 The number of nodes n in a full binary tree, is at least n = 2h + 1 and at most n =
2h+1 + 1, where h is the height of the tree. A tree consisting of only a root node has a
height of 0.
 The number of leaf nodes l in a perfect binary tree, is l= (n+1)/2 because the number
of non-leaf (a.k.a. internal) nodes
log2 (𝑙)−1

𝑛−𝑙 = ∑ 2𝑘 = 2log2(𝑙) − 1 = 𝑙 − 1
𝑘=0
 This means that a perfect binary tree with l leaves has n=2l-1 nodes.
 The maximum possible number of null links (i.e., absent children of the nodes) in a
complete binary tree of n nodes is (n+1), where only 1 node exists in bottom-most
level to the far left.
 The number of internal nodes in a complete binary tree of n nodes is (n/2).
 For any non-empty binary tree with n0 leaf nodes and n2 nodes of degree 2, n0 = n2 +
1.

Tree Traversal
A tree traversal (also known as tree search) is a form of graph traversal and refers to the
process of visiting (checking and/or updating) each node in a tree data structure, exactly once.
Such traversals are classified by the order in which the nodes are visited.

Depth-first search
These searches are referred to as depth-first search (DFS), as the search tree is deepened as
much as possible on each child before going to the next sibling. For a binary tree, they are
defined as display operations recursively at each node, starting with the root, whose
algorithm is as follows:

The general recursive pattern for traversing a (non-empty) binary tree is this: At node N you
must do these three things:
(L) Recursively traverse its left subtree. When this step is finished you are back at N again.
(R) Recursively traverse its right subtree. When this step is finished you are back at N again.
(N) Process N itself.

We may do these things in any order. If we do (L) before (R), we call it left-to-right traversal,
otherwise we call it right-to-left traversal. The following methods show left-to-right traversal.
Pre-order

Lecture note Page 29


Pre-order: F, B, A, D, C, E, G, I, H.

1. Check if the current node is empty / null.


2. Display the data part of the root (or current node).
3. Traverse the left subtree by recursively calling the pre-order function.
4. Traverse the right subtree by recursively calling the pre-order function.

Inorder

In-order: A, B, C, D, E, F, G, H, I.

1. Check if the current node is empty / null.


2. Traverse the left subtree by recursively calling the in-order function.
3. Display the data part of the root (or current node).
4. Traverse the right subtree by recursively calling the in-order function.
In a search tree, in-order traversal retrieves data in sorted order.

Post order

1. Check if the current node is empty / null.


2. Traverse the left subtree by recursively calling the post-order function.
3. Traverse the right subtree by recursively calling the post-order function.
4. Display the data part of the root (or current node).

The trace of a traversal is called a sequentialisation of the tree. The traversal trace is a list of
each visited root. No one sequentialisation according to pre-, in- or post-order describes the
underlying tree uniquely. Given a tree with distinct elements, either pre-order or post-order
paired with in-order is sufficient to describe the tree uniquely. However, pre-order with post-
order leaves some ambiguity in the tree structure.

Lecture note Page 30


Generic tree
To traverse any tree with depth-first search, perform the following operations recursively at
each node:
1. Perform pre-order operation.
2. For each i from 1 to the number of children do:
1. Visit i-th, if present.
2. Perform in-order operation.
3. Perform post-order operation.

Depending on the problem at hand, the pre-order, in-order or post-order operations may be
void, or you may only want to visit a specific child, so these operations are optional. Also, in
practice more than one of pre-order, in-order and post-order operations may be required. For
example, when inserting into a ternary tree, a pre-order operation is performed by comparing
items. A post-order operation may be needed afterwards to re-balance the tree.

Breadth-first search

Level-order: F, B, G, A, D, I, C, E, H.
Trees can also be traversed in level-order, where we visit every node on a level before going
to a lower level. This search is referred to as breadth-first search (BFS), as the search tree is
broadened as much as possible on each depth before going to the next depth.

Other types
There are also tree traversal algorithms that classify as neither depth-first search nor breadth-
first search. One such algorithm is Monte Carlo tree search, which concentrates on analyzing
the most promising moves, basing the expansion of the search tree on random sampling of the
search space.

QBASIC

Introduction
Can you guise why it is called BASIC? I meant what do you suppose is the emphasis of the
BASIC computer programming language? The answer is simple; it is called BASIC because
the language emphasizes the BASIC ideas found in all programming languages. BASIC is an
acronym, for– Beginners’ All-purpose Symbolic Instruction Code. The language BASIC
was designed in the early 1960s for teaching the basic principles of programming to non-
science majors. It has been popular ever since. There are many versions of BASIC. These
notes use QBasic; a Microsoft released a version of 1985, it is a version that once came free
with Microsoft operating systems. With QBasic you can easily write small programs and get
the idea of what programming is about. Other versions of BASIC are intended for

Lecture note Page 31


professional programmers who expect to write programs that are many thousands of lines
long. Our programs will not get that long.

Lesson 1
Running Your First Program
 Before you can create a program in QBASIC, you need the QBASIC compiler.
 QBASIC compiler can be downloaded from the Internet
 QBASIC compiler has a startup window with blue background.
 The Startup window will normally request you to press optional keys to continue.
Press the key that is appropriate.
QBasic on your Computer
 If your computer is running any variety of a Microsoft operating system, it can run
QBasic.
 If you are running a more recent operating system, click on the "Start" button.
o Click on "Run"
o In the "Open" box, enter CMD
o Click "OK" (or hit "Enter")
 When you get a window with the prompt, enter the command: qbasic then press the
ENTER key.

Fig 1.0 QBASIC Translator Start Up Screen

First Sample Program


Enter the first sample code in the QBASIC compiler as follows:
REM First QBASIC Program
CLS
PRINT "Hello"
PRINT
PRINT “This is my first Program “
REM – End Program--
CLS

Fig 1.1 QBASIC compiler with code

Lecture note Page 32


 Press F5 to run the program OR from the menu bar, click Run then Start OR Press
Shift + F5.
 You should now see a black screen as in fig 1.2 below

Fig 1.2 QBASIC Output Screen

QBASIC Program Building Blocks

QBASIC programming language is made up of some fundamental building blocks which are
treated as follows:

a. QBASIC Data Types there are various data types in QBASIC and can be
classified into two major categories:
(i.) Constants and
(ii.) Variables

Constant: Constants are numbers within a program whose values do not change.

 There are two classifications of constants namely:


(i.) Numerical constants: This comprises of Integer Constants and Real Constants.
(ii.) String Constants

Constant Declaration
 To declare constants of any type (integer, real or string), the keyword used in QBasci is
CONST.
 Constants can be declared as simply a value or with a descriptive name:
E.g.
CONST CourseUnit = 4.0
CONST CGPA = 4.49
CONST CourseTitle = “CS142”

Variable
Modern computers have a large amount of main memory (also called RAM). This memory is
used for many things. When you run a QBasic program, the statements of the program are
stored in main memory. The data for the program also can be stored in main memory. A
variable in QBasic is a small amount of computer memory that has been given a name. You (the
programmer) think of the name you want to use. The QBasic system will use a section of main
memory for that name. A variable is like a small box that holds a value. The value in the variable
can change (that is why it is called a variable). Here is a program that uses a variable:

 A Variable in QBASIC is a small amount of computer memory that has been given a
name. The location of a variable in RAM is called the "address.”

Lecture note Page 33


 Variables enable your program to remember values from one program statement to the
next.
 A variable is accessed by calling its name. Variable names can have a combination of
letters and numbers

In algebra, what do you call symbols like "x" and "y”, as in 3x2 + 2y?

The Answer is: variables

Examples of variable names are:


- Y, num, VALUE, aBc, xyz123

Variable Declaration
There are two methods for variable declaration:

i. Explicit Declaration: Here, you explicitly declare the variable as a type. This is done by
using the DIM statement.

The DIM statement is used to declare variables as follows:

DIM state AS STRING


DIM number AS INTEGER
DIM profit AS DOUBLE
DIM gpa AS SINGLE
state = “Covenant University”
number = 324
profit = 34.987890287
gpa = 4.99
PRINT state
PRINT number, profit, gpa

Examples of declaration with these symbols are:


DIM count$ --- count is of String type
DIM count% --- count is of Integer type
DIM count& --- count is of Long type
DIM count! --- count is of Single Precision type
DIM count # -- count is of double Precision type
ii. Implicit declaration: With this method, you put a symbol after the variable name which is
defined as representing that type.
These symbols are:

$ -- String
% -- Integer
& -- Long
! -- Single
# -- Double

With these symbols, variables could be declared and defined with a single statement using the
LET keyword.
E.g.
LET x$ = “QBASIC is cool!”

Lecture note Page 34


LET x% = 5
LET x& = 92,000
LET x! = 3.725
LET x # = 3.1417583294

Here is an example of a program that uses a variable:

' Program that uses a variable


'
LET NUM = 23.5
PRINT "The variable contains", NUM
END
When this program runs, the value 23.5 is placed in the variable NUM. Then the following is
written to the monitor:

The variable contains 23.5


What do you think the following program will write to the monitor?
' Program with a variable
'
LET NUM = 53
PRINT "The variable contains", NUM
END
Answer:
The program prints: The variable contains 53

Data Types in QBasic

Table 1.0 Data Types in QBASIC

Names for Variables


Remember that a variable is a small amount of computer memory that has been given a name. it
can be given a name such as:

Lecture note Page 35


NUM, POUNDS, VALUE, MILE, SUM, CLASSES, AGE

Names for variables must be single words that the programmer picks. The names don't have to
be real words, but it they should wisely choose to help in understanding the program they are.
Look over the following rules.

Rules for Variable Names


a. A variable is a small amount of computer memory that has been given a name.
b. The name is:
o up to 40 characters long,
o the first character must be A-Z, a-z,
o The rest of the characters must be A-Z, a-z, 0-9, or "."
o No spaces are allowed inside of a name.
c. You can't use a word that is already used for something else. (You can't use PRINT as a
name of a variable.)
d. The last character of the name tells what type of data the memory holds:
o SUM% holds an integer (no decimal point)
o SUM& holds a potentially very big integer
o SUM holds a floating point number (can have a decimal point)
o SUM# holds a potentially very big floating point number
o SUM$ holds a string of characters
e. Different names mean different places in memory.
f. The last character is part of the name, (so SUM% and SUM are different variables.)
g. If two names differ only in upper and lower case characters, they are really the same
name.

QUESTION :
Which of the following are OK names to use for a variable that will hold a floating point
number?
SUM, GRAND TOTAL, MyValue, 16Candles, SUM23, YEAR%

Answer:
o SUM --- OK (made up of correct characters, not too long)
o MyValue --- OK (upper and lower case letters can be used)
o SUM23 --- OK (digits can be used after the first letter)
o GRAND TOTAL --- BAD (no spaces allowed)
o 16Candles --- BAD (digits can't be used as first character)
o YEAR% --- BAD (last character % means this is an integer variable)

Case Differences in Variable Names


Look at the last rule again: If two names differ only in upper and lower case characters, they are
really the same name in Qbasic; meaning it is not case sensitive.
This means that:
VALUE , Value , value , vALuE : are really all the same name. In fact, if you try to use
more than one of these versions in your program, the QBasic system will change what you have
typed so that only one version is used. Say that you have typed in the following program:

' Attempt to use two versions of the same name


'
LET VALUE = 123

Lecture note Page 36


PRINT value

As soon as you move the cursor out of the PRINT statement the QBasic system will change
your program:

' Attempt to use two versions of the same name


'
LET value = 123
PRINT value

QUESTION :
VALUE is the name of a variable. Which of the following name a different variable?

VALUES , value, VALU , vALUe, VALUE$, Value

Answer:

o VALUES --- different, because it ends with an S


o VALU --- different, because the final E is mission
o VALUE$ --- different, because the final $ means a character variable
o value --- same
o vALUe --- same
o Value --- same

When Memory is Found for Variables


The first time a variable is used in a program, the QBasic system finds memory for it. This is true
for any statement, not just the LET statement. If there is no other information, the system will
put a zero into a number variable. Here is an example:
' Calculate Miles per Gallon (Buggy Version)
'
LET MILES = (45678.3 - 45149.6)
PRINT MILE / 12.5
END
The name MILE in the PRINT statement is the first time that variable name is seen. (MILES is a
completely different name as far as QBasic is concerned.) So the system finds memory for the
new variable MILE, and, lacking any other information, puts a zero into it. Now there are two
variables in memory:

MILES MILE

528.7 0

The arithmetic expression MILE / 12.5 will get the 0 from MILE and divide it by 12.5, resulting
in 0. Finally the PRINT will write to the monitor:
0
This is a bug, and hard to track down unless you carefully look at the spelling of each variable
name in the program. If a program you write mysteriously calculates an incorrect answer of zero,
check the spelling of each variable!

Lecture note Page 37


QUESTION 11:
What do you think the following program will write to the monitor?
' Calculate Miles per Gallon (Buggy? or not?)
'
LET MILE = (45678.3 - 45149.6)
PRINT MILE# / 12.5
END
Answer:
The program will print:
0
This is because MILE and MILE# are two different names, for two different variables. MILE#
was first used in the PRINT statement, so that is when memory was found for it. It was
initialized to zero, so it held a zero when the division MILE# / 12.5 was done.

Another QBASIC building Block is:

b. Some QBASIC Commands and Their Meanings


Commands tell QBASIC things to do as desired by the programmer. Commonly used QBASIC
commands are treated as follows:

i. INPUT Command
Syntax

INPUT list of variables


OR
INPUT “Prompting Message”, list of variables

ii. READ Command


Syntax

READ variable1, variable2, ... , variable n


.
.
DATA constant1, constant2, ..., constant n

iii. PRINT Command


Syntax
(i)
PRINT list of variables
OR
PRINT “Prompting Message”, list of variables

(ii)
? list of variables
OR
? “Prompting Message”, list of variables

iv. LET Command


Syntax

LET Variable = Numeric value/String value

Lecture note Page 38


OR
LET Variable = Expression
The LET Statement
Look at this program that uses variable again:

' Program that uses a variable


'
LET NUM = 23.5
PRINT "The variable contains", NUM
END

In this program, NUM is a variable. The programmer chose the name NUM . When the
program runs, several things happen when the LET statement executes:
 Memory is reserved for the variable NUM
 The number 23.5 is stored in the variable

So after this statement has executed a section of memory named NUM holds 23.5:

NUM

23.5

After the first statement has executed, the second statement executes:
PRINT "The variable contains", NUM
The variable NUM already exists, so no more memory is reserved for it.
The PRINT statement does several things:
 It prints "The variable contains"
 It looks in the variable NUM for a number.
 It prints out the number it found.

So on the monitor you see: The variable contains 23.5

QUESTION:
What do you think the following program will write to the monitor?
' Program with a variable
'
LET VALUE = 2 + 3
PRINT "The result is", VALUE
END
Answer:
The program will print: The result is 5
Saving a Result
Look at this new program again:
' Program with a variable
'
LET VALUE = 2 + 3
PRINT "The result is", VALUE
END

Lecture note Page 39


The first statement does several things:
1. Memory is reserved for the variable VALUE
2. A number is calculated: 2 + 3 = 5
3. The number 5 is stored in the variable.

The LET statement can be used with a variable to save the result of arithmetic. The value saved
in the variable will stay there until another statement changes it, or until the program stops
running.

QUESTION 4:
What do you think the following program will write to the monitor?
' Saving a result in a variable
'
LET SUM = 1 + 2 + 3
PRINT "The sum is", SUM
END
Answer:
The program will print: The sum is 6

Exercise
Using LET and other relevant commands, write a QBASIC program that finds the area and
perimeter of a triangle.

Another QBASIC building Block is:


c. QBASIC Standard Functions
Standard Functions: These are pre-written subprograms library provided in the compiler
environment which allow the programmer to use them without having to write code for certain
tasks. It can be grouped into the following categories:
i.) Numeric Functions
ii.) String Functions
iii.) Graphic Functions
iv.) Sound Functions
v.) File Handling Functions

Numeric Functions
These are functions for mathematical calculations provided in the QBASIC compiler. Numeric
functions take numbers as arguments and returns a number as output. Some of these functions
are:

ABS Function: returns the absolute value of a number.


Syntax

Num = ABS(signednumber)

The INPUT Statement


In this section you will learn about the QBasic INPUT statement. The INPUT statement
gets data from the computer keyboard.

Lectures Topics
o Input and Output in a computer system.

Lecture note Page 40


o INPUT statement.
o Prompting the user.
o Entering number data.
o Using several INPUT statements.

Up until now in these notes, all the data a program uses have been part of the program
itself. For example:
' Calculate Miles per Gallon
'
LET MILES = 45678.3 - 45149.6
LET GALLONS = 12.5
PRINT MILES / GALLONS
END

The data is the first odometer reading (45149.6), the second odometer reading (45678.3),
and the number of gallons.

QUESTION:
Will this program do the same thing each time it is run?
Answer:
Yes.
Input Devices
Consider the program.
LET MILES = 45678.3 - 45149.6
LET GALLONS = 12.5
PRINT MILES / GALLONS
END

Every time you run the program it will do exactly the same thing. The next time you fill
up your gas tank again, you would have to change the program to calculate with the new
values. The program is not very useful.
Most useful computer programs input data from various sources when they run. Input
means data that comes from outside the program. A program that does this can work
with new data each time it is run.
Sources of Input Data
o The Keyboard
o A Hard Disk
o A Floppy Disk
o A Cd-Rom
o The Mouse
o A Video Camera
o An Audio Digitizer (Part Of A Sound Board)
o The Internet
o Hundreds Of Other Sources

For us, most data will come from the user of a program typing on the keyboard. Some
sources of data (like audio data) require special hardware. A piece of hardware that is a
source of input to a computer program is called an input device.

Output Devices

Lecture note Page 41


As they run, most computer programs output data to various places. In programming,
output means data that the program sends outside of itself to some device. For us,
output mostly will go to the computer monitor.

Output can be sent to:


o The Monitor
o A Hard Disk
o A Diskette (Floppy Disk)
o The Printer
o A Sound Board
o The Internet
o Hundreds Of Other Places
A piece of hardware that uses data from program is called an output device.

QUESTION:
Some devices are on the list of input devices and on the list of output devices. What were
some of these devices?
Answer:
o a hard disk
o a diskette
o the Internet
I/O
The hard disk of a computer system is used for both Input to a program and Output
from a program. Other devices are exclusively input devices (such as the keyboard or
mouse) or output devices (such as the printer and monitor.) Input and Output are so
important to a computer system that the abbreviation I/O is used. Any device which
does either input, output, or both is called an I/O device. The movement of data from
or to such a device is often called "I/O".

The INPUT Statement


QBasic uses the INPUT statement to input data from the keyboard. Here is a program
that asks the user for a number, then prints out that number times two:

' Double a Number


'
PRINT "Type a number" 'Ask the user for a number
INPUT NUMBER 'Get it from the keyboard, put it in NUMBER
PRINT NUMBER * 2 'Print the number times two.
END

Here is how this program looks in the QBasic window:

Lecture note Page 42


These statements will execute one by one in sequence. To run the program push "shift-
F5" or use the menu "alt-R, S" (as usual).

QUESTION:
What is the first thing the program writes to the monitor?
Answer:
Type a number

Running the Program


The first statement is an ordinary PRINT statement that writes a string to the monitor.

' Double a Number


'
PRINT "Type a number" 'Ask the user for a number
INPUT NUMBER 'Get it from the keyboard, put it in NUMBER
PRINT NUMBER * 2 'Print the number times two.
END

Now the second statement INPUT NUMBER executes. The INPUT statement is used
to get data from the keyboard. It will:
o Print a question mark "?" to the screen.
o Wait until the user has typed in a number.
o Get the number when the user hits the "enter" key.
o Put the number in the variable NUMBER.

Just after this statement starts the monitor will look something like:
Type a number
?
The question mark came from the INPUT statement. The INPUT statement is waiting
for the user (you) to type a number. Say that you type 23. Now the monitor looks like:
Type a number
? 23
To enter the number, hit the enter key. The INPUT statement puts the 23 into the
variable NUMBER. Next, the PRINT statement executes.

QUESTION:
What will the monitor finally look like?
Answer:

The program has done both output and input. It output data to the monitor when it
wrote "Type a number" and the "?". It did input from the keyboard when it got the 23
and stored it in NUMBER. Then it did output again when it wrote 46 to the monitor.

Lecture note Page 43


Now hit any key on the keyboard to switch back to the QBasic window from the DOS
window.

Several Runs of the Program


You can run the program again, without making any change to it. (Push "shift-F5" to
start each run.) You can enter different data for each run. Here is an example of three
runs of the program:

After each run of the program, push any key to return to the QBasic screen. The picture
shows the DOS window after the program was run three times.

QUESTION:
Does the program work with floating point numbers?
Answer:
Yes. Floating point numbers are those with a decimal point, such as in the second run of
the program. The variable NUMBER can hold floating point values.

Variables with INPUT


The INPUT statement must use a variable as a place to put the data it gets. Remember
that a variable is a small amount of computer memory that has been given name. There is
nothing special about the variable used with INPUT. Here is the same program as
before, except that it uses a different variable name. It will do exactly the same thing as
the first version.

' Double a Number


PRINT "Type a number" 'Ask the user for a number
INPUT MyData 'Get it from the keyboard, put it in MyData
PRINT MyData * 2 'Print twice the number.
END

The variable should be the correct type for the expected input. Remember that the last
character of a variable name indicates what the variable is expected to hold. For example,
a variable VALUE# can potentially hold a very big floating point number. A variable
DATA% is expected to hold an integer (no decimal point). Here is a program that does
input of an integer:

' Integer Input


PRINT "Type an integer" 'Ask the user for an integer
INPUT DATA% 'Get it from the keyboard, put it in DATA%
PRINT DATA% * 2 'Print twice the number.

Lecture note Page 44


END

QUESTION:
Say that the user types 1.2 when the INPUT statement of this program asks for data.
What will the monitor show after the user hits "enter"?
Answer:
Type a number
? 1.2
2
The variable DATA% can only hold an integer, which cannot have a decimal point. The
user typed 1.2, but only the 1 was put into the variable. (The 1.2 is said to have been
truncated to 1). The 1 was then doubled, and 2 was output.

Control Structure
Loops
Many machines do their work by using repeated motions, cycles. The engine in your car
performs the same motions over and over again as it burns gasoline to provide power.
Electric motors are similar; they convert electric power into spinning motion. Both of
these machines are useful because they do the same things over and over and can keep
going as long as we want.

Computer programs, also, use cycles. Much of the usefulness of computer software
comes from doing things in cycles. In programming, cycles are called loops. When a
program has a loop in it, some statements are done over and over as long as is needed to
get the work done. Most computer programs execute millions of program statements
each time they are used. Most of these statements are the same statements being
executed over and over many times. This section of the lesson discusses loops in QBasic.

Lecture Topics:
o The DO ... LOOP
o Using CONTROL-BREAK to Exit a Loop.
o The Loop Body.
o Indenting Loop Bodies.
o Sequential Execution And Looping Combined.
o The SOUND Statement.

The DO and LOOP statements


Here is a program that uses a loop:
' Example of a loop
'
DO
PRINT "Type a number"
INPUT NUMBER
PRINT "6% of the number is", NUMBER * 0.06
LOOP
END
The DO statement marks the beginning of several statements that will be done again and
again. The LOOP statement marks the end of those statements. When this program runs
this is what happens:

Lecture note Page 45


o The program starts with the DO statement. The DO shows the beginning of the
loop.
o The first PRINT statement is executed and prints "Type a number".
o The INPUT statement is executed and gets a number to put in the variable
NUMBER.
o Say that the user typed in 100.
o The second PRINT statement prints "6% of the number is" and then computes
and prints six percent of NUMBER.
o If the NUMBER is 100, the program will print "6% of the number is 6"
o The LOOP statement marks the end of the loop.
o At this point, the statements between DO and LOOP have each executed
one time. They are about to execute a second time.
QUESTION:
What does the monitor look like at this time?
Answer:
At this point the monitor looks like:
Type a number
? 100
6% of the number is 6
But we are not done, yet.
Same Statements, Executed Again
Here is the example program, again:
' Example of a loop
DO
PRINT "Type a number"
INPUT NUMBER
PRINT "6% of the number is", NUMBER * 0.06
LOOP
END
The statements between DO and LOOP have each been executed once. The variable
NUMBER has the value 100 in it:

NUMBER

100

Now the statements inside the loop start again with the first statement after DO:
o For a second time, the PRINT statement prints "Type a number".
o For a second time, the INPUT statement gets a number to put in the variable
NUMBER.
o Say that the user typed in 50. The number 50 will replace what was previously in
the variable.
o For a second time, the second PRINT statement prints "6% of the number is"
and then computes and prints 50 * 0.06.
o The LOOP statement marks the end of the loop.

At this point, variable NUMBER now has the new value of 50 in it:

Lecture note Page 46


NUMBER

50

The monitor now shows:


Type a number
? 100
6% of the number is 6
Type a number
? 50
6% of the number is 3

QUESTION:
What do you suppose happens next? Have the user enter the number 200.
Answer:
The statements inside the loop are executed a THIRD time:
o For the third time, the PRINT statement prints a prompt.
o For the third time, the INPUT statement gets a number to put in the variable
NUMBER.
o Say that the user typed in 200. The number 200 will replace what was previously
in the variable.
o For the third time, the second PRINT statement prints "6% of the number is"
and then computes and prints 200 * 0.06.
o The LOOP statement marks the end of the loop.
Ending the Loop
Now the monitor shows:
Type a number
? 100
6% of the number is 6
Type a number
? 50
6% of the number is 3
Type a number
? 200
6% of the number is 12

The variable NUMBER has the new value of 200 in it:

NUMBER

200

The program will continue forever, performing the statements inside the DO ... LOOP
again and again. If you run this program, you will have to stop it, somehow. To stop the
program, hit CONTROL-BREAK. (Hold down the key marked "Ctrl" then tap the key
marked "Break.")

Lecture note Page 47


QUESTION:
Why not just let the program run until it ends?
Answer:
The program will never end.

Brackets
Look again at the program:
' Example of a loop
DO
PRINT "Type a number"
INPUT NUMBER
PRINT "6% of the number is", NUMBER * 0.06
LOOP
END

The DO and the LOOP are matched like brackets ( ) or [ ]. The statements inside of
them will be done over and over, starting with the first enclosed statement and going on
in sequence. You can have as many statements as you need between DO and LOOP:

DO ' start of loop


' first statement to be repeated
' second statement to be repeated
' third statement to be repeated
'
'
' last statement to be repeated
LOOP ' end of loop
END

QUESTION:
Do you see anything in the DO or the LOOP statement that says how many times the
statements between them are to be repeated?
Answer:
No, neither the DO nor the LOOP (nor the statements between them) say how many
times to repeat. The statements between the DO and the LOOP will be repeated
endlessly, or until the user hits CONTROL-BREAK.

A Loop Body
The statements between the DO and LOOP are called the body of the loop. So you can
say that for the example program, the loop body is repeated endlessly. Soon you will
learn how to write loops that do not repeat endlessly. Sometimes, however, endlessly
repeating loops are useful.

A Story Problem
Say that you are in charge of putting price tags on new merchandise in a clothing store. A
new shipment of several hundred items has just arrived. You have a list that tells you the
wholesale price of each item. The markup in your store is 50 percent, so the price tag
should be 1.5 times the wholesale price. You would like a program that asks you for the

Lecture note Page 48


wholesale price of an item and then prints the retail price, and keeps doing that over and
over (since you have hundreds of items).

QUESTION :
In rough outline, what do you suppose the program looks like?
Click here for a .
Answer:
Here is a rough outline for the program:
DO
' ask the user for the wholesale price of an item
' get the price, put it in a variable
' compute and print out the retail price
LOOP
END
Loop Conditions
The DO WHILE statement acts like a "gate keeper" for the loop body: execution is allowed to
enter the loop body only when a certain condition is true. This chapter will discuss how that
condition can be described.
Lesson Topics:
o Review of DO WHILE statements.
o The condition part of DO WHILE statements.
o Comparisons between numbers.
o Ending a loop with a Sentinel value.
o Examples of programs using sentinel values.
o General Scheme for DO WHILE loops with sentinels.
QUESTION:
(Review:) What is a loop condition?
Answer:
A loop condition is a test that is part of the DO WHILE statement.

Review of the DO WHILE statement


Look at the following program:
' Loop with a loop condition
'
LET COUNT = 0 'Statement 1
'
DO WHILE COUNT <= 5 'Statement 2
PRINT COUNT 'Statement 3
LET COUNT = COUNT + 1 'Statement 4
LOOP
END

Here the DO WHILE in statement 2 is the loop condition (the gate keeper). Execution enters
the loop body only if
COUNT <= 5
That is, only if the number stored in COUNT is less than or equal to 5. Then statements 3 and 4
are executed, and then LOOP sends execution back to the DO WHILE. The test COUNT <= 5
must be true every time the loop body starts to execute.

QUESTION:

Lecture note Page 49


Say that Statement 4 has just changed COUNT to 5. Now the LOOP statement sends execution
back to the DO WHILE. Will the loop body execute again?
Answer:
YES, because COUNT has a 5 stored in it, and:
COUNT <= 5
^
|
+----- holds a 5
is true because 5 is less than or equal to 5.

General form of the DO WHILE Statement


In general, the DO WHILE statement looks like this:

DO WHILE condition

. . . loop body . . .

LOOP
o The DO and the LOOP are brackets that mark the loop body.
o The WHILE condition lets execution enter (or re-enter) the loop body only if condition
is true.
o condition is a test involving variables and numbers.

So far in these chapters the condition has tested if one number is less than or equal to another
number. You can also test if one number is LESS THAN another number:
COUNT < 5
This tests if COUNT is LESS THAN 5. Here is the program, slightly modified.
' Loop with LESS THAN test
'
LET COUNT = 0 'Statement 1
'
DO WHILE COUNT < 5 'Statement 2
PRINT COUNT 'Statement 3
LET COUNT = COUNT + 1 'Statement 4
LOOP
END

Now the DO WHILE lets execution into the loop body only if COUNT is less than (but not
equal) to 5.

QUESTION:
Say that Statement 4 has just changed COUNT to 5. The LOOP statement sends execution back
to the DO WHILE. Will the loop body execute again?
Answer:
NO, because COUNT has a 5 stored in it, and:
COUNT < 5
^
|
+----- holds a 5
is FALSE because 5 is NOT less than 5.

Lecture note Page 50


Decision Making
In this section we will look at how computer programs make decisions based on numbers and
variables. This is done with a statement called the IF-THEN-ELSE statement. This is a very
useful statement. It will be an important building block in building up large programs.
Lesson Topics:
 Two-way Decisions.
 QBasic IF-THEN-ELSE statement.
 Outline of a two-way decision.
 Relational Expressions.
 Relational Symbols.
 Example Programs.

QUESTION:
You are driving in your car and it starts to rain. The rain falls on your windshield and makes it
hard to see. What should you do with the windshield wipers?
Answer:
Turn the windshield wipers on.

Two-way Decisions
The windshield wipers are controlled with an ON-OFF switch. The decision to turn the switch
on looks like this:

In this picture of a decision, you are supposed to start at the top, then follow the line to the
question:
is it raining?
The answer to the question is either TRUE or FALSE.
o If the answer is TRUE,
o follow the line labeled TRUE,do the directions in the box "turn wipers on", follow the
line to "continue"
o If the answer is FALSE
o follow the line labeled FALSE,
o do the directions in the box "turn wipers off", follow the line to "continue"

QUESTION:
How many ways can you go from "start" to "continue"?
Answer:
There are two paths through this chart.

QBasic Two-way Decision

Lecture note Page 51


The "windshield wiper" decision is a two-way decision (sometimes called a "true-false" decision.)
It seems small, but in programming very complicated decisions can be made up of many small
decisions.

Here is a QBasic program that includes a two-way decision:

PRINT "Enter a Number"


INPUT NUMBER
IF NUMBER >= 0 THEN
PRINT "The number is zero or positive" ' true branch
ELSE
PRINT "The number is negative" ' false branch
END IF
PRINT "Bye" ' this statement is always done
END

The words IF, THEN, ELSE, and END IF are brackets that divide the part of the program into
two branches. The ELSE is like a dividing line between the "true branch" and the "false branch".

 The IF statement always asks a question (usually about the number in a variable).
o The answer will be TRUE or FALSE.
 If the answer is TRUE only the true-branch is executed.
 If the answer is FALSE only the false-branch is executed.
 No matter which branch was chosen, execution continues with the statement after the
END IF.

A two-way decision is like picking which of two roads to take to the same destination. The fork
in the road is the IF statement, and the two roads come together just after the END IF
statement.

QUESTION:
The user runs the program and enters "12". What will the program print?
Answer:
Enter a number
? 12
The number is zero or positive.
Bye

The true branch was executed because the answer to the question NUMBER >= 0 was true.
The Program as a Chart
Here is the program again, done as a chart. Because the answer to the question was "true", the
path on the left was done.

Lecture note Page 52


The "two-way split" of the program is easy to see in a two dimensional chart. It is harder to see
this in a program.

QUESTION:
The user runs the program and enters "-5". What will the program print?
Answer:
Enter a number
? -5
The number is negative.
Bye
ONLY the FALSE branch was executed because the answer to the question NUMBER >= 0
was FALSE.
More than one Statement per Branch
Here is the program again with some added statements:
PRINT "Enter a Number"
INPUT NUMBER
'
IF NUMBER >= 0 THEN
PRINT "The number is zero or positive" ' true branch
PRINT "Positive numbers are > 0" ' true branch
ELSE
PRINT "The number is negative" ' false branch
PRINT "Negative numbers are < 0" ' false branch
END IF
'
PRINT "Bye" ' this statement is always done
END

The statements in the true branch are executed when the question in the IF statement is TRUE.
There can be as many statements as you want in the true branch. The true branch consists of the
statements between the IF statement and the ELSE statement. Of course, the statements in the
false branch are executed when the question in the IF statement is FALSE. There can be as many
statements as you want in the false branch. The false branch consists of the statements between the
ELSE statement and the END IF statement.

Lecture note Page 53


QUESTION:
In answer to the question, the user enters a 17. What will the new program print?
Answer:
Enter a number
? 17
The number is zero or positive
Positive numbers are > 0
Bye
The TRUE branch was executed because the answer to the question NUMBER >= 0 was
TRUE. The true branch consists of two statements this time.

Outline of a Two-way Decision


Here is how an outline of how to make a two-way decision:
... statements done before the decision
'
IF condition THEN
.... ' true branch
....
.... ' true branch
ELSE
.... ' false branch
....
.... ' false branch
END IF
'
... statements done after the branch comes back together

Here are some details:


 The IF condition THEN must be on one line.
 The condition compares variables and values.
o The statements in the true branch must be one per line.
o There can be as many statements in the true branch as you need.
 The ELSE statement must be by itself on a line.
o The statements in the false branch must be one per line.
o There can be as many statements in the false branch as you need.
 The END IF statement must be by itself on a line.

The condition looks like the part of a DO WHILE condition statement that compares what is held
in a variable with other values. You can use the same comparisons: <, <=, =, and so on.

QUESTION:
Is the following program correct?
PRINT "Enter a Number"
INPUT NUMBER
'
IF NUMBER >= 0 THEN
PRINT "The square root is:", SQR( NUMBER )
ELSE PRINT "There is no square root"
PRINT "Run the program again."
END IF

Lecture note Page 54


'
PRINT "Bye"
END
Answer:
No. The ELSE statement must be alone on its line.

Keeping Branches Separate


The IF, THEN, ELSE, and END IF are like brackets that emphasize the parts of the two-way
branch. The QBasic system requires that you put the ELSE and END IF on their own lines to
keep the true and false branches clear.
PRINT "Enter a Number"
INPUT NUMBER
'
IF NUMBER >= 0 THEN
PRINT "The square root is:", SQR( NUMBER )
ELSE PRINT "There is no square root" <---- Wrong!!!
PRINT "Run the program again."
END IF
'
PRINT "Bye"
END
So the above program is incorrect because the ELSE was not alone on its line.

QUESTION:
How would you fix the above program?
Answer:
PRINT "Enter a Number"
INPUT NUMBER
'
IF NUMBER >= 0 THEN
PRINT "The square root is:", SQR( NUMBER )
ELSE
PRINT "There is no square root"
PRINT "Run the program again."
END IF
'
PRINT "Bye"
END

In this program there are a different number of statements in the true branch than in the false
branch. This is fine.

Lecture note Page 55

You might also like