0% found this document useful (0 votes)
2 views28 pages

Msce Computer Notes 2022-Programming

Uploaded by

preciouskamodzi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views28 pages

Msce Computer Notes 2022-Programming

Uploaded by

preciouskamodzi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D.

CHONA-BRAINS MUST WORK

TOPIC: PROGRAMMING FUNDAMENTALS


Definition of Terms in Programming
 Computer program
This is a set of related instructions (statements) written in programming language to
instruct a computer to perform (carryout) a particular task.
A program contains a set or list of instructions (statements) that tell a computer to
perform a particular task on variables.

 Computer programming( or programming)


This is the process of writing a computer program (i.e. set of instructions) using a
programming language. E.g. BASIC, C, Ada e.t.c

 A programmer( also called Software developer or a Coder)


This is a person who writes (creates) and tests programs or software. A programmer uses
programming language software to code instructions that specify actions or tasks to be
carried out by a computer.
 Programming language
This is a formal language that specifies the syntax (structure or arrangement or format)
and semantic (meaningful) rules for writing computer programs.
Programmers use programming language software to write programs which are installed
in computers to instruct computers to perform required tasks.
Below are some examples of programming language:

Programming language Description


Pascal A programming language developed to help in teaching
and learning of structured (procedural/step-by-step)
programming. It was named after Blaise Pascal
FORTRAN(FORmula TRANslator) This is a programming language developed for
mathematicians, scientists and engineers to enable
writing of programs for solving mathematical, scientific
and engineering problems.
COBOL(Common Business- This is a programming language designed for
Oriented Language) development of programs that solve business
problems such as computer-based inventory control
system( stock control system)
BASIC(Beginners All-purpose This programming language was developed to enable
Symbolic Instruction Code) students easily learn programming. It is simple as such
it is a good tool for students that wish to learn
programming.

1
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

C:(named after its earlier This is a general purpose procedural (step-by-step)


language version called B as it programming language for developing system
obtained most of its features software (for use in UNIX, Linux O.S). It is a powerful
from it) high level programming language (i.e. With human
language features) but also offers programmers
powerful features for low level languages (i.e. machine
languages). It produces already compiled programs.
C is now available for many computers that’s why is
said to be a portable language.
Note: there is also C++ which is used for developing
operating systems, browsers and games.
Ada It was named after the first lady (female) programmer
named Ada Lovelaces.
It is a programming language for developing military,
industrial and real-time systems( i.e. systems that
update information at the same rate they receive the
information)

 Source code( programmers code)versus Object code( machine or computer code)


The Source code is set of instructions or statements written by a programmer in human-
readable form (i.e. in programming language form)
The source code needs to be later translated mostly by a compiler to produce a
machine-readable (computer-readable code) known as Object code.
In general sense an Object code is a sequence of instructions or statements in machine-
readable form (i.e. computer language). The object code is produced from translation of
source code. It is in binary format as such it is easily read, understood and executed by
the computer’s processor to solve a particular problem.

NOTE: Language translators (language processors) such as assemblers, interpreters and


compilers are required to translate (convert) source code (programmer’s code) to object
code (computer code) for execution of the instructions or statements to produce
required outcomes. Below is a simple description of language translators mentioned
above:

 Assembler
This is a language processor that converts source code of a program written in
assembly language into machine readable form (object code) that a computer
can understand and execute.
Note: Assembly language allows programmers write programs as a set of
symbolic operations codes called mnemonics (i.e. shortened two or three letter
words).
Below is an example of a source code in assembly language
MOV AX, 15 This means move the number 15 to a CPU register AX
SUB AX, 10 This means subtract 10 from the number in CPU register AX
NOTE: that programming in assembly language was difficult and time consuming as such it
was abandoned.

2
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

 Interpreter
This is a language processor that converts a source code into machine code
(object code) statement by statement(line by line)and allows the CPU execute
each instruction immediately after it has been translated.
Note: The interpreted statement or line is not stored in computer memory;
meaning that every time the program is needed for execution, the Interpreter
has to interpret each statement or line in the program again. This type of
language translator was commonly used in early computers that did not have
enough memory to store object code.

 Compiler
This is a language translator (language processor) that converts the whole
(entire) source code (i.e. all statements in a program) into its corresponding
object code and stores the object code before immediate or later execution. This
is called compiling.
Note: Once compiling has been done the computer keeps the translated code
(object code) such that no any other translation is done after the first translation.
What happens is that after translation; the compiler creates an executable file
through a process called linking which combines the object code with library files
that are required for execution of the program. The executable files are stored
on storage media with a name and a file extension .exe for example
[Link]

Nowadays computer programs are translated by Interpreters or Compilers


See the differences below:
Interpreter Compiler
It translates source program one It translates the whole source program
statement at a time and executes the into object program at once and stores
translated statement (object code) it before execution.
immediately without storing it
It translates a program each time it is The compiled program (object code) is
run hence slower than compiling saved or stored so translation is done
once and execution is multiple.
Interpreted object code takes less Compiled program requires more
memory than compiled program memory as its object file is larger.

History of Programming Languages


There are many programming languages that have been developed.
Classes/categories of programming languages
Programming languages are classified into two broad categories:
1) Low-level programming languages
2) High-level programming languages

3
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

The two categories of programming languages are divided into five generations of
programming languages
The low-level programming languages exist in the following generations:
a. First generation languages
b. Second generation languages
The high-level programing languages exist in the following generations:
c. Third generation language
d. Fourth generation language
e. Firth generation language
Other categories of programming language are object-oriented programming (OOP)
languages and web scripting language

