Logic Formulation
Jhune Hay L. Mitra
Instructor 1
DCIT21
Programming Cycle
Problem Definition
Problem Analysis
Algorithm Development
Coding and Debugging
Algorithm
a step-by-step problem-solving procedure
finite sequence of steps for solving a logical or
mathematical problem
a specific set of well-defined, simple mathematical
and logical procedures that can be followed to
solve a problem in a finite number of steps
Algorithm
Guide to a good Algorithm
1. Specify each step or instruction exactly.
2. There must be a finite number of steps.
3. There must be an output.
Pseudocodes
generic way of describing an algorithm
without the use of any specific programming
language syntax
models and resembles real programming
code
while not at end of list Enter Age
compare adjacent elements If(age>18)
if second is greater than first printf(“\n Qualified to vote);
switch them else
get next two elements print(“Too young”)
if elements were switched
repeat for entire list
Flowchart
a chart that contains symbols referring to computer operations, describing how
the program performs
a graphic map of the path of control or data through the operations in a
program or an information-handling system
symbols such as squares, diamonds, and ovals represent various operations
thesesymbols are connected by lines and arrows to indicate the flow of data or
control from one point to another
Flowchart Symbols
Terminal block
Process block
Input/output block
Decision block
Initialization block
Connector
Flow Lines
Flowchart Symbols
Flow Lines
indicated by straight lines with arrows to show the direction of data flow
the arrowhead is sometimes not shown when the direction of flow is clear
used to connect blocks by exiting from one and entering another
Flowchart Symbols
Terminal block
ovals or rounded rectangles used to indicate the start and the end of a
module or program
an oval is labeled with the name of the module at the start
the end is indicated by the word end or stop for the top or Control Module
START
END
Flowchart Symbols
Initialization block
used for declaring / initializing variables
needed to solve a certain process
Declaration:
binding of an identifier to the information that
relates to it
stating a variable name to be used X = 10
Y = 25
Initialization:
to set (a starting value of a variable)
to prepare (a computer or a printer) for use; boot
to format (a storage medium, such as a disk)
Flowchart Symbols
Process block
the rectangle indicates a processing block
has one entrance and one exit
X=Y+Z
A = 18
B=A-2
Flowchart Symbols
Input / Output block
the parallelogram indicates input to and output operations
has one entrance and only one exit
Get X Display X
Flowchart Symbols
Decision block
the diamond indicates a decision
has one entrance and exactly two exits from the block
T F
Condition
Action when Action when
TRUE FALSE
Flowchart Symbols
Connectors
the circle is used as a connection point between two sections of a
flowchart that are not adjacent or closely located to each other
used to enhance readability
A
Flowchart: Example 1
Create a flowchart that displays your age five years from now.
Start
Age = 18
Age = Age + 5
Display Age
Stop
Flowchart: Example 2
Create a flowchart that will increase the value of
the number by 5 if it is greater than 10 and display
the result; otherwise no action shall be done Start
x=0
Get x
Yes No
x > 10?
x=x+5
Display x
Stop
Flowchart: Example 3
Create
a flowchart that displays the user’s name, birthday, age, gender, and phone
number. Your flowchart should display the variables with the following format:
Name: ??????
Age: ??
BDay: ?????????
Gender: ?
Phone#: ???????
Flowchart: Example 3
Answer: Start
Name=“”
BDay=“”
Age=0
Gender=‘ ’
Pno=“|”
Get Name, BDay,
Age, Gender, Pno
Display “Name:” Name
“Age:” Age
“BDay:” BDay
“Gender:” Gender
“Phone#:” Pno
Stop
Flowchart: Example 4
Createa flowchart that will compute for the Average score of a student based
on three quizzes. The quiz scores are integers that may range from 0 to 100 and
are entered by the user. The Average may be a decimal number.
Example:
Q1: 98
Q2: 79
Q3: 88
Ave is 83.33
Flowchart: Example 4
Answer:
Start
Set Sum, Ave,
Q1, Q2, Q3 to 0
Get Q1, Q2, Q3
Sum=Q1+Q2+Q3
Ave=Sum/3
Display Ave
Stop
Flowchart: Exercise 1
Create a flowchart that computes for the circumference of a circle.
Assume the following values:
Radius = 7.23
Pi = 3.1416
Your flowchart should be able to show the value of the circumference.
You can compute the circumference by using the formula: 2piR.
Flowchart: Exercise 2
Draw a flowchart for a college’s admissions office with the following
specifications:
Allow the user to input the student’s high school GPA and admission test score
Print the message “Accept” if the student has a grade point average of 3.0 or above
and an admission test score of at least 60
If the student does not meet either of the qualification criteria, print “Reject”
Flowchart: Exercise 3
Create a flowchart that will increase the value of the number by 5 if it is
greater than 10 and display the result. Otherwise, multiply the value by 5
and display the result.
Conditional Statements
Conditions
are statements that result to a Boolean value.
Boolean value
may either be a TRUE or FALSE, 0 or 1 value
Operators which give Boolean value as results:
Logical Operators
Relational Operators
Conditional Statements
Basic Logical Operators
Logical
Operators
AND OR
NOT
Conditional Statements
NOT operator
reverses the logic or result of a certain condition
Condition A !A
F T
T F
Conditional Statements
AND/OR truth table
Condition a Condition b a AND b a OR b
F F F F
F T F T
T F F T
T T T T
Conditional Statements: Exercise1
Condition Condition Condition a AND b AND c a OR b OR c
a b c
F F F
F F T
F T F
F T T
T F F
T F T
T T F
T T T
Conditional Statements
Relational Operators
compare two values and returns a Boolean value depending on whether the
test condition is true or false
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Is equal to
!= Not equal to
<> Not equal to
Conditional Statements: Exercise 2
X Y X<Y X>Y X <= Y X >= Y X == Y X != Y
5 17 T F T F F T
18 4 F T F T F T
12 12 F F T T T F
30 31 T F T F F T
Conditional Statements
Conditions inside a decision box must:
either result to TRUE or FALSE
be represented in a mathematical form
Is variable X
X < 20? less than 20?
Conditional Statements
Single Alternative Selection Structure
A decision box may not necessarily have to do something for both the TRUE path
and the FALSE path
T F
Conditional Statements
Dual Alternative Selection Structure
This performs two different things when the condition is TRUE or FALSE
T F
Conditional Statements: Exercise 3
1. Draw a flowchart that will ask the user to enter a character (either ‘M’ or ‘F’) to
indicate the user’s gender (Male or Female).
Display “Male” if the user enters the value of ‘M’ and “Female” for the value of ‘F’
Assume all inputs are correct
2. Draw a flowchart that will ask the user to input the values of four variables, A, B,
C, and D
Print “TRUE” if A is greater than B, and C is less than D Print “FALSE” if otherwise
Conditional Statements
Multiple Decision Boxes
Start
x=0
Get x
Yes
x == 1? Disp “Hello”
No
Yes Disp “Hi”
x == 2?
No Stop
Yes
x == 3? Disp “Bye”
No
Disp “Invalid”
Conditional Statements
Using Decision Box with Ranges
Example:
Create a flowchart that would print the name of the supervisor under a certain
department number.
Department Number Supervisor
1-3 Mr. X
4-7 Mr. Y
8-9 Mr. Z
Conditional Statements
Solution #1:
Start
Dept = 0
Get Dept
Yes
Dept <= 3? Disp “Mr. X”
No
Yes
Dept <= 7? Disp “Mr. Y” Stop
No
Disp “Mr. Z”
Conditional Statements
Solution #2:
Start
Dept = 0
Get Dept
Yes
Dept <= 8? Disp “Mr. Z”
No
Yes
Dept <= 4? Disp “Mr. Y” Stop
No
Disp “Mr. X”
Conditional Statements: Exercise 4
Draw a flowchart that will print the student’s letter grade given the following
specifications:
90 – 100 = A
80 – 89 = B
70 – 79 = C
60 – 69 = D
below 60 = F
Looping Constructs
What is a Loop?
It is a set of program instructions that executes repeatedly until a condition is
satisfied.
Loop
Run True +*-/%
Condition Variable User Input
Stop False Initialization
Looping Constructs
Repetition Structure
Also referred to as a loop or iteration
repeats one or more instructions until a certain condition is met
Yes
X>Y?
Process A
No
Looping Constructs
Yes
X < Y? Display X
No
Yes
X < Y? Display X X=X+1
No
Looping Constructs
Pre-test repetition structure
The condition is tested BEFORE any actions are performed
if the condition does not exist, the loop will never begin and the actions inside the
loop will not be executed
Looping Constructs
Post-test repetition structure
The condition is tested AFTER the actions are performed
always performs its actions at least once
Display X
X=X+1
Yes
X < Y?
No
Looping Constructs: Example 1
Create a flowchart that will display the following result that the variable is
initialized to 5
Start
Output:
5 X=5
4
3 Yes
X > 0? Disp X
2 No
1 Disp “Done” X=X-1
Done
Stop
Looping Constructs: Example 2
Create a flowchart that will print a series of numbers based on what was entered
by the user.
Start
X, Y
Get X, Y
Yes
X<Y X=X+1
No
Disp “Done” Disp X
Stop
Looping Constructs: Example 3
Create a flowchart that will print the sum of all even numbers from 1 - 100.
Start
SUM,
CTR = 0
Yes
CTR < 100? SUM = SUM + CTR
No
CTR = CTR + 2
Disp SUM
Stop
Looping Constructs: Example 4
Create a flowchart that will print the sum of all odd numbers from 1 - 100.
Start
SUM = 0
CTR = 1
Yes CTR = CTR + 2
CTR < 100?
No
SUM = SUM + CTR
Disp SUM
Stop
Looping Constructs: Example 5
Create a flowchart that prints your name five times.
Start
Name=“”
Ctr=1
Get Name
Yes
Ctr<=5 Ctr=Ctr+1
No
Stop Disp Name
Looping Constructs – Multiple Loops
Looping Constructs – Multiple Loops
Multiple loops
are useful if there is a need to perform repetitive tasks
when there is a need to perform user input validations
Looping Constructs – Multiple Loops
Start
Gen=“”
Ctr=1
No
Ctr<=5
Yes
Disp Stop
Get Gen
‘Invalid’
Yes Gen!=‘M’
AND
Gen!=‘F’
No
Yes No
Gen==‘M’
Disp ‘Male’ Disp ‘Female’
Ctr=Ctr+1