0% found this document useful (0 votes)
433 views53 pages

Week 2 Programming Concepts and Logic Formulation: CC102 Fundamentals of Programming

This document provides an overview of programming concepts and logic formulation covered in Week 2 of a fundamentals of programming course. It defines key terms like algorithms, pseudocode, and flowcharts. It also explains the programming cycle and how to use decision structures. Examples are given to illustrate how to create flowcharts to solve problems involving variables, user input, and calculations. Symbols and their uses in flowcharts are defined.

Uploaded by

RonGodfrey Ultra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
433 views53 pages

Week 2 Programming Concepts and Logic Formulation: CC102 Fundamentals of Programming

This document provides an overview of programming concepts and logic formulation covered in Week 2 of a fundamentals of programming course. It defines key terms like algorithms, pseudocode, and flowcharts. It also explains the programming cycle and how to use decision structures. Examples are given to illustrate how to create flowcharts to solve problems involving variables, user input, and calculations. Symbols and their uses in flowcharts are defined.

Uploaded by

RonGodfrey Ultra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 53

WEEK 2

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

What is a Computer Program?


• A computer program (also
a software program, or
just a program) is a
sequence of instructions
written to perform a
specified task for a
computer.

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

while not at end of list


compare adjacent elements
if second is greater than first
switch them
get next two elements
if elements were switched
repeat for entire list

CC102-FUNDAMENTALS OF PROGRAMMING 7
WEEK 2 FUNDAMENTALS OF PROGRAMMING

STEPS IN PROBLEM SOLVING


• First produce a general algorithm (one can use pseudo
code)
• Refine the algorithm successively to get step by step
detailed algorithm that is very close to a computer
language.
• Pseudo code is an artificial and informal language that
helps programmers develop algorithms. Pseudo code is
very similar to everyday English.
CC102-FUNDAMENTALS OF PROGRAMMING 8
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

Input / Output block


• the parallelogram indicates
input to and output operations
• an I/O block has one entrance
and only one exit

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.

• What would be the result of conditions A, B, and C


when using the AND and OR operator?
CC102-FUNDAMENTALS OF PROGRAMMING 26
WEEK 2 FUNDAMENTALS OF PROGRAMMING

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

More on Decision Box


• it should be noted that what you place inside the diamond
box are conditions that either results to TRUE or FALSE
• Note: You don’t place conditions that are in essay form, you
must be able to represent it in a mathematical form.

CC102-FUNDAMENTALS OF PROGRAMMING 29
WEEK 2 FUNDAMENTALS OF PROGRAMMING

Single Alternative Selection Structure


• A decision box may not
necessarily have to do something
for both the TRUE path and the
FALSE path.
• Example: if x < y, print x

CC102-FUNDAMENTALS OF PROGRAMMING 30
WEEK 2 FUNDAMENTALS OF PROGRAMMING

Dual Alternative Selection Structure


• performs two different things
when the condition is TRUE or
FALSE
• Example: if x < y, print x,
otherwise print y

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

Multiple Decision Boxes


• can oftentimes be combined into a single box
• there are times, however, when you have to be very
specific with the conditions that you have to put in
• Example:
• Create a flowchart that asks the user enter a number
from 1 – 3. If 1 is entered, print “Hello”; if 2 is entered,
print “Hi”; and if 3 is entered, then print “Bye”.
Otherwise, print “Invalid.”
CC102-FUNDAMENTALS OF PROGRAMMING 33
WEEK 2 FUNDAMENTALS OF PROGRAMMING

Multiple Decision Boxes


Start

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

Using Decision Box with Ranges


• use a sequence of decision blocks starting with either the lowest or
highest value in each range of values you are using to make your
selections
• 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
CC102-FUNDAMENTALS OF PROGRAMMING 35
WEEK 2 FUNDAMENTALS OF PROGRAMMING

Multiple Decision Boxes


Start

Dept = 0

Get Dept

Dept <= Yes


3? “Mr. X”

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.

• this type of structure is known as a pre-test repetition structure


• the condition is tested BEFORE any actions are performed
• in a pre-test repetition structure, if the condition does not exist, the loop will
never begin and the actions inside the loop will not be executed
CC102-FUNDAMENTALS OF PROGRAMMING 40
WEEK 2 FUNDAMENTALS OF PROGRAMMING

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:

Hoffer, J.A. George, J. Valacich, J. (2013).


Modern systems analysis and design (7 th ed).
Pearson Education Gonzalez, V. Ph. D. Java 2 bible. UP Los
Banos, Laguna Schildt, H. (2011).
Java the complete reference (8th ed). McGrew Hill
Professional

CC102-FUNDAMENTALS OF PROGRAMMING 52
THE END

CC102
FUNDAMENTALS OF PROGRAMMING

You might also like