Program Logic Formulation
Program Logic Formulation
Programming Languages
● Programming Language
– a standardized communication technique for
expressing instructions to a computer
● Count ← Count
● next name in
NameList
● Display Count
● Stop
More on Algorithms
FINITE – The problem must be solvable in a
finite number of operations
UNAMBIGUOUS – Each instruction must
have a unique interpretation.
ORDERED – Each instruction must have a
predecessor (except the start)
DEFINED INPUT/OUTPUT
pshscs2.tripod.com/files/plf.pdf
4. Coding and Debugging
●After constructing the algorithm, it is now
possible to create the source code. Using
the algorithm as basis, the source code
can now be written using the chosen
programming language.
● Debugging
– The process of fixing some errors (bugs)
in your program
pshscs2.tripod.com/files/plf.pdf
Six Steps in Problem Solving
1. Identify the problem.
2. Understand the problem.
3. Identify alternative ways to solve the
problem.
4. Select the best ways to solve the
problem from the alternatives.
5. List instructions that enable you to solve
the problem from the selected method.
6. Evaluate the solution.
pshscs2.tripod.com/files/plf.pdf
Seven Basic Elements of
Programming
• Data: Constants, Variables
• Input: reading of values from input devices
(keyboard, disk drives, etc…)
• Output: writing of information to any output
device (disk drives, printer, etc…)
• Operations: comparing values, assigning
values, combining values.
• Conditions / Selections: IfThenElse, Switch
Case,
• Loops / Iterations: While, doWhile, fordo,
repeatuntil
• Subroutines / Modules: functions,
procedures pshscs2.tripod.com/files/plf.pdf
Problems that can be solved on
computers:
Computational
Problems involving some sort of
mathematical processing
Logical
Involve relational or logical
processing
Repetitive
Involve repeating a set of mathematical
and / or logical instruction.
pshscs2.tripod.com/files/plf.pdf
Logical Control Structures
• Elementary building blocks of structured
programs
• Statements that control the order in which
other program statements are executed
• Refer to different ways in which program
instructions may be executed
pshscs2.tripod.com/files/plf.pdf
Logical Control Structures
1. Sequence
2. Selection / Decision
3. Iteration / Loop
4. Case
pshscs2.tripod.com/files/plf.pdf
SEQUENCE
• Instructions are executed in the order in which
they appear
• “Stepbystep” execution of instructions
pshscs2.tripod.com/files/plf.pdf
SELECTION / DECISION
• A logical control structure that execute
instructions depending on the existence of a
condition
• Sometimes called an “IfThenElse”
logical control structure
pshscs2.tripod.com/files/plf.pdf
ITERATION / LOOP
• A logical control structure indicating the repeated
execution of a series of steps (or instructions).
pshscs2.tripod.com/files/plf.pdf
CASE
• A logical control structure that is used when
there are numerous paths to be followed
depending on the content of a given variable.
pshscs2.tripod.com/files/plf.pdf
Example: Program Development
Flow
pshscs2.tripod.com/files/plf.pdf
DATA
Constant
–A value that never changes during the
processing of all the instructions in a
solution.
Variable
–The value of a variable does change
during processing.
– Also called as “identifier”
pshscs2.tripod.com/files/plf.pdf
CONSTANT
• Can be any type of data: numerical,
alphanumeric (or character), or special symbol
• Two (2) kinds of constants
–Literal: refers to the actual value itself (e.g.
3.1416, “pshs”)
–Named: uses a name or alias to represent an
actual or literal value (e.g. PI, school_name)
pshscs2.tripod.com/files/plf.pdf
VARIABLE
• Can be categorized by the kind of data it
can hold.
• They must hold data that are of the same
type, otherwise a mismatch error will
occur.
• Can be any type of data: numerical,
alphanumeric (or character), logical, or
special symbol
pshscs2.tripod.com/files/plf.pdf
Example: Constants & variables
on the computer
• Constants
8935084, 1.5, 3.1416, “pshs”, “*”
• Variables
AGE=12, PRICE=99.99, CITY=“Quezon
City”, Student_Name=“Pisay dela Cruz”,
ZIP_CODE=“1008”, MARK=“A”,
End_of_File=False
pshscs2.tripod.com/files/plf.pdf
DATA TYPES
• Numeric
• Character
• Logical
• Date / Time
pshscs2.tripod.com/files/plf.pdf
Numerical Data
• Include all types of numbers (i.e., integers, non
integers)
• The only data type that can be used in
calculations
• Subtypes:
– Integer: negative numbers & whole
numbers
– Real: decimal numbers
– Float: numbers in exponential /
scientific form
pshscs2.tripod.com/files/plf.pdf
Character Data
• Consists of all numbers, letters, and
specialcharacters available to the
computer (#, &, *, +, , 09, AZ, az) and
placed within quotation marks.
• Cannot be used for calculations even if
they consist of only numbers.
• String: means a string of characters
• Concatenation: means joining of two or
more pieces of character or string data
pshscs2.tripod.com/files/plf.pdf
Logical Data
• Consist of two pieces of data in the data
set
– the words TRUE and FALSE.
• Logical data are used in making a yes or
no decision.
pshscs2.tripod.com/files/plf.pdf
OPERATORS
• Are the data connectors within
expressions and equations.
• They tell the computer how to process the
data.
• They also tell the computer what type of
processing needs to be done
(i.e., mathematical, relational, or
logical).
pshscs2.tripod.com/files/plf.pdf
Types of operators used in
calculations & problem solving:
1. Mathematical
2. Relational
3. Logical
pshscs2.tripod.com/files/plf.pdf
Mathematical Operators
Include the following:
– Addition +
– Subtraction
– Multiplication /
*
– Integer Division \
– Modulo
– Division Division
– MOD
Powers ^ or **
– Functions FunctionName (parameters)
pshscs2.tripod.com/files/plf.pdf
Relational Operators
Include the following:
– Equal to =
– Less than <
– Greater than >
– Less than or equal to <=
– Greater than or equal to >=
– Not equal to <>
or !=
pshscs2.tripod.com/files/plf.pdf
Relational Operators
• A programmer uses relational operators
to program decisions.
• The resultant of a relational operator is
the logical data type TRUE or FALSE.
• Are also used to control repetitive
instructions called loops.
pshscs2.tripod.com/files/plf.pdf
Logical Operators
• Are used to connect relational
expressions (decisionmaking
expressions) & to perform operations on
logical data.
pshscs2.tripod.com/files/plf.pdf
Expressions & Equations
pshscs2.tripod.com/files/plf.pdf
PseudoCode Description
Format
Format Explanation Format Explanation
This is the where {comment} Used for comments.
procedures, variables,
Declaration types, etc., are declared Conditional Indicates selective
section expression processing.
● Proce If the conditional
ss 1 expression is true,
Declares the names, Process 1 is
○ types, etc., of procedures ● Proce executed; if the
, variables. ss 2 conditional expression
This is the area where is false, Process 2 is
processing is described. Conditional Ienxdeicautesd.a
Processing expression loop with the
section termination
● Proce condition at top.
ss The process is
executed as long as
Assigns the value of the
●Variable←Expressio expression to the
n variable.
Example 1 “2 Branch”
When mathematics score is less than 60, the student gets
a failing mark
Mathematics Exam.
Declarati ○Eng.SCORE
Integer Variable: Math.SCORE,
on
English Exam. section
Yes
● Math.SCORE ← Mathematics
Math. SCORE < Mark
Yes ● Eng.SCORE ← English Mark
60 Processin
No Math.Score < 60
Eng. SCORE < g
60 section
Eng.Score <
No
Fail 60
● Fail
● Do Nothing
● Do Nothing
Example 3 “3 Branch”
When mathematics score is less than 60, or English
score is less than 60, the student gets a failing mark.
● Fail
● Do Nothing
● Do Nothing
Example 4 “3 Branch”
When mathematics score is greater than equal 80
evaluation A, when score is less than 80 and greater than
equal 60 evaluation B, when score is less than 60 then
evaluation C.
Mathematics Exam.
Declarati ○ Integer Variable: Math.SCORE
80≦Score Score < on
section
60 Math. SCORE
● Math.SCORE ←
60 ≦Score < Mathematics Mark
Math.Score≧80
80 Processin
● Evaluation A
Evaluation A g
section
Evaluation B Math.Score≧60
Evaluation C ● Evaluation B
● Evaluation C
Example 5 “Repeat”
Variable x has an initial value 0. And add 1 to x. It repeats
addition while x is not 5 (until x becomes 5).
X←0
Declarati ○ Integer Variable: X
on
section
● X←
No 0
X≠5
Processin X ≠5
Yes g
● X=X+1
section
X ← X+1
Example 6 “Loop”
Variable x has an initial value 0. And add 3 to x. It repeats
addition 5 times.
X ← 0, Y ← 0
Declarati ○ Integer Variable: X, Y
on
section
● X←
No 0
Y < 5 ● Y←
Processin
Yes g 0
section Y < 5
X←X+3 ● X=X+3
● Y=Y+1
Y ←Y+
1
X ← 0, Y ← 1
Declarati ○ Integer Variable: X, Y
on
Loop
section
till Y > 9 ● X←
0
X←X+Y Processin
● Y←
g 1
Y ←Y+ section Y≦9
2 ● X=X+Y
Loop ● Y=Y+2
Example 8 “Loop Array”
There are four money savings boxes. Their names are
"week1", "week2", "week3", and "week4".
Get the total amount of money at the end of the
month.
Save(1) ← 120, Save(2) ← 340 ○ Integer Variable: Sum, i
Save(3) ← 230, Save(4) ← 180 Declarati
● i=i+1
i ←i+
1
Example 9 “Loop Array & Branch”
There are the
Calculate foursum
money savings
of odd numberboxes.
1, 3,Their name
5, 7, and 9. are
"week1 to week4". Only when the amount of savings on the
weekend is 200 or more is it added to Sum.
No ● Sum ← 0
● i← 1
i ≦ 10
Processin
Yes g
section
Sum ← Sum + i i ≦ 10
i ←i+ ● S
1 u
m
S
u
Practice 2
Given an array T(n) which
contains some
values stored in ascending
order, look for a given value in
this array by using the binary
search method.
Before Practice 2
low ← ------ mid -------→ high
T(n1 T(n
T(1) T(2) T(3) … … …
) )
low ← ---
→
low ← ----→ high
high data <
T(mid) Target found
data >
T(mid)
data =
T(mid)
Practice 2
idx ← 0, low ← 1, high ← n
Declarati
section
○ Integer Variable:
idx, low, high, mid
Loop
on
low > high or idx ≠ 0
● idx ← 0
mid ← (high + Low)/2 ● low ← 1
● high ← n
No
data = T(mid) low ≦high and
Processin
section
Yes No idx = 0
● mid ←
data > T(mid) (low + high)/2
Yes
g
data = T(mid)
● idx ← mid
idx ← mid low ← mid + 1 high ← mid - 1
data >
T(mid)
● low ←
mid+1
Loop
● high←mid
1