Low-level programming Languages (also called hardware-oriented


languages)
These are programming languages that are easily understood by the computer. Programs
written in low-level languages require minimal (small) effort to convert into computer
(object) code. Note: Programs in Low-level programming language are hardware-specific as
such they are not portable. This means that they are written for particular computer
architecture as such they cannot be installed in another computer.
Types/ examples of Low-level programming languages
i. Machine language( first generation language)
This is a programming language in which instructions or statements are written using
binary code (computer code).
This language uses many lines of binary code to accomplish even a simple task like
just adding two numbers. See an example of a program in machine language:

1110 0011 00000001 10000001


00001100 10001101 11100011

Advantages of Machine language


 Programs in machine language are very easy for the computer to understand
and faster to execute ( without need for translation) since they are already in
binary code(computer code)
Problems associated with Machine language
 Program codes in machine language (i.e. in binary digits) are hard for a
person to understand what it is all about.
 Programs in machine language are hardware specific meaning that a code
written for Intel Pentium processor will not run on Motorola processor since
different CPUs understand machine language differently. This means that a
programmer has to know for which C.P.U the program is being written.

4
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

 Programs in machine language are hard to write and debug (i.e. locate and
correct errors).
Note: Debugging is the process of locating and correcting errors in a
program.

ii. Assembly language (second generation language)


This is a programming language that enables the programmer write program
instructions as a set of symbolic operation codes called mnemonics.
Mnemonics are shortened two or three letter words. Assembly language was
basically developed to solve problems of understanding and using machine language
(binary code)
See an example of an assembly language code

MOV AX , 9
ADD AX , 10

MOV means move


ADD means add
AX is a data register

The first line means move 9 into the processor’s register named AX.
The second line instructs the computer to add 10 to a value which is in a register AX.
Note: Programs in Assembly language require a language translator called Assembler to
translate the Symbolic code into Machine language code for the computer to understand
and execute it. Programs in assembly language are also machine-dependent as such a
program written for one computer cannot be used on another computer.

Advantages of Low-level programming languages


 Low-level language programs are directly understood by the CPU with minimal or no
conversion or translation.
 Programs in low-level language are also executed faster as they are already small simple
components.
 Programs in low-level language do not crash or breakdown easily once written.

Disadvantages of Low-level programming languages


 Low-level programming languages are difficult to learn and use.
 It is difficult to debug (correct errors) in low-level language programs.
 It requires highly trained programmers to develop and maintain programs in low-level
language.
 Programs in low-level language are machine dependant meaning that they are not
portable for use in other computers

5
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

High-level programming Languages (also called hardware/ machine


independent languages)
These are programming languages that are close to human language as such they can be
read and understood even by people that are not experts in programming.
Note: High-level programming languages are machine-independent meaning that the
programmer does not have to concentrate on the architecture of the machine for the
program since the program runs on all computer architectures.
Below is an example of a program written in Pascal, a high-level programming
language:[Link]

(*displays Hello World on Screen*)

Program HelloWorld (output);

Begin {main body}


Writeln (‘Hello World’);

End.
You can see that the language in use in the program is close to humans making it easy for
programmers to write programs.
Note: High-level language programs require translation into machine code for execution.
The translation is done by interpreters and compilers
Types/classes of High-level programming languages
These are as follows:
a) Third generation languages (3GLs) (Also called Structured or procedural languages)
These are programming languages that make it possible to break down a program
into components called modules with each module performing a particular task.
Note: The process of breaking down a program into modules (sub-programs) is called
structured programming.
Approaches emphasised in structured programming:
 Large programs should be broken down into smaller sub-programs with each
sub-program performing a single task.
 Use control structures in problem solving

Benefits of Structured programming


 It is easy to write programs
 It is flexible meaning that the programs run on all computers of all
architectures
 The program is easy to read and modify
Examples of third generation programming languages are: Pascal, FORTRAN,
COBOL, BASIC, C and Ada.

6
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

Below is a sample Third generation language program code written in Pascal.

1 (*displays Hello World on Screen*)

2 Program HelloWorld (output);

3 Begin {main body}

4 Writeln (‘Hello World’);

5 End.

Line1: The line enclosed in a combination of parenthesis () and asterisk * is known as a


comment. A Comment explains the purpose of a program or a statement to other
programmers.
Line 2: This is a program header

 In Pascal, a program starts with the keyword “Program” followed by the name of the
program in this case Hello World.
 The word output in the parenthesis is known as parameter. It indicates that the
program displays the output on the screen.
Line 3: The keyword Begin indicates the start of executable statements.

 This is where you define the structure of the program using input, output statements
e.g. Writeln (write line) for output and Readln (Read line) for input.
Line 4: Writeln(‘Hello world’)

 This is the most important statement in this program. Writeln is a Pascal output
function for instructing the computer to display Hello World on the screen in our
example once the program is executed. Writeln is short form for Write line. The
word Readln(short form for Read line) is a statement that takes input into Pascal e.g.
Readln(value)
Line 5: The last statement End. marks the end of the program

 When the computer reads End., the program is terminated and unloaded from main
memory(RAM).

b) Fourth generation languages (4GLs)


These were introduced as improvements to third generation languages to reduce
programmer’s effort by making programmers focus more on the problem other than
on algorithm (procedure or steps) or structure of the program. The Fourth
generation languages have advanced programming tools for integrating programs
with databases, generating reports and creating graphical user interface (GUI)

7
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

applications. The 4GLs also have a tool that automatically generates program code
called application generator. This helps to remove the tiresome work of writing
every line of code.

Example of Fourth generation languages (4GLs) are


 Microsoft Visual Basic
 Progress 4GL
 PowerBuilder
 FoxPro
 Python

A sample 4GLs program written in Visual Basic:


A program displaying Hello World. Notice the differences in syntax with Pascal Code

‘displays Hello World on Screen

