02 Logic Formulation PDF
02 Logic Formulation PDF
Problem Analysis
Algorithm Development
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
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
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=“|”
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
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
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
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
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.
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
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
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
Gen=“”
Ctr=1
No
Ctr<=5
Yes
Disp Stop
Get Gen
‘Invalid’
Yes Gen!=‘M’
AND
Gen!=‘F’
No
Yes No
Gen==‘M’
Ctr=Ctr+1