Week 2 Programming Concepts and Logic Formulation: CC102 Fundamentals of Programming
Week 2 Programming Concepts and Logic Formulation: CC102 Fundamentals of Programming
Programming
Concepts and Logic Formulation
CC102
FUNDAMENTALS OF PROGRAMMING
WEEK 2 FUNDAMENTALS OF PROGRAMMING
LEARNING OUTCOMES:
At the end of the session, the students should be able to:
1. Can identify and explain each step involved in the programming
cycle
2. Can define, explain and determine the used of algorithm
3. Can define, explain, and determine the used of pseudocode
4. Can identify and discuss each flowchart symbol
5. Can illustrate and design flowcharts for the given problems.
CC102-FUNDAMENTALS OF PROGRAMMING 2
WEEK 2 FUNDAMENTALS OF PROGRAMMING
CC102-FUNDAMENTALS OF PROGRAMMING 3
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Programming Cycle
1. Problem definition – specifies what we want for the
computer to do
2. Problem analysis – looking at the problem itself; point
of view of the computer; start with the input/output
3. Algorithm development – this is an outline that
breaks problem down into segments; strategy on how
to do the task
4. Coding and debugging – this is the process to remove
errors
CC102-FUNDAMENTALS OF PROGRAMMING 4
WEEK 2 FUNDAMENTALS OF PROGRAMMING
ALGORITHM
• a step-by-step problem-solving procedure, especially an
established, recursive computational procedure for solving a
problem in a finite number of steps
• 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
CC102-FUNDAMENTALS OF PROGRAMMING 5
WEEK 2 FUNDAMENTALS OF PROGRAMMING
CHARACTERISTICS OF AN
ALGORITHM
• Specify each step or instruction exactly – There must be no
ambiguity or the instructions must be clear.
• There must be a finite number of steps – The algorithm must
terminate and should have a stopping point.
• There must be an output – The algorithm must produce the
correct result.
CC102-FUNDAMENTALS OF PROGRAMMING 6
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Pseudocodes
• generic way of describing an algorithm without use of any specific programming
language syntax
• it cannot be executed on a real computer, but it models and resembles real
programming code, and is written at roughly the same level of detail
CC102-FUNDAMENTALS OF PROGRAMMING 7
WEEK 2 FUNDAMENTALS OF PROGRAMMING
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
• these symbols are connected by lines and arrows to indicate
the flow of data or control from one point to another
CC102-FUNDAMENTALS OF PROGRAMMING 9
WEEK 2 FUNDAMENTALS OF PROGRAMMING
FLOWCHART SYMBOL
• Terminal block • Initialization
• Process • On Page
Connector
• Flow Lines
• Input/Output
• Off Page
Connector
• Decision
• Documents
CC102-FUNDAMENTALS OF PROGRAMMING 10
WEEK 2 FUNDAMENTALS OF PROGRAMMING
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
11
CC102-FUNDAMENTALS OF PROGRAMMING
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Terminal block
• ovals or rounded rectangles are 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
• a start has no flow lines entering it and only one
exiting it; an end or exit has one flow line
entering it but none exiting it
CC102-FUNDAMENTALS OF PROGRAMMING 12
WEEK 2 FUNDAMENTALS OF PROGRAMMING
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
• 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)
CC102-FUNDAMENTALS OF PROGRAMMING 13
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Process block
• the rectangle indicates a processing
block, for such things as
calculations, opening and closing
files, and so forth
• a processing block has one
entrance and one exit
CC102-FUNDAMENTALS OF PROGRAMMING 14
WEEK 2 FUNDAMENTALS OF PROGRAMMING
CC102-FUNDAMENTALS OF PROGRAMMING 15
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Decision block
• the diamond indicates a decision
• it has one entrance and exactly two
exits from the block
• one exit is the action when the
resultant is TRUE and the other exit
is the action when resultant is
FALSE
CC102-FUNDAMENTALS OF PROGRAMMING 16
WEEK 2 FUNDAMENTALS OF PROGRAMMING
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 .
• Note: These connectors should be used as
little as possible. They should only be used
to enhance readability. Overuse, however,
decreases readability and produces a
cluttered effect.
CC102-FUNDAMENTALS OF PROGRAMMING 17
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Example 1:
• Create a flowchart that
displays your age five years
from now. The algorithm
should have a variable that
is initialized to your current
age.
CC102-FUNDAMENTALS OF PROGRAMMING 18
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Example 2:
• Create a flowchart that displays the
user’s name, birthday, age, sex, and
phone number. Your flowchart
should display the variables with
the following format:
Name:
Age:
BDay:
Sex:
Phone#:
CC102-FUNDAMENTALS OF PROGRAMMING 19
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Example 3:
• Create a flowchart that will compute for the
Average score of a student based on three
quizzes. The quiz scores are entered by the
user. The scores are integers and may range
from 0 to 100, inclusive. However, the
Average may have a value having decimal
places. Example:
Q1: 98
Q2: 79
Q3: 88
Ave is 83.33
CC102-FUNDAMENTALS OF PROGRAMMING 20
WEEK 2 FUNDAMENTALS OF PROGRAMMING
DECISION STRUCTURES
• Conditions are statements that result to a Boolean
value. The Boolean value may either be a TRUE or
FALSE, 0 or 1 value.
• Conditions are categorized into two:
• 1) LOGICAL OPERATORS
• 2)RELATIONAL OPERATORS
CC102-FUNDAMENTALS OF PROGRAMMING 21
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Conditional Statements
• three basic logic operators that can be used for
conditional statements:
CC102-FUNDAMENTALS OF PROGRAMMING 22
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Conditional Statements
• LOGICAL OPERATORS
CC102-FUNDAMENTALS OF PROGRAMMING 23
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Conditional Statements
• NOT Truth Table
• TRUE = T or
• 1 FALSE = F or 0
• The NOT operator reverses the logic or result of a certain
condition
CC102-FUNDAMENTALS OF PROGRAMMING 24
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Conditional Statements
• AND/OR Truth Table
CC102-FUNDAMENTALS OF PROGRAMMING 25
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Exercise 1.
Relational Operators
• compare two values and returns a Boolean value depending
on whether the test condition is true or false
CC102-FUNDAMENTALS OF PROGRAMMING 27
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Exercise 2.
• Assume that you have variables X and Y. What would be the
results of the conditions if we were to compare them with
different values and different relational operators?
CC102-FUNDAMENTALS OF PROGRAMMING 28
WEEK 2 FUNDAMENTALS OF PROGRAMMING
CC102-FUNDAMENTALS OF PROGRAMMING 29
WEEK 2 FUNDAMENTALS OF PROGRAMMING
CC102-FUNDAMENTALS OF PROGRAMMING 30
WEEK 2 FUNDAMENTALS OF PROGRAMMING
CC102-FUNDAMENTALS OF PROGRAMMING 31
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Exercise 3.
• 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 and print “TRUE” if A is greater
than B, and C is less than D or if A is greater than B and C is less
than D. Print “FALSE” if otherwise.
CC102-FUNDAMENTALS OF PROGRAMMING 32
WEEK 2 FUNDAMENTALS OF PROGRAMMING
x=0
Get x
Yes
x == 1? “Hello”
No
Yes
x == 2? “Hi”
No End
Yes
x == 3? “Bye”
No
“Invalid”
CC102-FUNDAMENTALS OF PROGRAMMING 34
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Dept = 0
Get Dept
No
Dept Yes
<=7? “Mr. Y”
End
No
“Mr. Z”
CC102-FUNDAMENTALS OF PROGRAMMING 36
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Looping Constructs
•Repetition Structure
• also referred to as a loop
or iteration, this type of
structure repeats one or
more instructions until a
certain condition is met
CC102-FUNDAMENTALS OF PROGRAMMING 37
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Looping Constructs
• the action performed by a repetition structure must
eventually cause the loop to terminate, otherwise, an infinite
loop is created
• in the next flowchart segment, X is never changed
• once the loop starts, it will never end
• QUESTION:
How can this flowchart be modified so it is no longer
becomes an infinite loop?
CC102-FUNDAMENTALS OF PROGRAMMING 38
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Looping Constructs
CC102-FUNDAMENTALS OF PROGRAMMING 39
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Looping Constructs
• ANSWER: By adding an action within the repetition that changes the value of
X.
Looping Constructs
• this flowchart segment shows a
post-test repetition structure
• the condition is tested AFTER the
actions are performed
• a post-test repetition structure
always performs its actions at
least once
CC102-FUNDAMENTALS OF PROGRAMMING 41
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Example 1:
• Create a flowchart that will display the following result:
(Assume that the variable is initialized to 5) Sample Output:
5
4
3
2
1
Done
CC102-FUNDAMENTALS OF PROGRAMMING 42
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Flowchart
CC102-FUNDAMENTALS OF PROGRAMMING 43
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Example 2:
• Create a flowchart that will print a series of numbers based
on what was entered by the user. Number Entered: 4
• Sample Output:
1
2
3
4
Done
CC102-FUNDAMENTALS OF PROGRAMMING 44
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Flowchart
CC102-FUNDAMENTALS OF PROGRAMMING 45
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Example 3:
• Create a flowchart that will
print the sum of all even
numbers from 1 - 100.
CC102-FUNDAMENTALS OF PROGRAMMING 46
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Example 4:
• Example 4: Create a
flowchart that will print the
sum of all odd numbers
from 1 - 100
CC102-FUNDAMENTALS OF PROGRAMMING 47
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Example 5:
• Create a flowchart that
prints your name five
times.
CC102-FUNDAMENTALS OF PROGRAMMING 48
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Multiple Loops
• it is possible to have a loop within a loop
• in case of multiple loops, it is critical to determine what
condition will terminate the whole flowchart
CC102-FUNDAMENTALS OF PROGRAMMING 49
WEEK 2 FUNDAMENTALS OF PROGRAMMING
Example:
• Create a flowchart that asks the user to enter a gender.
Determine if the user presses either ‘M’ or ‘F’, if ‘M’ display
“MALE”, or if ‘F’ display “FEMALE”. Otherwise, display “Invalid
Gender” and let the user reenter a gender. Repeat the whole
process 5 times.
CC102-FUNDAMENTALS OF PROGRAMMING 50
WEEK 2 FUNDAMENTALS OF PROGRAMMING
CC102-FUNDAMENTALS OF PROGRAMMING 51
WEEK 2 FUNDAMENTALS OF PROGRAMMING
REFERENCES:
CC102-FUNDAMENTALS OF PROGRAMMING 52
THE END
CC102
FUNDAMENTALS OF PROGRAMMING