Module HelloWorld

Sub Main() ‘main body


[Link](“Hello World”)
[Link]() ‘press any key to continue
End Sub

End Module

Below is a brief explanation of the program source code above:

Line 1: Starting with apostrophe denotes (indicates) a comment in Visual Basic that
explains the purpose of the program to other programmers.
Line 2: The Module (component) header. This is equivalent to a program header in
Pascal which gives the name of the program.
Note: in Visual Basic, an application may contain many modules. Each module must
be accompanied by a unique name.
Line 3: Sub Main is known as Main method or Subroutine. This marks the start or
entry point to executable statements. The first line (in this section) starting with
apostrophe denotes a comment in Visual Basic.(explains the purpose for the
statements).
Line 4: [Link](‘Hello World’)statement is similar to Writeln in Pascal.
Note: that the word Console means a component that controls input or output in
this case a screen. Therefore once the program is executed, Writeline instructs the
computer to display Hello World on the screen
8
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

Line 5: [Link]()function is similar to Readln used in Pascal, but in this


Visual basic program it is used to pause screen displaying until when the user presses
any key.
Line 6: End Sub marks the end of Main Method.
The End Module returns the control of the program to the operating system to
unload the program from memory.
c) Firth Generation Programming Languages (5GLs) (Also called Natural Languages)
These are programming languages used to develop systems that solve problems
using artificial intelligence. (I.e. systems that mimic (imitate) human-like intelligence.
For example:
 Speech recognition
 Decision making
 Movement(robotics)
Note: in firth generation languages a programmer only worries about the conditions
required to solve the problem and not designing the logic for solving the problem
called algorithm.
Examples of firth generation languages are:
 PROLOG
 Mercury
 OPS5
Object-oriented Programming (OOP)
There has been a shift from procedural (structured) programming to object-oriented
programming. In Object oriented programming, a program consists of several objects that
interact by sending messages to each other to do something. For example, in life a person
(considered as an object) sends message to another person requesting for something to be
done.
NOTE: that OOP is not classified as separate generation but just a shift in program
development methodology. In fact the programming languages that supported structured
(procedural) programming have just been improved to support object-oriented
programming. The objects that make up a program in OOP are linked together through a
method called association. Examples of OOP languages include: Microsoft Visual Basic,
Java, C++, Python, C, Perl, Delphi Pascal, Smalltalk, Ruby e.t.c

9
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

Below is a sample OOP: Hello World written in GUI environment of Visual [Link] showing
objects such as a Forms.

Public Class formHello


Private Sub CmdOk_Click(ByVal sender As [Link],ByVal e As
[Link]) Handles [Link]
‘This program displays
Hello World on a Form
[Link]= “Hello, World”
End Sub

End Class

Web development and Scripting Languages (also called Web programming


languages)
 Web development languages are languages used to develop and add functionalities to
web pages.
Note: Web pages are hypertext documents created in Hypertext Mark-up language
(HTML).HTML consists of tags that are interpreted by the web browser software to display
the content when the file is opened on the browser. A tag is a word, symbol or character
that is enclosed in angle brackets <> that is interpreted by the browser as a command.
Most HTML elements have an opening tag <tag name>and closing tag</tag name>.The
content to be displayed by the web browser is enclosed between the opening and closing
tags. E.g. <tag name>your content goes here</tag name>
For example the statement below will display the word “Hello World” in bold on the screen.
Note: type the HTML code below in Notepad. Save it with a file name and a file extension
.html extension
<HTML>
<body>
<h1>Hello World</h1>
</body>
</HTML>
Note: HTML tags are not case sensitive so you can type them in upper or lowercase.
Below are some examples of HTML and their meaning

HTML TAG MEANING


<html></html> This tag marks the beginning and end an HTMl document. It
encloses the entire HTML document. It is made of the document
header which is represented by the <head> tag and the document
body which is represented by <body> tag. The closing tag</html>
comes at the end of the script.
<head><head> This marks the header part of the html document. It contains
elements which identify the page when searching using search
engine. The document header can keep other tags such as <title>

10
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

<title><title> This gives the title of the HTML document (web page) e.g. About Us.
The <title> tag is used within the <head> tag
<body></body> This marks the visible content of the HTML document (web page). It
also contains other tags.
<h1></h1> This represents the heading within the document body, with h1 as
the largest size heading and h6 as smallest size heading.
<ol></ol> This represents ordered list of items (e.g. numbered list). Listing of
each item is done using <li> as open tag and </li> as closing tag. The
closing tag </ol> comes at the end of the listing.
<ul><ul> This represents unordered list of items (e.g. bulleted list). Listing of
each item is done using <li> as open tag and </li> as closing tag. The
closing tag </ul> comes at the end of the listing.
<li></li> This is used to list each items in ordered or unordered lists.
<font></font> This sets font characteristics to text such as color,size etc
<table></table This is used to create a table
<br> This sets a line break. This tag has no closing tag.
<p><p> This represent a paragraph e.g.<p align=”center”> he went
home</p>
<img> This is used to represent HTMl images. For example <img
src=”[Link]”> or <img src= “/News/[Link]”>

Note: Some tags have attributes which give additional information about an element e.g.
<img src= “[Link]” alt= “Mr Nicolus Dausi” width=”106” height= “150”>
Note: HTML was improved to create a language for writing web pages called Extended
HTML which is commonly abbreviated XML( Extended Mark-up Language). XML allows users
to define their own tags other than just using the ones in HTML.
Note: HTML does not have declaration part (i.e. an instruction part that reserve memory
space to store input or output) and controls (i.e. tools used by the programmer to build the
visible objects on the program) as such HTML is not considered a programming language.
Note: Nowadays there are web programming languages that are used to enhance
interaction and dynamism of HTML web pages. These special web languages are called
scripting languages. They are used to write scripts that are added to HTML programs to add
special functionalities e.g. sliding pictures, calendars.
Note: A script is a small program code containing a list of commands that can be executed
automatically in one go.
Examples of web scripting languages are:

 Java Script
 Visual Basic script( VB script)
 Hypertext pre-processor (php)
Below is a sample HTML page (A simple structure of HTML for a page that displays Hello
World on a Browser)
Note: write the code in any text editor such as Notepad and save it with a file name with a
.html extension
11
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

To open Notepad:
 Click start button
 Choose All programs
 Click on Accessories and click Notepad
Type the HTML codes below:

<!DOCTYPE HTML PUBLIC <html>


[Link] <head>
<title> Greetings</title>
<html> </head>
<head> <body bgcolor=“blue”>
<title> Greetings</title> <h1><font color="yellow">Hello World</font></h1>
</head> <br> I would like to greet the following people:
<body bgcolor=“blue”> <ol>
<li>John Banda</li>
<h1>Hello World</h1> <li>Morris Mpunga</li>
</body> </ol>
</html> </body></html>

The title bar or browser will display “Greetings” and on the screen it will display “Hello
World”. You can also search the page by just typing Greetings of the search area.
Advantages (benefits) of High level programming languages
1) They are portable since the programs can be installed in many computers (i.e. they
are not computer specific).
2) They are user friendly (i.e. easy to use and to learn) since they use language close to
human language.
3) They are flexible and this promotes creativity and innovation of programmers which
helps to increase productivity in the work place.
4) It is easier to correct programs written in high level languages.
5) They enable faster creation of programs (i.e. takes limited time to code)

