My Notes
My Notes
Just as human beings communicate with each other through the use of
languages such as Filipino, English, or Chinese, programmers use a variety of
special languages called programming languages, to communicate with
the computer. The instructions in a program are stated using computer language
such as C, C#, Visual BASIC, JAVA and among others. Natural languages such as
English and Filipino are not suitable to use in computer programming because of
their ambiguity and lack of precision.
What you want the
computer to do
What you have What you need
Programming Languages
Writing a computer program requires the use of a programming
language that the computer can understand.
The use of very high – level problem – oriented languages require only little
training in programming because they are designed to solve specific problems. To make
computer programming even a lot easier, programming is being developed to
approach that level of our natural languages.
HIGH
HIGH –– LEVEL
LEVEL Natural
Natural Language
Language Fifth
Fifth generation
generation
(nearer
(nearer man)
man)
Problem
Problem –– Oriented
Oriented Language
Language Fourth
Fourth generation
generation
Procedural
Procedural Language
Language Third
Third generation
generation
Assembly
Assembly Language
Language Second
Second generation
generation
LOW
LOW –– LEVEL
LEVEL
(nearer
(nearer machine)
machine) Machine
Machine Language
Language First
First generation
generation
Computer
Computer Hardware
Hardware (Machine)
(Machine)
Computer Programming I
1
Determine the…
a. Desired output. Ask what output information you want the
computer program to generate.
b. Input data. Ask what input data are available for processing by
the computer program.
c. Processing requirements. Knowing the input data and the
desired output information, you now define what processes are to be
performed on the input data to arrive at the desired output
information.
2. Program Design. We plan and create the solution to the problem according to the
program specifications we made in step 1.
The solution to the problem may be expressed in one of the following forms:
a. Pseudocode. This is a step – by – step narrative description of the solution in short
sentences. Pseudocode is an outline form of the program.
3. Program Code. In this step, we write the program solution using a computer
programming language. The coding or writing of the program is according to the program
design we made in step 2.
It is necessary to code the program solution in a form of language that the computer can
understand. Examples of computer programming languages are C, C#, Visual BASIC, JAVA
Computer Programming I
1
desk check
manual check
4. Program Test Perform...
translation attempt
test with sample data
Users
5. Program Documentation Write procedure for...
Operators
Programmers
Errors
inefficient or ineffective
6. Program Maintenance Adjust for... operations
non-standard code
changes over time
Examples: Given the problem below, identify the problem statement (Input-Process-Output)
Problem 1: Given two scores obtained by a student in two different exams, compute the
average of these scores.
Problem Statement:
following problem. Compute the average of the two score obtained by each of the 100
students.
Problem Statement:
This very simple example only serves to illustrate the process of problem analysis. You don’t
really need a computer program to perform the task. The real value and power of programs will
become apparent in cases where voluminous data are to be processed more quickly and more
accurately.
What is Data?
DATA is defined as a set of symbols, which are used to represent objects, events,
activities, quantities, and other entities, and which can be processed or manipulated by manual or
automatic means. Today, the computer is widely used in processing data.
Examples of DATA
Employee ID number Color Name
SSS ID Number Sex Address
Customer ID Number Product code Course
Student ID Number Unit price Amount
Tax ID Number Weight Reorder level
Processed data is often called information. Information is characterized as those that are
useful in making decisions. However, current use even in technical terms allows the interchanging of
the words data and information.
DATA INFORMATION
Example 1: Enumerate all data that can be identified or inferred from the following description of a data
Computer Programming I
1
processing application.
A teacher of Asian College of Technology maintains on the computer the records of all his/her
students. These records include their complete names, course and semester level, scores in the
prelim, pre-midterm, midterm, pre-final and final exams, average of the 5 major exam scores,
midterm and final grades, their sex (male or female), and remarks on whether they passed or
not.
List of data:
Last name, first name, middle initial of student
Course, semester level
Prelim score, pre-midterm score, midterm score, pre-final score, final score
Average of 5 major exam scores
Midterm grade, final grade
Sex (male or female), remarks (passed or failed)
Example 2: Enumerate all data that you can identify or infer from the following description of a data
processing application.
ACT-HRM Restaurant is to use an information system to provide better control of all restaurant
operations and to give better service at the same time. Instead of writing down orders on a
paper pad and bringing a copy to the kitchen, the waiters would use a hand-held terminal to key
in the orders at the tables. When the order is complete, they would press a button and the order
would be transmitted into the kitchen by radio waves. At the end of the meal, the bill would be
computed and printed out automatically, including a complete listing of every item ordered. By
having these data on the computer, it would be easy to maintain sales statistics about which
items were ordered when. The sales data would also link directly to the inventory system used
for ordering food, which should help in purchasing.
List of data:
Waiter code number food code number
Waiter name food description
Order code number food sale unit
Order date food unit price
Order time food ingredient code
Order entry code food ingredient description
Order entry quantity food ingredient inventory unit
Order entry total amount food ingredient inventory level
Order total amount food ingredient reorder level
Order service time food ingredient cost
Tip amount food ingredient supplier
TYPES of DATA
Computer Programming I
1
A numeric data has a value consisting of digits (0 – 9), positive sign (+),
negative sign (-), or decimal point (.). Currency symbols (ex. $ and ₱) and the comma
sign (as in thousand and millions) are not included in a numeric data.
A Boolean or logical data has a value of either true (.T.) or false (.F.).
A constant is a piece of data whose value do not change during the execution of a program.
A variable is a piece of data whose value could change during the execution of a program.
Computer Programming I
1
Example 1: Study the table of data with the corresponding data type that follows.
Example 2: Study the table of data with the corresponding data type that follows.
VARIABLES OR IDENTIFIERS
Technically, variables are a symbolic name associated with a value and whose
associated value may be changed. Variables somewhat serve as containers, which may contain
values which are constant and which may be utilized, while the program is running or being executed.
3. No embedded space/s can be placed between the characters. This means you
cannot separate words in a name by a space. Normally you concentrate (append) second
and subsequent words onto the identifier by capitalizing the second word.
4. Being descriptive is helpful. Use meaningful names that represent the item being
described. If the identifier is used to reference a person’s age, call it age, not x. The more
descriptive your identifiers are, the easier it is for you and the others to read your code.
Computer Programming I
1
1. Pascal Case – the first letter in the identifier and the first letter of each subsequent
concatenated word are being capitalized.
Ex. FamilyName, FirstName, MiddleInitial
2. Camel Case/Hungarian Notation – the first letter of an identifier is lowercase and
the first letter of each subsequent concatenated word is capitalized.
Ex. familyName, firstName, middleInitial
3. Uppercase – variables or identifiers use capitalization of all of its
characters
Ex. FAMILYNAME, FIRSTNAME, MIDDLEINITIAL
variable = value
The data type of the variable will follow the data type of the value assigned to it.
Example 2: Store the average of midterm and final in the variable average.
average = (midterm + final)/2
Example 3: Store the string value “Juan dela Cruz” in the variable name.
name = “Juan dela Cruz”
Example 4: Assign the logical value of .F. (false) to the variable done.
done = .F.
Computer Programming I
1
ALGORITHM
What is an algorithm?
Algorithms consist of steps, which describe how the
data are to be manipulated by the computer program.
PROPERTIES of an ALGORITHM
The following are properties of an algorithm:
4Pseudocode. The steps of achieving a certain objective are being presented through text.
Sample Pseudocode:
if (AGE < 18)
DISPLAY “MINOR”
else
DISPLAY “LEGAL AGE”
Computer Programming I
1
Example: Develop an algorithm to compute and display the tuition fee of a student, given the
number of units enrolled and the tuition fee rate per unit.
Problem Statement:
Input data - number of units enrolled (UNITS), tuition rate per unit (RATE)
Output information - total tuition fee
Processes - Multiplication of number of units enrolled and tuition rate per unit
Pseudocode Flowchart
START
START
END
END
Computer Programming I
1
ARITHMETIC OPERATORS
OPERATOR OPERATION PRIORITY
Multiplication * 1
Division / 1
Modulus % 1
Addition + 2
Subtraction - 2
The computer evaluates the operator with the highest priority (1). For the same
priority, evaluation is from left to right. Operations inside parentheses () if any are
evaluated first.
10 + 6 – 11
1 2
(1) 10 + 6 = 16
(2) 16 – 11 = 5
Computer Programming I
1
2+9/3*5
3 1 2
(1) 9/3=3
(2) 3 * 5 = 15
(3) 2 + 15 = 17
2+4%2
2 1
(1) 4%2=0
(2) 2+0=2
Example 4: The computer will evaluate the following arithmetic expression as
((2+2)*2)%10
equation with parenthesis, the inner equation within the parenthesis should be
performed first.
Therefore,
((2+2)*2)%10
1 2 3
(1) 2+2=4
(2) 4*2=8
(3) 8 % 10 = 8
Computer Programming I
1
PROGRAMMING STRUCTURES
The program could get so messy and difficult to understand and maintain if it will
not use the structured approach.
When writing algorithms, some or all of the following situations may arise:
Algorithms written with only the sequence, selection, and repetition structures are called
structured algorithms. The process of writing such algorithms is called structured
programming and the resulting programs are called structured programs.
Sequence Structure
Pseudocode Flowchart
Task 1
Task 1
Task 2
Task 2
Selection Structure
Pseudocode Flowchart
if (condition)
Task
T F
conditio
n
Task
while (condition)
F
{
Task conditi
on
} T
Task
While condition is still true (T), Task 1 is repeatedly performed. Condition is tested first before Task is
performed.
The selection structure and the repetition structure may also be viewed as tasks in a sequence
structure. Hence a program may be taken as a sequence of a mixture of sequence, selection and
repetition structures.
Note that there is always 1 entry point and 1 exit point in each structure. This gives us a
way of writing our programs in modules.
Computer Programming I
1
SEQUENCE STRUCTURE
Example 1: Input two numbers, compute and display the sum, difference, product and quotient.
Problem Statement:
NUM1,
Input NUM1 and NUM2 NUM1,
NUM2
NUM2
Example 2: 1 Kilometer has 1000 meters. Given a distance in meters, compute and display the
END
END
Computer Programming I
1
distance in kilometers.
Problem Statement:
Pseudocode Flowchart
START
START
METERS
METERS
Input METERS
Display DIST
DIST
DIST
END
END
RELATIONAL OPERATORS
OPERATOR OPERATION PRIORITY
== Equal to (the same as) 1
Equality
!= Not equal to (not the same as) 1
Greater than (more, bigger, larger, higher, longer
> 1
than, exceeds, over)
Less than (smaller, fewer, lower, shorter than,
Relational < 1
below)
>= Greater than or equal to (at least) 1
<= Less than or equal to (at most) 1
The computer evaluates first the operator with the highest priority (1). For the same
priority, evaluation is from left to right. Operations inside parentheses () if any are
evaluated first.
Example: The following are examples of relational expressions and their use.
Logical expressions and relational expressions are the same in the sense that
they both represent true or false logical conditions.
LOGICAL OPERATORS
OPERATOR OPERATION PRIORITY
.NOT. Negation 1
.AND. Logical Multiplication 2
.OR. Logical Addition 3
The computer evaluates first the operator with the highest priority (1). For
same priority, evaluation is from left to right. Operations inside
parentheses () if any are evaluated first.
.NOT. reverses the logical value of a relational expression while .AND. and .OR. are used to
combine two or more relational expressions into a single true or false logical condition.
Please study the table that follows:
SELECTION STRUCTURE
One of the most significant structures in
Computer Programming I
1
programming is the selection structure. In selection structure, users are instructed to choose
from the given options.
There are several ways to represent these choices or options and these are the following:
One – way selection structure
Two – way selection structure
Multiple selection structure
One – way selection structure is best illustrated in the given flowchart that follows:
Pseudocde Flowchart
if (condition) T
F
Task conditio
n
Task
Problem Statement:
Pseudocode Flowchart
START
START
Input NUM
if (NUM>=0) NUM
NUM
display “Positive Number”
F
NUM
NUM>=>=
00
T
“Positive
“PositiveNumber”
Number”
END
END
Pseudocode Flowchart
if (condition)
Task 1
else T F
conditio
Task2 n
Task 1 Task 2
As shown in the flowchart, a single condition is coupled with two tasks – one task (Task 1) is to be done
when the requirement is met; otherwise, another task, which is Task 2, will be executed.
Example: Write an algorithm that determines if the input number is NON_NEGATIVE or NEGATIVE.
Problem Statement:
Pseudocode Flowchart
START
START
Input NUM
NUM
NUM
if (NUM>=0)
display “NON_NEGATIVE”
else T F
NUM
display “NEGATIVE” NUM>=>=
00
“NON_NEGATIVE” “NEGATIVE”
“NON_NEGATIVE” “NEGATIVE”
END
END
Study the flowchart below which supports the multiple selection structure.
Pseudocode Flowchart
if (condition 1)
T F
Task Condition
else if (condition 2) 1
Example: Write a program that examines the value of a variable called Temp. Then Display the
following messages depending on the value assigned to Temp.
Temperature Message
Less than 0 ICE
Between 0 and 100, inclusive WATER
Exceeds 100 STEAM
Computer Programming I
1
Problem Statement:
Pseudocode Flowchart
START
START
Input Temp
Temp
Temp
if (Temp < 0)
display “ICE”
else if (Temp > 100) Temp < T “ICE”
Temp < “ICE”
0
display “STEAM” 0
F
else
display “WATER” T
Temp >
Temp > “STEAM”
100 “STEAM”
100
F
“WATER”
“WATER”
END
END
INCREMENTATION and DECREMENTATION
INCREMENTATION or to increment the value of a numeric variable means
to increase the value stored in that variable by a certain specified number. This is
expressed as:
variable = variable + increment
Computer Programming I
1
This means that the new value of the variable is equal to its previous value plus the increment.
Example: Increment the value of age with the value of 5.
age = age + 5
STRUCTURED PROGRAMMING
REPETITION STRUCTURE
Repetition structure is a repeating process while condition is
still true (.T.). A very good example of it is when you try to hop 100 times
using a jumping rope. You reach the count of 100 as you are doing the same
thing and that is hopping or jumping. You may also refer to a runner. As the
athlete goes around the oval, he is doing the same thing and that is running
around the oval.
Repetition structure is best illustrated in two ways and these are:
Pre test
Post test
TESTING CONDITION BEFORE TASK
Pretest repetition structure is a repetition structure wherein the condition is
being evaluated first before a certain task is executed. Pretest repetition structure is illustrated
below:
Computer Programming I
1
Pseudocode Flowchart
while (condition)
{
Task
F
} conditi
on
T
Task
The conditional expression, sometimes called the loop condition, is a logical condition to
be tested. It is enclosed in parentheses and is similar to the expressions you use for selection
statement. The conditional expression must be Boolean deciding true or false. An interpretation of
the While statement is “while condition is true, perform statement(s).”
The statement following the conditional expression makes up the body of the loop. The body of
the loop is performed as long as the conditional expression evaluates the true. Like the selection
construct, the statement following the conditional expression can be a single statement.
statement(s)
}
The For Loop executes a statement or a block of statements repeatedly until a specified
expression evaluates to false. The For Loop is useful for iterating over arrays and for sequential
processing. Unlike many other kinds of loops, such as the While Loop, the For Loop is often
distinguished by an explicit loop counter or loop variable. This allows the body of the For Loop (the
code that is being repeatedly executed) to know about the sequencing of each iteration. For Loops
are also typically used when the number of iterations is known before entering the loop. For Loops
are the shorthand way to make loops when the number of iterations is known.
Example: Write an algorithm that generates the given sequence numbers. (Pretest)
Sequence numbers
1 2 3 4 5
Pseudocode Flowchart
START
START
CTR = 1
CTR = 1
CTR = 1
while (CTR <= 5)
F
{ CTR <=
CTR <=
5
display CTR 5
T
CTR = CTR + 1 CTR
CTR
}
CTR = CTR + 1
CTR = CTR + 1
END
TESTING CONDITION AFTER TASK END
Post – test repetition structure is a repetition structure wherein the task is being
executed first before the condition is evaluated. Post – test repetition structure is illustrated in the
flowchart that follows:
Pseudocode Flowchart
Task
T
conditio
n
F
Computer Programming I
1
do
{
Task
}
while (condition)
Example: Write an algorithm that generates the given sequence numbers. (Post-test)
Sequence numbers
1 2 3 4 5
START
START
CTR
CTR==00
CTR
Pseudocode CTR==CTR
CTR+Flowchart
+11
CTR = 0
do CTR
CTR
{
T
CTR
CTR<=4
<=4
END
END
Computer Programming I
1
CTR = CTR + 1
display CTR
}
while (CTR <= 4)
COUNTER-CONTROLLED LOOP
This is when the loop may be executed a specified number of times.
With a counter-controlled loop, a variable simulating a counter is used as the
loop control variable to keep track of the number of iterations.
Many programs you develop require that you write one or more
Computer Programming I
1
statements that must be performed more than once. When you know the number of times the
statements must be executed, you can create a counter-controlled loop.
Example:
Pseudocode Flowchart
ctr = 1
ctr = 1
if (ctr <= 5)
{ F
ctr <= 5
ctr = ctr + 1
} T
ctr = ctr + 1
ctr
In this example, the initial value of ctr is literally 1. After initialization, a condition box follows
which sets the value as to how many repetitions are to be executed.
SENTINEL-CONTROLLED LOOP
Pseudocode
ROW = 1
PRODUCT = 0
while (ROW <= 10)
{
COL = 1
while (COL <= 10)
{
PRODUCT = ROW * COL
Display PRODUCT
COL = COL + 1
` }
ROW = ROW + 1
Flowchart
START
START
ROW
ROW==11
PRODUCT
PRODUCT==00
ROW
ROW<=<=
10
10
T END
END
COL
COL<= 110
COL
PRODUCT
COL ===<=
PRODUCT
COL COL
ROW110++*1*1COL
=COL
PRODUCT
PRODUCT
COL ROW COL ROW
T F ROW==ROW
ROW++11