Msce Computer Notes 2022-Programming
Msce Computer Notes 2022-Programming
1
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK
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]
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
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.
MOV AX , 9
ADD AX , 10
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.
5
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK
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
6
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK
5 End.
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).
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.
Module HelloWorld
End Module
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
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.
End Class
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:
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)
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.
13
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK
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.
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
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
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.
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.
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.
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.
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.
20
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK
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.
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.
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.
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.
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.
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
Change the Properties of the form, label, textbox and command button as below:
25
2022 MSCE COMPUTER STUDIES NOTES-PROGRAMMING BY A.D. CHONA-BRAINS MUST WORK
Double-click the command button Ok to display the code window and type the code
below between Private Sub and 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
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:
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]
A VB program may require more than one form. To add a form to a project:
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
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