Disadvantages (Limitations) of High level programming languages


1. They encourage use of many instructions in a statement (due to their flexibility) and
the big size of these instructions result in slow program processing (execution).
2. High-level language programs require translation by interpreters and compilers to
convert to machine code form hence need more software resources.
3. They require large computer memory to be executed. Note: compiled programs have
big size hence need more memory.
Self-Test Questions:
1) Define the following terms:
a. Computer program(1m)
b. Computer Programming(1m)
c. Programmer(1m)
d. Programming language(1m)
e. Source code(1m)
f. Object code(1m)
12
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

2) Differentiate between
a. high-level and low level programming languages(2m)
b. Interpreter and compiler(2m)
3) Give and explain two examples of low-level programming languages.(4m)
4) Give any three examples of high-level programming languages(3m)
5) State any three advantages of high-level languages over low-level languages(3m)
6) Give any three advantages of machine language over high-level languages(3m)
7) Write the following in full(4m)
a. OOP
b. HTML
c. HTTP
d. BASIC
8) Define the term encapsulate as used in object oriented programming.
9) Explain why an executable file is unique compared to source file? Answer: because it
can be easily understood by a computer unlike source file.(2m)
10) Explain why early computers worked well with interpreters? Answer: because
interpreters do not store the program. Once translated it is executed immediately.

Program Development Process


Most students tend to have difficulties in analysing and providing a solution to a given
programming problem. Below are guidelines on how to solve a programming problem:
1. Clearly define what a program is to do.(i.e. problem definition) e.g. add two numbers
and give the answer.
2. Visualize the program running on the computer to identify the logic behind solving
the problem at hand. (This helps you develop a step-by-step design (i.e. set of steps)
for solving the problem called the algorithm)
3. Write a pseudocode version of the program (. i.e. a rough or sketch code of a
program written in a language close to humans before the actual coding). The
pseudocode is developed from the algorithm.
4. Check the pseudocode / program design for any logical flow errors.
Note: The act of testing/ checking an algorithm (program design) for logical errors is
known as dry running.
5. Write the program code on paper in programming language.
6. Check the program code you have written on paper for any syntax and logical errors.
7. Write the source code using a programming software such as Visual basic or C++
8. Compile the program and correct (debug) any errors displayed by the compiler.
9. Run the program and provide input data to test it for any logical errors.
10. Validate the results of the program (i.e. check the correctness of the results of the
program)

13
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

Program Development Life-Cycle (P.D.L.C.)


There are five phases that make up program development life-cycle (PDLC). These are as
follows:
A. Problem definition
B. Algorithm design
C. Program coding
D. Program testing and debugging
E. Program review and maintenance
Below is an explanation of each phase

A. Problem definition
This refers to the ability to identify a problem that needs to be solved using a computer
program.
A creative programmer identifies problems and opportunities in the society and seeks to
provide a computer-based solution to them. The software products we have are actually
results of some skilful programmers who were providing solutions to problems that
were there e.g. Calculating numbers manually which was tiresome and was resulting in
errors.

Situations that may motivate a programmer to develop a computer program:


 Problems or undesirable situations that are preventing individuals or
organisations from achieving their objectives.
 Opportunities to improve existing delivery of services e.g. new markets outside
the country may provoke the need for websites in order to communicate with
the customers.
 New directives given by management in a company or government to change
current state of affairs e.g. a directive to develop a software that can track
finances in order to prevent or end fraud of theft of public funds e.g. the Ifmis
Example:
Below is a sample problem: Finding sum of two numbers
Here a programmer has a responsibility to develop a program that can calculate the sum
of any two numbers. To do this, a programmer must think (conceptualise) the solution
by first understanding the problem, identifying the input, processing logic and finally the
expected output.
Note: A careful analysis of the problem reveals to the programmer the process (steps) as
well as hardware and software requirements for solving the problem called algorithm.

