ICT 102: INTRODUCTION TO PROGRAMMING CONCEPT
USING QBASIC
ND II SECOND SEMESTER LECTURE NOTE, 2022
ea en Om a el (ele ee rd
Gee cee Ot elt Oe MWe ce naaen eon DLecIOBy Ibrahim Isa Wadda,
DEPARTMENT OF COMPUTER SCIENCE | RAMAT POLYTECHNIC MAIDUGURI
INTRODUCTION
It is of paramount important for every individual who uses a computer to be familiar
with at least one programming language in order to enable him/her the ability to
solve problems using the computer.
This note is intended for Civil engineering students in Ramat Polytechnic Maiduguri
with little or no programming skills. It explains the basic structures involved in
programming using QBASIC. Knowledge acquired here will help the student to
understand other programming languages easily.
PROGRAMMING WITH QBASIC
BASIC is an acronym for Beginners All-purpose Symbolic Instruction Code.
BASIC was developed By John Kemmeny and THomaz. Kutz, Various vastion of
BASIC has evolved since invention. Examples of some BASIC versions are; GW-
BASIC, BASICA, True- BASIC, QBASIC, Visual Basic.
QBASIC (Quick BASIC) is an evolution from BASIC. There are different versions
of QBASIC, Example QBASIC 1.0, QBASIC 1.1, QBASIC 4.0, QBASIC 4.5, ete.
‘The illustrations on this booklet has been tested on QBASIC 1.1 and QBASIC 4.0
COMPUTER PROGRAMS, PROGRAMMERS AND PROGRAMMING
LANGUAGES:
Everything that happens within a computer is as a result of instructions given to
that computer. Computer Programs are set of instructions given to a computer in
other to perform a particular task. These instructions are written by Programmers. A
computer programmer could be anyone who can interact with the computer using
programming languages. Programming Languages are not plain English or native
language. But online every other languages, each programming language has its
syntax (set of rules that govern a particular language).Programming Languages are
divided into three categories as follows:
1. Machine Languages
2. Assembly Languages
3. High Level LanguagesBy Ibrahim Isa Wadda,
DEPARTMENT OF COMPUTER SCIENCE | RAMAT POLYTECHNIC MAIDUGURI
Machine Language is the natural language of the computer, each of computers has
its own language. This language is difficult for human to understand.
Assembly language was developed due to the complexity of machine language.
Computers could only understand assembly languages with the aid of computer
programs called translators.
High Level Language was developed to ease the burden of programming. These
types of languages seem closely similar to human languages as compared to
assembly languages.
Low Level Languages (LLL): The term low level means closeness to the way in
which the machine has been built. Low level languages are machine oriented and
require extensive knowledge of computer hardware and its configuration.
Machine language: is the only language that is directly understood by the
computer. It does not needs any translator programme. We also call it machine code
and it is written as strings of 1s (one) and 0s (zero). When this sequence of codes is
fed to the computer, it recognizes the codes and converts it into electrical signals
needed to run it. e.g., a programme instruction may look like this 1011000111101.
Assembly Language: The set of symbols and letters forms the assembly language,
this set of symbols and letters is called mnemonics and a translator programme is
required to translate the assembly language to machine language. This translator
programme is called assembler. It is considered to be a second generation language.
High Level Languages (HLL): Higher level languages are simple languages that
use English and mathematical symbols like +,- ,% , /, ete. ‘ for its programme
construction.
STEPS TO SOLVE A PROGRAMMING PROBLEM.
‘A good program must undergo the following stages of development; these stages
must be followed orderly as listed below
1. Problem Analysis
2. Algorithm Development
3. Program Coding
4. Compilation and ExecutionBy Ibrahim Isa Wadda,
DEPARTMENT OF COMPUTER SCIENCE | AMAT POLYTECHNIC MAIDUGUR!
5. Debugging and Testing
6. Documentation
PROBLEM ANALYSIS.
It is a good programming practice for a programmer to analyze a given task,
checkout for what is involved, understand it before any other action is implemented.
ALGORTHM DEVELOPMENT
After a given task has been successfully analyzed , the programmer is expected to
develop an algorithm which will help during the program coding. An algorithm is
an illustrations that gives the guideline about the flow of any Program. An Algorithm
can be a flowchart or a pseudocode.
STEP 1: PROBLEM ANALYSIS
The first step towards solving this problem is the analysis stage. A good programmer
should be able to ascertain the following:
How many inputs are expected?
What are the operations which these inputs must undergo
Is there any expected output?
Ifyes, then what form should the expected output take.
And so on.
From the above problem, after the analysis, notice that
+ two input values are expected from the user, and this your inputs are integers
+ These integers are added together
+ The output of this is integer will be displayed After I have noted this, you can
now proceed to the second stage, which is Algorithm Development.
STEP 2. ALGORITHM DEVELOPMENT
Using a pseudocodes
Remember, It is not strictly recommended that a pseudocode should take the form
of any programming language. Pseudocodes are false codes because they could not
be executed in any programming environment.
The example below is a pseudocode for the problem in case1
1. Declare Num1 as an IntegerBy Ibrahim Isa Wadda,
DEPARTMENT O COMPUTER CENCE | AMAT POLYTECHNIC MAIDUGUR
2. Declare Num? as an Integer
3. Declare Result as an Integer
4, Input a Value for Num!
5. Input a value for Num2
6. Let Result = Numl + Num2
7. Display the output
8. End of Program
The above statements are not programming languages, they cannot be executed on
any programming environment because they do not follow any language syntax.
(Syntax are set of rules that govern a particular language.)
Also note that declaration of variables is not mandatory in all programming
languages.
The algorithm for the above problem can also be developed using a flowchart as
follows:
INPUT Numi , Num2 as Integers
|
Result= Numi + Num2
J
4
STEP 3. CODING
This involves transcribing the algorithm. This involves the use of programming
languages. In the course of our study, we will be writing of programs using a version
of BASIC called QBASIC.By Ibrahim Isa Wadda,
DEPARTMENT OF COMPUTER SCIENCE | RAMAT POLYTECHNIC MAIDUGURI
The recent problem could be written using QBASIC as follows:
CLS
DIM Num] AS INTEGER
DIM Num2 AS INTEGER,
DIM Result AS INTEGER INPUT Num1
INPUT Num2
Result = Num] + Num2 PRINT Result
END
The above QBASIC program will prompt the user to enter a value for the first
integer, NumI, And then Num2. After which it will process the operation and print
the Result.
STEP 4: PROGRAM COMPILATION AND EXECUTION
During this stage, the computer will run through the lines of codes, to check if there
are errors, Execution will only be successful if there are no error(bugs) on the
program. In most cases, compilation and execution can be completed in less than a
second.
STEP 5: PROGRAM DEBUGGING AND TESTING.
Mostly, computer programs, no matter how simple they look, may not be executed
properly, they may contain few errors or bugs at first instance, these programs are
debugged (i.e errors are eliminated gradually until desired target is achieved. These
errors can either be syntax or semantic errors).
STEP 6: PROGRAM DOCUMENTATION
Program documentation presents the detail description of each step in a program.
This is the final step of software development. It is very important because It gives
vital information about your program which could be useful in the future. Other
programmers will be able to understand your programs if it is well documented.
ALGORITHM
Algorithm is a step-by-step procedure, which defines a set of instructions to be
executed in a certain order to get the desired output. Algorithms are generally created
independent of underlying languages, i.e. an algorithm can be implemented in more
than one programming language.By Ibrahim Isa Wadda,
DEPARTMENT OF COMPUTER SCIENCE | RAMAT POLYTECHNIC MAIDUGURI
Characteristics of an Algorithm
Notall procedures can be called an algorithm. An algorithm must have the following
characteristic:
Unambiguous — Algorithm should be clear and unambiguous. Each of its
steps (or phases), and their inputs/outputs should be clear and must lead to
only one meaning.
Input — An algorithm should have 0 or more well-defined inputs.
Output — An algorithm should have 1 or more well-defined outputs, and
should match the desired output.
iteness — Algorithms must terminate after a finite number of steps.
Feasibility — Should be feasible with the available resources.
Independent — An algorithm should have step-by-step directions, which
should be independent of any programming code.
Flowchart is a diagrammatic representation of sequence of logical steps of a
program. Flowcharts use simple geometric shapes to depict processes and arrows to
show relationships and process/data flow.
Pseudocodes (also called false codes )could be English-like statements used to
illustrate every step involved in the design of a particular program.
A Flowchart is a diagram of statements of an algorithm showing the relationship
among the statements.
Some of the symbols used in a flowchart include the following
Symbol Symbol Name Purpose
Start/Stop | Used at the beginning and end of the
algorithm to show start and end of the
program.
Process Indicates processes like mathematical
operations.
outputs.
[TJ [Input Output Used for denoting program inputs and
Decision | Stands for decision statements in a program,
where answer is usually Yes or No.By Ibrahim Isa Wadda,
DEPARTMENT OF COMPUTER SCIENCE | RAMAT POLYTECHNIC MAIDUGURI
Arrow Shows relationships between different
shapes.
Cy On-page ‘Connects two or more parts of a flowchart,
Connector__| which are on the same page.
Offpage | Connects two parts of a flowchart which are
LJ Connector spread over different pages.
Example Flowcharts
Here is a flowchart to calculate the average of two numbet
Start
Take nut,
num2
Average =
(numtsnum2)/2
pi Print Average /
End
Difference between an identifier and a variable.
Identifier
+ All of them are not variables.
+ They are used to name a variable, a function, a class, a structure, a union.
+ Itis created to give a unique name to an entity.
+ They can consist of alphabets, digits, and underscores.
+ There is no punctuation or special symbol, except the underscore.By Ibrahim Isa Wadda,
DEPARTMENT OF COMPUTER SCIENCE | RAMAT POLYTECHNIC MAIDUGURI
+ It can be upper case or lower case.
+ It can start with lower case letter, upper case letter or an underscore.
+ Ithelps locate the name of the entity which is defined along with a keyword.
Example
enum geeks_artiles_in {Jan=1, Feb, Mar, Apr, May, June, July}
Variable
+ Its used to give a name to a memory location.
+ Itholds a value.
+ The names of variables are different.
+ They help allot a unique name to a specific memory location.
Example
int a= 6;
SOME QBASIC KEYWORDS
There are a number of keywords in QBASIC, This keywords are reserved for
specific functions and could not be used as variables in any instance.
Some of the frequently used Keywords in QBASIC are shown on the table below:
KEYWORD NAME
USAGE
CLS Clear Screen To clear the Output Screen.
REM Remark For insertion of Comments
DIM Dimension To Declare variables
PRINT PRINT To Display an output
END END Shows the End of an Program
INPUT INPUT Accepts values from a user
LET LET Assigns a value to a variable
IF IF Begins a conditional statement
INTEGER _| INTEGER Positive and negative whole numbersBy Ibrahim Isa Wadda,
DEPART OF COMPUTER SCIENCE |RAMAT POLYTECHNC AIDUGUY
BASIC’S FUNDAMENTAL DATA PROCESSING ELEMENT
An important first step in learning a programming language is to understand
the types of data that can be processed, the data structures that are allowed, and the
data operations that can be performed.
Data Types
Every programming language has a character set. These are the only
admissible characters that can be used in the language. Basic allows the use of both
numeric (0-9) and alphabetic (A,B, ...Z) characters as well as some special symbols
such as /,=,;,*, ete.
Data Structures: All version of BASIC enable programmers to make use of the
following four data structures: Constants, Variables, arrays, and files. These four
data structures can handle almost any business data processing task.
Constants: A constant refers to a data item that cannot change its value when a
program is run. It is either a numeric constant or string constant.
Numeric Constant: These are numbers used in BASIC. Numeric constants can be
integers or real integer constants are numbers without decimal pump eg. 4, 2000, -
186 etc. while real constants are numbers with decimal point eg. 2.006, 0.21, 6.,
10, -14.929, 13569.1 ete.
One of the nice features of BASIC is that integer and decimals are handled in
the same array. You do not have to distinguish between them. Here are two important
rules to follow when keying in numbers.
1. Always precede a negative number with a minus sign
2. Never use commas to indicate “thousands”, millions” and so on.
All versions of BASIC also use another means to represent real numbers —
scientific, or engineering notation. These real numbers are broken into two
parts; the first part indicates the number’s actual digits and the second part
notes the power of 10 to which these digits are being taken. Below are some
examples to illustrate.
Regular Notation, Scientific Notation
20.334 2.0334E+01
0.567 35.67E-03
-62939.6 -6.29396E+04By Ibrahim Isa Wadda,
DEPARTMENT OF COMPUTER SCIENCE | AMAT POLYTECHNIC MAIDUGUR!
String Constant: Any set of characters enclosed within a quotation marks are
identified by BASIC as string constants. Examples: “234629095”; “sales volume”;
“Enter yes or No”; etc.
Variables: A variable refers to a data item that can change its value during program
execution. In another words, it is a name given to memory location s for storing
constants. It can be numeric variable or string variable.
Numeric variables: Are names given to memory location for storing numeric
constants (numbers). It is formed either by a single alphabetic character or a single
alphabetic character followed by a single digit e.g. B or B2.
String Variables: These are variables whose contents are string constants. It is
formed by adding a dollar sign ($) to a numeric variable e.g. BS or B2$.
Note: Arrays will be discussed later.
DATA OPERATIONS:
Data values are transformed or created in BASIC by using three types of data
operations: arithmetical operations, relational operations and logical operations.
Specific operator within each type of data operation actually indicates the nature of
the operation to be transformed. These operations are normally represented in
expressions, which can be regarded as specific combinations of constants, variable
and operations. This system should become clear to you through the examples used
to explain the different types of operations
SIMPLE MATHEMATICAL OPERATIONS IN QBASIC
Like other programming languages, mathematical operations can be carried out in
BASIC using the following Operators
OPERATOR MEANING EXAMPLE
* Multiplication 24
+ Addition 2+4
/ Division 4/2
% Modulus 4%2
“or ** Exponentiation 42
10By Ibrahim Isa Wadda,
DEPARTMENT OF COMPUTER SCIENCE | RAMAT POLYTECHNIC MAIDUGURI
The above operators are called Arithmetic Operators. They are used to perform
simple algebraic operations.
Arithmetic expression Equiv. BASIC expression
(a+by (A+B)**3 or (AFBYS
ath? AtBY2
alth (AM+BY/IC
c
2,04 A/BH(C*D)(E*G*H)
b fgh
yYd--+y 0.5*(QIK-KAQ*Z"2))
Ak oqz’
Relational operators: There are six relational operators:
Operator operation Example Meaning
> greater than ADB A is greater than B
> preater or equal to ADB Ais greater than o equal to B
less than AB Ais less than B
less than or equal to. ASB Avis less than ot equal to B
= equal to A=B A is equal toB
<,F¢ not equal to AB A is not equal to B
The relational operators are used to form simple relational expressions. In any
simple relational expression, the value can be either true or false. This depends on
the nature of the relationship between the two arithmetical expres
Logical operations:-There are three logical operators:
Operator Operation
NOT changes the value of a relational expression (i.e. a true
Expression is evaluated as being “false” or a “false
expression is evaluated as being “true”).
AND Evaluated as “true” only when both relational expressions
are true.
OR Evaluated as “true” when either or both relational
expressions are true,
Below are illustration for the above discussions: Consider the case where A=3, B=4
and C=2
1By Ibrahim Isa Wadda,
DEPARTMENT OF COMPUTER SCIENCE | RAMAT POLYTECHNIC MAIDUGURI
Expression
‘A=3 AND B=4 ‘A=3 AND B=4
3=3 AND 4=4
TRUE AND TRUE
TRUE
B+2<4 AND 2*C>A B+2<4 AND 2*C>A
4+2<4 AND 2*2>3
6<4 AND 4>3
FALSE AND TRUE
FALSE
AtC=5 OR B>=3*A AtC=5 OR B>=3*A
3+2-5 OR 4>=3%3
5=5 OR 4>=9
TRUE OR FALSE
TRUE
NOT 2*C>BHA NOT 2*C >B+A
NOT 2*2>4+3
NOT 4>7
NOT FALSE=TRUE
These logical operators are used to construct compound relational expressions. Ab
compound relational expressions can contain more than one logical operator. With
compound relational expressions, NOT operations are performed before AND
operations, which are performed before OR operations.
FUNCTION
A funetion is a built-in formula to accomplish a certain task such as mathematical,
statistical, financial, logical/data calculations, etc. Functions in QBASIC are
readymade programs that take some data, manipulate them and return the value,
which may be a string or numeric type.
TYPES OF FUNCTIONS: —
QBASIC supports two types of functions. They are: —
A) USER-DEFINED FUNCTIONS: —
A function that is written and stored by the programmer to perform a specific task is
known as User Defined Functions. When QBASIC does not provide a built-in
2By Ibrahim Isa Wadda,
DEPARTMENT O COMPUTER CENCE | AMAT POLYTECHNIC MAIDUGUR
function to solve any particular problem required by the programmer in that case
only programmers define this type of function.
FUNCTION, ND FUNCTION statement can be used to define the
function.
DECLARE FUNCTION area(1,b) <—— Declaration Part
cLS
INPUT "Enter Length
Ta ata Tec a ee
Nea)
Aree Calling Function
rat} gota tao
FUNCTION area (1, b)
a=1*b Defining Function
lias procedure
PND easton)
Oe
1. Greatest among two number
FUNCTION PROCEDURE
DECLARE FUNCTION MAX(A,B)
CLS
INPUT "Enter any two number"; A, B
PRINT "The greater number is"; MAX(A, B)
END
FUNCTION MAX (A, B)
IF A> B THEN
MAX=A
END FUNCTION,
BBy Ibrahim Isa Wadda,
DEPARTMENT OF COMPUTER SCIENCE | RAMAT POLYTECHNIC MAIDUGURI
2. Using user defined function, write a program to input monthly income in
parameter then computer annual tax to be paid. The tax rate is 15% if annual
income is above Rs. 200000, otherwise tax rate is 1%.
FUNCTION PROCEDURE
DECLARE FUNCTION TAX(I)
CLS
INPUT "Enter monthly income"; I
PRINT "Tax to be paid="; TAX(I)
END
FUNCTION TAX (1)
A=I*12
IF A> 200000 THEN
TAX=15/100* A
ELSE
TAX=1/100* A
END IF
END FUNCTION
3 Given Number is perfect Square or Not
CLS
INPUT "enter any number"; n
A=SQR(n)
IF A= INT(A) THEN
PRINT "perfect square"
ELSE
PRINT "Not perfect square"
ENDIF
END
B) BUILD-IN OR LIBRARY FUNCTIONS: -
Predefined functions: these are functions that are “built into” BASIC (in the library
of BASIC). This means they are available for use in any BASIC program being
written, Below are some examples of predefined functions:
Funetion Description
SING) returns the sine of an angle
COS(x) returns the cosine of an angle
4By Ibrahim Isa Wadda,
DEPARTMENT OF COMPUTER SCIENCE | RAMAT POLYTECHNIC MAIDUGURI
TAN(x) retums the tangent of an angle
ATN(x) returns the arc tangent of an angle
LOG(x) returns the natural logarithm of a number
EXP(x) raises the mathematical number “e” to a power given by
the Argument x
SER) returns the square root of a number
STATEMENTS in QBASIC
A statement is a collection of instructions written in QBASIC using keywords and
commands. There are three types of statements in basic:
IF Statement
The IF statement always asks a question (usually about the number in a variable.)
If the answer is TRUE the true branch is executed. If the answer is FALSE the true
branch is skipped. In both cases, execution continues with the statement after the
END IF. Example:
CLS
IF 5>2 THEN
PRINT “5 is greater than 2”
END IF
SE Statement
The
else statement is a conditional statement that runs a block of code if'a
certain condition is met. If the Boolean expression is false, the if statement might
be followed by an optional else statement.
15By Ibrahim Isa Wadda,
DEPARTMENT OF COMPUTER SCIENCE | RAMAT POLYTECHNIC MAIDUGURI
Example:
CLS
INPUT "Enter any number “; N
IF N MOD 2 =0 THEN
PRINT "The number is EVEN ";
ELSE
PRINT “The number is ODD";
END IF
END
IF ELSE IF:
Statement: if else if statement is a conditional control chain of statement in Qbasic,
here first the program check the first expression of the program and if the
requirement is false, it sends you back to the if-else condition. In simple words, it
will simply check the first expression and if the first expression is an error it moves
to the second, And finally, need to end the statement using end if.
CLS
INPUT "ENTER ANY THREE NUMBERS "; A, B, C
IF A= 10
END
d. Nested Loop
‘The Loop inside the loop structure is called Nested loop. It contains an outer loop
and an inner loop.
77777777
6666666
555555
44444
333
22
1
wvBy Ibrahim Isa Wadda,
DEPARTMENT OF COMPUTER SCIENCE | RAMAT POLYTECHNIC MAIDUGURI
CLS
FOR A=7TO 1 STEP -1
FOR 1TOA
PRINT A;
NEXT B
PRINT
NEXT A
END
ARRAYS
Arrays hold a list of variables of the same data type.
Syntax
DIM ArrayName(Size) AS DataType
e.g DIM MyCar(5) AS STRING
The example above can be illustrated using a diagram
0 1 2 3 4
Honda
It simply Shows that Volvo is stored in the myCar(1)
We are going to write a program that will store the following their respective array
Locations.
0 1 2 3 4
Honda BMW Toyota Lexus Ferari
Note that the first index of an array is always 0.
Array can also be used to store numbers, which could be used for calculations.
Consider the example below
‘The Array myNum(5)
18By Ibrahim Isa Wadda,
DEPARTMENT OF COMPUTER CENCE | RAMAT POLYTECHNIC MAIDUGURL
23 nL iL. |
PRINT Num(0) + Num(3)
The Computer will calculate and Print 34 as the result.
Ey
The above will print “Ferari” as output. If you change to PRINT myCar(3) , the
Output will Be “Lexus” and so on.
... Wishing you guys best of Luck!
19