B. Algorithm design
An algorithm is a sequence or set of steps for solving a problem. After clear definition
and analysis of the problem you need to design steps for implementing a solution to the
problem. This step-by-step solution design is called an algorithm.
Below is an algorithm for the problem in our problem:
i. Input:
a. First number
b. Second number
14
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

ii. Process : the formula for calculating sum of two numbers in this case
Number1+Number2
iii. Output: Sum of two numbers
There are three ways of expressing steps in solution to the problem (three ways of
expressing your algorithm. You can use:
 Structured English statements
 Flowcharts (i.e. a graphical representation of an algorithm)
 Pseudo codes (i.e. a short or sketch form language representation of algorithm)
Some guidelines for writing pseudo code:
 Write one statement per line
 Capitalize initial keyword
 Indent to hierarchy
 End multiline structure
For example; a programmer may use any of the pseudocodes below to outline steps to
be followed by a computer to calculate the sum of two numbers:

Program : Calculate Sum of two numbers //: Calculate Sum of two numbers

BEGIN BEGIN
PRINT “Enter Number1”
READ “Number 1” GET a,b
PRINT “Enter Number2” ADD c=a + b
READ “Number2” PRINT c
Sum=Number1+Number2
PRINT Sum
END END

Explanation of the first pseudocode (one on the left)


 The algorithm above expressed in pseudocode starts with a declaration on the
task to be performed by the program.

 The phrases within the keywords BEGIN and END indicates the executable
statements for input, process and output. E.g. statement for input: READ
“Number1”. Statement for Process: Sum=Number1+ Number2 and statement
for output: PRINT Sum
Note: The following are common keywords used in pseudo code:
//: denotes a comment
BEGIN is the first statement while END is the last statement to be executed
INPUT, GET, READ are keywords used to input data
CALCULATE, COMPUTE are used for calculation of results of given expression
PRINT, OUTPUT, DISPLAY are used to display the output of the program

15
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

C. Program coding
This is the process of writing a computer program using programming language
software. E.g. Visual [Link], C++ e.t.c
This phase involves writing a code in programming language from the pseudocode.
Note: Nowadays programs are coded in high-level languages. Programs written in such
languages need to be first converted into machine readable form for them to be
executed by the computer.
To code your program with ease you need to understand some elements in high-level
programming languages.
Common elements in programming languages
The following are some elements or features that are common in high-level languages:
1. Reserved words
These are words that have special meaning and are only used for intended
(particular/special) purpose.
Examples of some reserved words in Visual [Link]:
 Const (stands for constant. This keyword is used to indicate a constant
value e.g. Pie in a program for calculating Area of circle)
 Dim (which stands for Dimension. This keyword is uses to declare a
reserved memory space for storage of input or output from a computer
program)
 As which is used to specify data type in a declaration statement(memory)
e.g. Dim PI As Double

2. Identifiers
These are programmer-defined symbolic names that are used to identify
elements such as variables and constants in a program. For example in a program
for calculating hourly pay; a programmer can use names like Wage, hours and
rate as identifier. See below
Wage= hours * rate

3. Operators
These are symbols that are used to perform arithmetic operations on values
(operands). In the statement: Wage= hours * rate the asterisk * is an operator
which will multiply hours by ones rate in order to get a wage to be paid.
Examples of operators are:+ for Addition, - for subtraction, / for division. * for
multiplication and = which is an assignment operator.
Other operators are ^ (caret) and MOD for getting an integer remainder.

4. Variables
A variable refers to a named location in computer’s memory for holding data.
Note: the content of a Variable may change during program execution unlike
constants whose content does not change.
For example: in a program for calculating area of a circle:
22
Pie is constant value = 7 while Radius is a Variable as it may change.
See the program for calculating Area of a Circle given any Radius in Visual
[Link] showing the Form Object and tools (textbox, buttons) on the Form.
16
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

First add the Tools that you will require to the form and define the properties
and settings of each tool such as tool name and text. See the table below for
guidelines:
Object or Control Tool Properties Property Settings
Form1(this is an object  Name  frmCalculatingArea
on which you insert (found under Design)
(does not appear on form
other objects or tools
but read by the processor)
such as Labels ,  Text
textboxes, command (found under  Area of a Circle
buttons) Appearance) (which appears on the form)
Label1 (this is a label
that places a control  Name  lblRadius
that explains contents (found under Design) (does not appear on form
of another control on a but read by the processor)
form such as contents  Text
of text box  Radius
(found under
(which appears on the
Appearance)
form)
Text1  Name  txtRadius
(this is a textbox) a tool (found under Design) (does not appear on form
that draws a box in
but read by the processor)
which a user provides  Text
data input (found under  (Blank) i.e. leave it blank
-Do the same for Appearance) as it will not display any
Tex2([Link] Area) text on it but typed data
Button1 (This is a tool  Name  cmdCalculateArea
that places a command (found under Design) (does not appear on form
on a form. Buttons are
but read by the processor)
used to interact with  Text
an application (found under  CALCULATE
program) e.g OK, Appearance) (which appears on the form)
CALCULATE, EXIT e.t.c

17
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

 Double-click the Button on the form CALCULATE and type the program
statements between Private Sub and End Sub

PublicClassfrmHello

PrivateSub cmdCalculate_Click(sender AsObject, e


AsEventArgs) Handles [Link]

Dim Radius As Integer


Dim Area As Double
Const PI As Double = 22 / 7
Radius = Val([Link]) 'Converts radius
Which is in a textbox [Link] to a numeric value
data type
Area = PI * Radius * Radius
[Link] = Str(Area)'Converts the
numeric value of Area to text data type(string) for
display in a textbox [Link]

EndSub
EndClass
 Then Click Start to run the program
 Provide it with the radius and click CALCULATE button to get the Area of Circle
D. Program testing and debugging
This involves checking whether the program is performing the tasks as required and
identifying any errors in the program. The errors in a program are called bugs.
Note: Debugging is the process of correcting errors that were identified in a program.

Types of Bugs (Types of errors) in a program


There are two types of errors that are encountered when testing a program namely:
i. Syntax errors (i.e. errors from improper use of language rules) e.g. grammar
mistakes, punctuation, improper naming of variables, misspelled identifiers or
keywords. These are detectable by a compiler or an interpreter unlike logical
errors.

ii. Logical errors (i.e. errors in the order in which the problem has to be solved)
Logical errors originate from poorly developed program design (algorithm)
Note: Logical errors are not detectable by the compiler or interpreter. When there
are logical errors in a program, the program still runs but it gives wrong output and
sometimes it may halt (stop) during execution.

Tracing (identifying) errors in a program


There is need to perform a careful step-by-step trace of input, processing and
output statements in order to identify errors in a program. One has to choose a
good test and debug strategy (method) in order to thoroughly test an algorithm or
program design) before coding it.

18
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

The best method to trace errors in a program design or algorithm is hand tracing.
This method involves thorough (full or detailed) testing of an algorithm or program
design on paper.

Steps involved in testing a program


There are three steps:
a. Dry-run (Desk Check)
This involves checking whether an algorithm or program has errors using a
trace table before writing it in the program editor (i.e. before coding it).
This practice involves going through an algorithm line by line exactly as a
computer would during execution. This helps the programmer identify
syntax and logical errors.

b. Debugging
This involves use of debugging utilities (in a program editor) to detect and
correct syntax errors before compiling the source code (programmers code)
into object code (machine code).
Note: Debugging is done once the program is written in the program editor
(found in programing language) while Dry-run is done before the program is
coded into a program editor.

c. Test data
This involves carrying out trial runs using test data to check for logical and
run-time errors. This is done after the program has compiled successfully.
When testing, you need to enter valid and invalid data to test whether the
program produces desired output.

E. Program review and maintenance


This refers to the process of continuously updating and fixing program errors after
installation. You need to continue fixing and updating the program until it reaches a
point where the program has to be replaced.
When you reach this point you stop maintaining it and the program development life
cycle starts all over again.

Characteristics of a good program


The following are characteristic of a good computer program:

1) Simplicity: A good program should be simple and easy to understand.


2) Integrity: In programming Integrity means that a program must produce reliable
and accurate data. You must therefore make sure that the mathematical
statements in the program are well tested for accuracy of output.

3) Efficiency: A good program should ensure faster execution speed and good
utilization of memory (i.e. it should have a good performance)

19
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

4) Clarity: A good program should have clearly written statements and comments
for easy readability.

5) Modularity: This means that a complex program should be broken down into
components known as modules, procedures or sub-programs.

Programming in Visual Basic


BASIC in programming is an acronym for Beginners All-purpose Symbolic Instruction Code.
There are many BASIC versions but their foundation is the original BASIC language that was
developed in 1964 by John G. Kemery and Thomas E. Kurtz at Dartmouth College.
Note: BASIC programming was introduced to help non-mathematicians learn programming
with ease.
There are many dialects of BASIC such as Microsoft Visual Basic (V.B.), Quick BASIC and Q-
BASIC e.t.c. There are also some versions of BASIC developed for creating applications
(programs) for smartphones and tablets e.g. smart BASIC, BASIC!, BASIC-II for iOS
devices(iphone Operating system devices), RFO BASIC and Mintoris BASIC for Android.
Note: Visual Basic is now a member of Microsoft software development suite known as
Microsoft Visual Studio.

Microsoft Visual [Link] (V.B. Net)


This is a GUI-based (graphical user interface based) programming language that is mainly for
developing applications meant to run on Microsoft Windows platforms such as Windows
7,8,10.
Main features of [Link]
i. It is an event-driven programming language
An Event in programming is a response that is generated by a program when the
user performs an action. For example, when a user right-clicks a mouse, the clicking
causes a program to display a menu on screen. Note: The response of a program
depends on the code of an event procedure. An event procedure: is a group of self-
contained statements that are executed by a computer when an action is performed.
ii. It has a collection of tools called Controls that are used by programmers to put
items of objects in a program
These controls are accessed by clicking icons on the Toolbox.
Examples of controls available in Visual Basic are:
 Labels
 Textboxes
 Buttons
 Menus and toolbars
 Status bars e.t.c.
iii. It has objects called forms that have a title bar at the top.
The programmer can add controls (such as buttons, menu bars, and textboxes) onto
a form when creating an application (program).

20
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

iv. It is an Object-oriented programming language implemented using a framework


known as .Net that enables applications developed in V.B. Net run on internet.

Basic Syntax of [Link] Program


Note: the term Syntax refers to the structured arrangement of items and [Link] is
an object-oriented programming language that consists of various objects that interact with
each other by means of passing messages to each other via methods (actions)
[Link] Programming Syntax basic Concepts
To work with VB. Net effectively, you must be familiar with the following concepts:

 Object: This is a visible entity of a class that has state (data or properties) and
behaviours. E.g. a student is an object as he or she has state or data such as first name,
surname, and age. The student also has behaviour or functions such as walking, eating
that is not in a class of plants.

 Class: This is a template or collection of behaviours (functions or methods) and state


(properties of variables) that define a set of objects. E.g. form 3 class is made of students
which are objects. These objects belong to a class of animals. They have the same state
and behaviour as such they belong to the same class.
Note: Objects that are only accessed by the program itself alone are said to be in a
private class while those objects that can also be accessed by statements of other
programs are said to be in a public class.

 Methods: A method is a definition of the behaviour or function to be performed by an


object to manipulate data (state)E.g. a method to add, subtract or delete data. Normally
a class defines methods that are common to all the objects that belong to that class. The
program statements are written in the methods to manipulate data. A group of
functions or methods are stored in a file called a Module.

 Variables: A variable in programming is a location capable of storing or holding data


temporarily within a program. In OOP, each object has its unique set of variables. The
variables are assigned values that are used in the execution of methods or functions.

 Comments: These are a set of statements that are used in the code to explain to other
programmers what a program or program statement does. Comments in [Link] start
with a single apostrophe e.g. ‘This program adds two numbers and displays the sum.
Note: Comments are ignored by the compiler during execution. They are important as
they help other programmers understand your program and its purpose.

Structure of Visual Basic (VB) Program


A [Link] program consists of the following:
 Namespace declaration (i.e. space for declaration of objects names. Note: No
two similar objects can have the same name. e.g. You cannot have two textboxes
with similar name, but you can have a textbox and a label having same name.
 A class or module
 Variables (i.e. declared memory locations for storing any data within a program)
21
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

 One or two procedures ( actions to be done on variables)e.g. add, subtract


 Executable statements (I.e. statements to be executed)
 Comments (i.e. Texts that explains the purpose of a program or a program
statement to other programmers). A comment starts with an apostrophe.

Getting Started with [Link] 2010 (or 2012)


 Click Start button
 Point “Microsoft Visual Studio”
 Select/choose Microsoft Visual Basic 2010 Express/2012. This starts Project
window. You can Start a new project or open an existing project
o Note: In Visual Basic, an application is referred to as a project because it
comprises of several files. In our case we will start from New Project for
the purposes of learning. To start a New project do the following:

i. Click New Project icon on the Start page window; several types of projects will be
displayed. E.G.
 Windows Form Application(for creating an application with windows user
interface(GUI)
 Console Application (for creating a command-line application)
Note: Windows Forms and Console Applications are two environments
used to develop Visual Basic Applications. For a start, we will learn
programming using form application.

ii. Select “Windows Form Application” them type the name of the application e.g.
Calculator or HelloWorld. This name will replace the default (automatic) name
Windows Application1.

iii. Click Ok button. This will display the Visual Basic Integrated Development
Environment (IDE) Note: Integrated Development Environment (IDE) refers to a
programming environment that provides programmers with several
programming features such as Source Code editor, debugger and compiler.

Below are the features of the Microsoft [Link] 2010/2012:


 Menu and Standard toolbar
Visual Basic Integrated Development Environment (IDE) has a menu bar
and standard toolbar at the top. These are used for creating, editing and
manipulating the objects of the application.

 Form(s)
This is an object on which you insert other objects such as textboxes,
command buttons, labels etc. A form as an object has state (properties)
and behaviour (methods). Note: Properties define the appearance of the
object while methods are actions that can be set to execute once you
click the mouse button e.g. show method makes a form visible on the
screen.

22
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

 Toolbox
This contains most of the tools that are required by the programmer to
create a program. The tools include command button, textbox tool, shape
tool, label etc. The toolbox mostly shows on the right of the form. If it is
not displayed do the following:
 Click View menu
 Choose Toolbox
Note: Click and drag each of
the tools to use them and to
add their controls on the
form.
Note: A control is an item of
an object that appears on the
form e.g. Save button is an
item of command button.
Below are some common controls on toolbox that one can include on a form when building
an application:
a) Button
This is a tool used to place a command button on a form. These buttons are used to
interact with an application e.g. Ok, Save, Exit, Close buttons.

b) TextBox
This tool is used to draw text boxes in which a user provides input.

c) Label
This tool is used to place a control or an instance that explains (describes) the
content of other controls on the form. E.g. a label Surname is used to describe a
textbox in which a person’s surname will have to be entered.
Surname Phiri
d) CheckBox
This control (tool) provides a way of choosing from multiple values by clicking on
check boxes on a form.

e) ListBox
This tool is used to display a list of items that a user can select from. E.g. when you
click on down arrow, a list of items pulls own from which you can choose an item.

f) ComboBox
This is a type of list box that combine the capability of a textbox and a list box
meaning that it displays a list of items to select from during program execution and
additionally items can be added into the combo box.

23
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

g) Date TimePicker: This tool is used to place a control on a form that a user can click to
display a dropdown calendar from which to choose current day, month or year in a
specified format.

h) VScrollBar: This is a tool for placing a vertical scrollbar for scrolling from top to
bottom.

i) HScrollBar: This tool is used to place a horizontal scrollbar for scrolling from left to
right.

j) Shape tool: This is a tool used to draw circles, rectangles and other shapes within a
form.

k) PictureBox: This displays an image.

l) SaveFileDialog: This tool displays a dialog box that lets the user specify a location for
saving a file.
Note: You can hover on each of the tools to display information about uses of the other
tools.
 Solution Explorer
This is a window that is displayed to the right of the Integrated Development
Environment (IDE) window. The Solution Explorer displays a list of objects associated
with the open project e.g. it displays forms and modules in open project.

Properties of objects and controls


Note: after placing instances of objects and controls, you must define properties of each
object such as name, color, caption etc.
To define properties of an object e.g. a button
i. Right-click the object e.g. Right-click a button, a form, a textbox etc.
ii. Choose Properties from the shortcut menu. The Properties window opens. You
can now go ahead and define the properties. E.g. Name of object under the
section Design, Text(to appear on the object), Font color, Back color under the
section Appearance

Important Note: When naming the objects in your program/application:


 There must be no spaces in the object name
 Each object name must be preceded with three-letter tag to identify the object
type. The three-letter tag identifying the type of object must be in small
letters(lowercase) e.g. frmHello

Type of Object/Control Three-letter Tag


forms frm e.g. frmHello
label lbl e.g. lblFirstName
textbox txt e.g. txtSurname
command button cmd e.g. cmdOK

24
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

 Code window
The code window is an environment where a programmer writes (types) the source
code that instructs the program what to do e.g. displaying Hello World on the form.
To display the code window:
i. Double-click an object i.e. a command button (e.g. cmdOK).
OR click the button object then click View menu and choose Code.
ii. The code window opens where you type a code. Type the code to be executed
between Private Sub and End Sub
Activity: Create a Hello World project

 Start Visual Basic .Click New Project


 Select Windows Form Application and type the name of the project e.g.
“HelloWorldApplication” in the name box.
 Draw a label, text box and command button as shown below:

 Change the Properties of the form, label, textbox and command button as below:

Object or Control Tool Properties Property Settings


Form1(this is an object  Name  frmHello
on which you insert (found under Design) (does not appear on form
other objects or tools
but read by the processor)
such as Labels ,  Text
textboxes, command (found under  Hello World
buttons) Appearance) (which appears on the form)
Label1 (this is a label
that places a control  Name  lblHello
that explains contents (found under Design) (does not appear on form
of another control on a but read by the processor)
form such as contents  Text
of text box  Greetings
(found under
(which appears on the
Appearance)
form)
Text1  Name  txtHello
(this is a textbox) a tool (found under Design) (does not appear on form
that draws a box in
but read by the processor)
which a user provides  Text
data input (found under  (Blank) i.e. leave it blank
Appearance) as it will not display any
text on it but data typed
or generated by a
command.

25
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

Button1 (This is a tool  Name  cmdOk


that places a command (found under Design) (does not appear on form
on a form. Buttons are
but read by the processor)
used to interact with  Text
an application (found under  Ok
program) e.g OK, Appearance) (which appears on the form)
CALCULATE, EXIT e.t.c

 Double-click the command button Ok to display the code window and type the code
below between Private Sub and End Sub

Public Class formHello


Private Sub CmdOk_Click(ByVal sender As [Link],ByVal e As
[Link]) Handles [Link]
‘This program displays
Hello World on a Form
[Link]= “Hello World”
End Sub

End Class
Below is an explanation of the HelloWorld code above:
o The first line is a comment that explains what the program does. The comment in VB
starts with an apostrophe.
o The second line is an assignment statement. It means take whatever is on the left of
the assignment operator (i.e. the equal sign =) and replace with what is on the right.
I.e. Hello World replaces txtHello textbox content.
Running a program
To execute a program and display the result, do the following:

a. Switch to form display –by clicking Form Design tab on the Project Explorer window

b. Click the Debugging button OR press F5 key. The program will be


executed.
Enter data and click the button to get the results. E.g. click Ok to display Hello World on
screen
To stop running a program:
o Click Stop button on the toolbar

Saving [Link] Project

Note: Saving V.B. Net project differs from saving a file in other application software because here
you are required to save several files as a Project and module files

To save a project (follow the steps already done when you were starting a project). When saving,
[Link] creates a folder in which project files will be stored.

26
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

To save a Form:

o Click File menu


o Click Save form As
o Type the name of the form
o The save as type is .vb file
o Click Save button.

Opening existing Project

To open VB project during startup:

 Click Open project button, a list of folders for each object is displayed
 Double-click the folder that contains the project you want to open
Note: select a file with an extension .vbproj e.g. [Link]

Adding New Forms

A VB program may require more than one form. To add a form to a project:

 Click “Project” menu then choose “Add Windows Form”


 Click the Windows Form Icon and then
 Click Add button after typing the form name in the Name box and the file extension e.g.
[Link]

Comments, Declaration and Executable statements


A visual basic program is made of the following building blocks:

 Comments
 Declarations
 Executable statements

Comments

 These are a set of statements that are included in the program to explain the purpose of the
program to other programmers. They are also included as a reference in case of future
modification of the program.
A comment must start with an apostrophe (‘) followed by the comment statement e.g.
txtSum=Str(Sum)
‘displays the sum of numbers in the textbox
Note: comments are not executed when the program runs

Declaration

 This is an instruction that is given to reserve memory (space) enough to store input or output
from a computer program.

Note: for a computer to reserve enough memory for input or output, you need to specify the
type of data to be held in specific memory address. Memory locations whose content may
change during program execution are presented using a variable. A variable is a name or
identifier that is used to represent a memory location whose content may change as the
program is executing.

Note: When you declare a variable, the computer sets aside memory space to hold a value in the
variable.

27
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK

Rules to follow when declaring variable

 The variable name must not be more than 255 characters


 The variable name must start with a letter.
 Variable may be declared using lower or uppercase since VB is not case sensitive.
 A variable name must not be a reserved word (i.e. a key word that has a special meaning or
function in VB). Note: Reserved words cannot be used for any other purposes e.g. Dim, Case,
Else are reserved words.
 Variable names should not have spaces. If there are two words then use underscore to combine
them but each word must start with capital letter.

Note: in Visual basic, a variable is declared using the reserved word Dim which is short form for
Dimension. This reserved word is used to associate a variable with specific data type. See the
data types below:
o Integer i.e. whole number. Its suffix is %
o Long integer e.g. over 2 billion. Its suffix is &
o Single (i.e. a real precision) its suffix is !
o Double (i.e. fraction) its suffix is #
o String ( characters enclosed in quotation marks) its suffix is $
o Boolean (true or false)
E.g. to declare Sum as whole number type Dim Sum As Integer OR Dim Sum As %

Note: The shortening with suffix helps to make life easy and saves time when programming.
You can also declare constants. Note: use Const keyword at the beginning of the variable e.g.
e.g. Const Pi As Integer

28

You might also like