SlideShare una empresa de Scribd logo
1 de 174
02/10/12
Programming
8.2 Approach in Problem Solving
02/10/12
• 8.2.1 Input Process Output (IPO)
Analysis
Approach in Problem Solving
02/10/12
Learning Outcome
• Identify input, process and output from a
given problem
02/10/12
Input Process Output (IPO)
Analysis
• The IPO is used to analyze problems and
develop algorithms
• Used to organize and summarize the
results of a problem analysis
• It shows where in the solution the
processing takes place
02/10/12
Input Process Output (IPO)
Analysis
Input Processing Output
Processing items:
Algorithm
• It can also be represent using IPO
chart
02/10/12
Input Process Output (IPO)
Analysis
To do the IPO Analysis, start with:
1- Output (Display the results)
2- Input (Read the data)
3- Process (Perform the computation)
Input :
Process :
Output :
Identify
02/10/12
• Output should answer the following
question:
What does the user want to see
printed
on the printer, displayed on the
screen, or
stored in a file?
Input Process Output (IPO)
Analysis
02/10/12
• Input should answer the following
question:
What information will the computer
need
to know to print, display, or store the
output items?
Input Process Output (IPO)
Analysis
02/10/12
• Processing item:
An intermediate value that the
algorithm uses when processing the
input into the output
Input Process Output (IPO)
Analysis
02/10/12
Analyze the Problem
• What is it you are trying to accomplish?
• What outcome are you trying to arrive at?
• List the Inputs and Outputs
• Often you work backwards from the Output
• List the Outputs, and then figure out what
Inputs you need in order to arrive at the
Outputs
02/10/12
Example 1
Problem statement:
Calculate the area of a rectangle
We can summarize the information
contained in the problem statement as
follows
width
height
Analyze the Problem
02/10/12
Problem Analysis:
To do the IPO Analysis, start with:
1 – Output
2 – Input
3 – Process
INPUT :
PROCESS :
OUTPUT :
Identify :
Analyze the Problem
02/10/12
Problem Analysis:
Input: width, height
Process: area = width x height
Output: area of rectangle
Analyze the Problem
02/10/12
Input Processing Output
width
height
Processing items:
area = width x height
Algorithm :
area of rectangle
Analyze the Problem
The Problem Analysis, can also be
represent using IPO (Input, Processing,
Output) Chart
02/10/12
Example 2
Problem statement:
Find the average of three numbers
input by user
We can summarize the information
contained in the problem statement as
follows
Analyze the Problem
02/10/12
Problem Analysis:
To do the IPO Analysis, start with:
1 – Output
2 – Input
3 – Process
INPUT :
PROCESS :
OUTPUT :
Identify :
Analyze the Problem
02/10/12
Problem Analysis:
Input: number1, number2, number3
Process:
average = (number1 + number2 +
number3) / 3
Output: average
Analyze the Problem
02/10/12
Input Processing Output
number1
number2
number3
Processing items:
average = (number1 +
number2 + number3) / 3
Algorithm :
average
Analyze the Problem
The Problem Analysis, can also be
represent using IPO (Input, Processing,
Output) Chart
02/10/12
Example 3
Problem statement:
Determine the total cost of apples,
given the number of kilos of apples
purchased and the cost of apples per
kilo
We can summarize the information
contained in the problem statement as
follows
Analyze the Problem
02/10/12
Problem Analysis:
To do the IPO Analysis, start with:
1 – Output
2 – Input
3 – Process
INPUT :
PROCESS :
OUTPUT :
Identify :
Analyze the Problem
02/10/12
Problem Analysis:
Input:
Number of kilos of apples
Cost of apples per kilo
Process:
Total cost = Number of kilos of apples ×
Cost of apples per kilo
Output:
Total cost of apples
Analyze the Problem
02/10/12
Analyze the Problem
The Problem Analysis, can also be
represent using IPO (Input, Processing,
Output) Chart
Input Processing Output
Number of kilos of
apples
Cost of apples per kilo
Processing items:
Total cost = Number of
kilos of apples × Cost of
apples per kilo
Algorithm :
Total cost of apples
02/10/12
Summary
• Approaches in problem solving
• Identify Input Process Output (IPO) from a
problem statement
What have you learned ?
02/10/12
• 8.2.2 Algorithm
• 8.2.3 Pseudocode
Approach in Problem Solving
02/10/12
Learning Outcome
• Define algorithm
• Solve problem using pseudocode
02/10/12
ALGORITHM
• The algorithm is the abstract idea of
solving a problem.
• An algorithm is a step-by-step
instructions that will transform the input
into the output
• It can be represent using pseudocode or
flow chart
02/10/12
From Algorithms to Programs
Problem
C++ Program
1. Problem Analysis
2. Algorithm
02/10/12
Algorithm in everyday’s life
• How to make a mug of hot coffee:
1.Start
• Boil water
• Prepare a mug
• Put a tea spoon of coffee & sugar
• Pour hot water
• Stir
• End
02/10/12
ALGORITHM
can be described using three control structures
(Bohm and Jacopini-1966);
• A Sequence
o is a series of statements that execute one after
another
• A Selection – (branch)
o statement is used to determine which of two different
statements to execute depending on certain
conditions
• A Looping – (repetition)
o statement is used to repeat statements while certain
02/10/12
PLANNING THE ALGORITHM
Identify:
INPUT :
PROCESS :
OUTPUT :
2. Transfer to pseudo code or flow chart
3. a. Must start with a start
b. Must close with an end
1. Do Problem Analysis
02/10/12
TECHNIQUES TO REPRESENT
THE ALGORITHM
Pseudocode Flow chart
• Artificial, informal
language used to
develop algorithms
• Similar to everyday
English
• Graphical
representation of an
algorithm
• Special-purpose
symbols connected by
arrows (flow lines)
1.Pseudocod
e
2.Flow Chart
02/10/12
TECHNIQUES TO REPRESENT
THE ALGORITHM
Calculate the area of a circle
Pseudocode Flow chart
Start:
Input radius
Calculate circle area
circle area = 3.142 * radius *
radius
Print the circle area
End:
1. Example of Pseudocode & Flow
chart
radius
circle area = 3.142 * radius *
radius
circle area
start
end
02/10/12
1. Pseudocode
- Pseudocode Format
start
statement 1
statement 2
end
1
3
2
TECHNIQUES TO REPRESENT
THE ALGORITHM
02/10/12
Algorithm Design : Pseudocode
Example 1
Problem statement:
Calculate the area of a rectangle
Remember!
1. Do Problem Analysis
2. Transfer to pseudo code or flow chart
02/10/12
1. Problem Analysis:
Input: width, height
Process: area = width x height
Output: area of rectangle
Algorithm Design : Pseudocode
02/10/12
Algorithm Design : Pseudocode
start
read width and height
calculate area
area of rectangle = width * height
print area of rectangle
end
2. Pseudocode
02/10/12
IPO Chart with Pseudocode
Input Processing Output
width
height
Processing items:
area = width x height
Algorithm - Pseudocode:
Start
Read width and height
Calculate area
area of rectangle =
width * height
Print area
End
area of rectangle
02/10/12
Algorithm Design : Pseudocode
Example 2
Problem statement:
Find the average of three numbers input
by user
Remember!
1. Do Problem Analysis
2. Transfer to pseudo code or flow chart
02/10/12
1. Problem Analysis:
Input: number1, number2, number3
Process:
average = (number1 + number2 +
number3) / 3
Output: average
Algorithm Design : Pseudocode
02/10/12
Algorithm Design : Pseudocode
start
input number1, number2, number3
calculate average
average = (number1 + number2 + number3) / 3
print average
end
2. Pseudocode
02/10/12
IPO Chart with Pseudocode
Input Processing Output
number1
number2
number3
Processing items:
average = (number1 +
number2 + number3) / 3
Algorithm - Pseudocode:
Start
Input number1, number2,
number3
Calculate average
average = (number1 +
number2 + number3) / 3
Print average
End
average
02/10/12
Algorithm Design : Pseudocode
Example 3
Problem statement:
Determine the total cost of apples,
given the number of kilos of apples
purchased and the cost of apples per
kilo
Remember!
1. Do Problem Analysis
2. Transfer to pseudo code or flow chart
02/10/12
1. Problem Analysis:
Input:
Number of kilos of apples
Cost of apples per kilo
Process:
Total cost = Number of kilos of apples ×
Cost of apples per kilo
Output:
Total cost of apples
Algorithm Design : Pseudocode
02/10/12
Algorithm Design : Pseudocode
start
Read Number of kilos of apples and Cost of
apples per kilo
Calculate Total cost
Total cost = Number of kilos of apples x Cost of
apples per kilo
Print Total cost of apples
end
2. Pseudocode
02/10/12
Input Processing Output
Number of kilos of
apples
Cost of apples per kilo
Total cost = Number of kilos of
apples × Cost of apples per kilo
Algorithm - Pseudocode:
Start
Read Number of kilos of apples and
Cost of apples per kilo
Calculate Total Cost
Total cost = Number of kilos of
apples × Cost of apples
per kilo
Print Total cost of apples
End
Total cost of
apples
IPO Chart with Pseudocode
02/10/12
Summary
• Approaches in problem solving
• Planning the algorithm
• Steps in building pseudocode
• Problem solving using pseudocode
What have you learned ?
02/10/12
• 8.2.4 Flow Chart
Approach in Problem Solving
02/10/12
Learning Outcome
• Solve problem using flow chart
02/10/12
2. Flow Chart
- Flow Chart Format
TECHNIQUES TO REPRESENT
THE ALGORITHM
Statement 1
Statement 2
The statement
refers to any
input, output
& process
involved
start
end
02/10/12
FLOW CHART - SYMBOLS
Graphic
Symbol
Name Meaning
Terminal
Symbol (oval)
indicates the beginning and end
points of an algorithm
Process
Symbol
(rectangle)
shows an instruction other than
input, output or selection
Input-Output
Symbol
(parallelogram)
shows an input or output operation
02/10/12
FLOW CHART - SYMBOLS
Graphic
Symbol
Name Meaning
Disk Storage
Input-Output
Symbol (cylinder)
indicates input from or output to
disk storage
Printer Output
Symbol
shows hardcopy printer output
Selection Symbol
(diamond)
shows a selection process for two-
way selection
02/10/12
FLOW CHART - SYMBOLS
Graphic
Symbol
Name Meaning
Flow Lines
(arrow)
indicates the logical sequence of
execution steps in the algorithm
Off-Page
Connector
provides continuation of a logical
path on another page
On-Page
Connector
(circle)
provides continuation of a logical
path at another point in the same
page
02/10/12
Algorithm Design : Flow Chart
Example 1
Problem statement:
Calculate the area of a rectangle
Remember!
1. Do Problem Analysis
2. Transfer to pseudo code or flow chart
02/10/12
1. Problem Analysis:
Input: width, height
Process: area = width x height
Output: area of rectangle
Algorithm Design : Flow Chart
02/10/12
Algorithm Design : Flow Chart
2. Flow Chart
area = width * height
width, height
area
start
end
02/10/12
IPO Chart with Flow Chart
Input Processing Output
width
height
Processing items:
area = width x height
Algorithm – Flow Chart:
area of rectangle
area = width * height
width, height
area
start
end
02/10/12
Algorithm Design : Flow Chart
Example 2
Problem statement:
Find the average of three numbers input
by user
Remember!
1. Do Problem Analysis
2. Transfer to pseudo code or flow chart
02/10/12
1. Problem Analysis:
Input: number1, number2, number3
Process:
average = (number1 + number2 + number3)
/ 3
Output: average
Algorithm Design : Flow Chart
02/10/12
Algorithm Design : Flow Chart
2. Flow Chart
average = (number1 + number2 + number3) / 3
number1, number2, number3
average
start
end
02/10/12
IPO Chart with Flow Chart
Input Processing Output
number1
number2
number3
Processing items:
average = (num1 + num2 + num3) / 3
Algorithm – Flow Chart:
average
average = (number1 + number2 + number3) / 3
number1, number2, number3
average
start
end
02/10/12
Algorithm Design : Flow Chart
Example 3
Problem statement:
Determine the total cost of apples,
given the number of kilos of apples
purchased and the cost of apples per
kilo
Remember!
1. Do Problem Analysis
2. Transfer to pseudo code or flow chart
02/10/12
1. Problem Analysis:
Input:
Number of kilos of apples
Cost of apples per kilo
Process:
Total cost = Number of kilos of apples ×
Cost of apples per kilo
Output:
Total cost of apples
Algorithm Design : Flow Chart
02/10/12
Algorithm Design : Flow Chart
2. Flow Chart
Number of kilos of
apples,
Cost of apples per kilo
Total cost =
Number of kilos of apples x
Cost of apples per kilo
Total cost of apples
end
start
02/10/12
IPO Chart with Flow Chart
Input Processing Output
Number of kilos of
apples
Cost of apples per kilo
Total cost = Number of
kilos of apples × Cost of
apples per kilo
Algorithm – Flow Chart:
Total cost of apples
Number of kilos of
apples, Cost of apples
per kilo
Total cost =
Number of kilos of apples
x Cost of apples per kilo
Total cost of apples
end
start
02/10/12
Summary
• Approaches in problem solving
• Steps in building flow chart
• Problem solving using flow chart
What have you learned ?
02/10/12
Revision
• What is IPO analysis?
• What is algorithm?
• What are the steps in planning your
algorithm?
• Differentiate between pseudocode and
flowchart.
What have you learned ?
02/10/12
8.2 Approach in Problem Solving
8.2.5 Control Structure
8.2.5.2 Sequence
02/10/12
8.2.5.1 Sequence
Learning Outcome
At the end of the lesson, students should be able
to :
1. Understand basic problem solving techniques.
• Use the sequence structured in problem solving.
• Develop flowchart through the process of top-down.
02/10/12
8.2.5.1 Sequence
• The simplest programs consist just sequence of
statements :
• no loops, no selections amongst alternative actions, no
use of subroutines.
02/10/12
8.2.5.1 Sequence
• Instruction in sequence programming are
executed sequentially one by one
• The sequence structure directs the computer to
process the instructions, one after another, in the
order listed in the program.
first instruction last instruction.
02/10/12
8.2.5.1 Sequence
02/10/12
8.2.5.1 Sequence
• Planning the Algorithm
• Record the algorithm using IPO analysis / IPO
chart.
Problem Analysis:
Input: sales
Process: bonus = sales * (0.05)
Output: bonus
02/10/12
Algorithm Design : Pseudocode
start
input sales
bonus = sales * (0.05)
print bonus
end
Pseudocode
-tool programmers use to help them plan an
algorithm
-consist of short, English-like statements
02/10/12
Algorithm Design : Flow Chart
Flow Chart
- Use
standardized
symbols to show
the steps the
computer need
to take to
accomplish the
program’s goal
start
bonus = sales *
(0.05)
end
sales
bonus
02/10/12
8.2.5.1 Sequence
02/10/12
8.2.5.1 Sequence
Problem Analysis:
Input: width, length, price of tile
Process:
area = width * length
total of price = area * price of tile
Output: total of price
• Planning the Algorithm
• Record the algorithm using IPO analysis / IPO
chart.
02/10/12
Algorithm Design : Pseudocode
start
input width, length, price of tile
area = width * length
total price of tile = area * price of tile
print total price of tile
end
Pseudocode
-tool programmers use to help them plan an
algorithm
-consist of short, English-like statements
02/10/12
Algorithm Design : Flow Chart
start
area = width * length
total price of tile = area * price of
tile
end
width
length
price of tile
Total price of
tile
Flow Chart
- Use
standardized
symbols to
show the steps
the computer
need to take to
accomplish the
program’s goal
02/10/12
8.2.5.1 Sequence
02/10/12
8.2.5.1 Sequence
Problem Analysis:
Input: hour
Process:
Total overtime payment = hour * 5.00
Output: total overtime payment
• Planning the Algorithm
• Record the algorithm using IPO analysis / IPO
chart.
02/10/12
Algorithm Design : Pseudocode
start
input hour
total overtime payment = hour * 5.00
print total overtime payment
end
Pseudocode
-tool programmers use to help them plan an
algorithm
-consist of short, English-like statements
02/10/12
Algorithm Design : Flow Chart
start
Total overtime payment =
hour * 5.00
end
hour
Total overtime
payment
Flow Chart
- Use
standardized
symbols to
show the steps
the computer
need to take to
accomplish the
program’s goal
02/10/12
Summary
• Instruction in sequence programming are executed
sequentially one by one
• First step in the problem-solving process it to
analyze the problem
• Planning the algorithm is using IPO analysis / IPO
chart
• Programmer uses the pseudocode as a guide
when coding the algorithm
• Flow chart uses standardized symbols to show the
steps the computer needs to take to accomplish
the program’s goal
02/10/12
8.0
PROGRAMMING
8.2: Approach in Problem Solving
8.2.5: Control Structure
8.2.5.2  Selection
02/10/12
• At the end of this topic, students should
be able to:
a) explain the purpose of selection control
structure.
b) apply selection control structure in
problem solving.
Learning Outcome
02/10/12
Control Structure : Selection
SELECTION Analogy
You need to choose to make “Coffee O” or
“Milk Coffee”
02/10/12
Control Structure : Selection
SEQUENC
E
Control Structures
SELECTION LOOPING
02/10/12
What is Selection Structure?
• The selection structure allows instructions
to be executed non-sequentially.
• It allows the comparison of two
expressions, and based on the
comparison, to select certain course of
action.
Control Structure : Selection
02/10/12
Control Structure : Selection
if-else
Types of selection structure
nested if-else switch
02/10/12
• If-else structure’s form
if (expression)
statement_1
else
statement_2
Selection  (1) if-else
02/10/12
• In this form, the expression is first
evaluated.
• If it evaluates to non-zero (TRUE),
statement_1 is executed.
• Otherwise, statement_2 is executed.
• Either statement_1 or statement_2 is
executed but not BOTH.
Selection  (1) if-else
02/10/12
• Pseudocode format:
Selection  (1) if-else
start
if (expression)
statement_1
else
statement_2
end
02/10/12
• Flow chart format:
Selection  (1) if-else
02/10/12
• Example 1:
Print “Excellent!” when user enter marks
greater than and equal to 80, else print
“Sorry, try again”.
Selection  (1) if-else
Remember to plan your algorithm!
1.Do IPO Analysis
• Transfer to Pseudocode or Flow
Chart
02/10/12
• Example 1:
Print “Excellent!” when user enter marks
greater than and equal to 80, else print
“Sorry, try again”.
Selection  (1) if-else
02/10/12
IPO Analysis:
Input: marks
Process:
if (marks >= 80)
print “Excellent”
else
print “Sorry, try again”
Output: “Excellent!” or “Sorry, try again”
Selection  (1) if-else
02/10/12
• Pseudocode
Selection  (1) if-else
start
read marks
if (marks >= 80)
print “Excellent!”
else
print “Sorry, try again”
end
02/10/12
• Flow chart
start
end
marks
“Excellent!”
marks >= 80
true “Sorry, try again”
false
Selection  (1) if-else
02/10/12
• Example 2:
A high school poetry competition is open
only for students above 15 years old.
Display “Eligible” if the students meet the
requirement, else display “Not eligible” if
otherwise.
Selection  (1) if-else
02/10/12
• Example 2:
A high school poetry competition is open
only for students above 15 years old.
Display “Eligible” if the students meet the
requirement, else display “Not eligible” if
otherwise.
Selection  (1) if-else
02/10/12
IPO Analysis:
Input: age
Process:
if (age > 15)
print “Eligible”
else
print “Not eligible”
Output: “Eligible” or “Not eligible”
Selection  (1) if-else
02/10/12
• Pseudocode
Selection  (1) if-else
start
read age
if (age > 15)
print “Eligible”
else
print “Not eligible”
end
02/10/12
• Flow chart
start
end
age
“Eligible”
age > 15
true “Not eligible”
false
Selection  (1) if-else
02/10/12
• Example 3:
If x is greater than y, display “x is bigger
than y” else display “x is smaller than y”.
Selection  (1) if-else
02/10/12
• Example 3:
If x is greater than y, display “x is bigger
than y” else display “x is smaller than y”.
Selection  (1) if-else
02/10/12
IPO Analysis:
Input: x,y
Process:
if (x > y)
print “x is bigger than y”
else
print “x is smaller than y”
Output: “x is bigger than y” OR “x is smaller than y”
Selection  (1) if-else
02/10/12
• Pseudocode
Selection  (1) if-else
start
read x,y
if (x > y)
print “x is bigger than y”
else
print “x is smaller than y”
end
02/10/12
• Flow chart
start
end
x,y
“x is bigger than y”
x > y
true “x is smaller than y”
false
Selection  (1) if-else
02/10/12
Selection  (2) nested if-else
if-else
Types of selection structure
nested if-
else
switch
02/10/12
• The if-else structure can also be nested to
any depth.
Selection  (2) nested if-else
02/10/12
• The nested if-else if structured takes the general form:
if (expression_1)
statement_1;
else if (expression_2)
statement_2;
else if (expression_3)
statement_3;
else
statement_4;
Selection  (2) nested if-else
02/10/12
• In this nested form, expression_1 is
evaluated. If it evaluates to non-zero
(TRUE), statement_1 is executed.
• If not, control goes to the second if, where
expression_2 is evaluated. If it evaluates
to non-zero (TRUE), statement_2 is
executed.
• If not, control goes to the third if, where
expression_3 is evaluated. If it evaluates
to non-zero (TRUE), statement_3 is
executed.
Selection  (2) nested if-else
02/10/12
• If not, statement_4 is executed.
• Rules  Only ONE of the statements is
executed.
Selection  (2) nested if-else
02/10/12
• Example 1:
if student’s grade is greater than or equal to 80
Print “A”
else if student’s grade is greater than or equal to
60
Print “B”
else if student’s grade is greater than or equal to
50
Print “C”
else
Print “Failed”
Selection  (2) nested if-else
02/10/12
IPO Analysis:
Input: grade
Process: if (grade >= 80)
print “A”
else if (grade >= 60)
print “B”
else if (grade >= 50)
print “C”
else
print “Failed”
Output: “A” or “B” or “C” or “Failed”
Selection  (2) nested if-else
02/10/12
• Pseudocode
Selection  (2) nested if-else
start
read grade
if (grade >= 80)
print “A”
else if (grade >= 60)
print “B”
else if (grade >= 50)
print “C”
else
print “Failed”
end
02/10/12
• Flow chart
Selection  (2) nested if-else
start
end
grade
grade >= 80
true
“A”
false
grade >= 60
grade >= 50
“B”
“C”
false
false
“Failed”
true
true
02/10/12
Selection  (2) nested if-else
• Example 2:
if (x > 0)
display "x is positive"
else if (x < 0)
display "x is negative"
else
display "x is 0"
02/10/12
Selection  (2) nested if-else
IPO Analysis:
Input: x
Process: if (x > 0)
display "x is positive"
else if (x < 0)
display "x is negative"
else
display "x is 0“
Output: “x is positive“ or "x is negative“ or “x is 0”
02/10/12
Selection  (2) nested if-else
• Pseudo code
start
read x
if (x > 0)
display "x is positive"
else if (x < 0)
display "x is negative"
else
display "x is 0"
end
02/10/12
Selection  (2) nested if-else
• Flow chart
start
end
x
(x > 0)
true "x is
positive”
false
(x < 0) “x is
negative”
false
“x is 0”
true
02/10/12
Selection  (3) Switch
if-else
Types of selection structure
nested if-else switch
02/10/12
• Switch case statements are to check
several possible constant values for an
expression.
• Switch form:
switch (expression) {
case constant1: group of statements1
break
case constant2: group of statements2
break
. . .
default: default group of statements
}
Selection  (3) Switch
02/10/12
• switch evaluates expression and checks if
it is equivalent to constant1, if it is, it
executes group of statements1 until it
finds the break statement.
Selection  (3) Switch
02/10/12
• If expression was not equal
to constant1 it will be checked
against constant2. If it is equal to this, it
will execute statements2.
Selection  (3) Switch
02/10/12
• If the value of expression DID NOT
match any of the previously specified
constants (you can include as
many case labels as values you want to
check), the program will execute the
statements included after the default.
Selection  (3) Switch
02/10/12
iv) Selection - switch
switch Flow chart
02/10/12
• Example 1:
Selection  (3) Switch
x Print
1 X is 1
2 X is 2
Other value Value of x unknown
02/10/12
Selection  (3) Switch
IPO Analysis:
Input: x
Process:
switch (x)
{ case 1: print "x is 1“
break
case 2: print "x is 2“
break
default: Print "value of x unknown”
}
Output: "x is 1" or "x is 2" or "value of x unknown”
02/10/12
Selection  (3) Switch
• Pseudocode
start
read x
switch (x)
{ case 1: print "x is 1“
break
case 2: print "x is 2"
break
default: print "value of x unknown”
}
end
02/10/12
b) Flow chart
iv) Selection - switch
02/10/12
SUMMARY
if-else
Types of selection structure
nested if-
else
switch
* Use an appropriate selection
control structure in problem solving
02/10/12
Topic: 8.2
Approach in Problem Solving
8.2.5: Control Structure
8.2.5.3: Looping
02/10/12
Learning Outcome
At the end of this topic, students should be able to:
• explain the purpose of looping control structure.
• apply looping control structure in problem
solving.
02/10/12
What is looping control structure?
• The looping (or repetition) structure allows a
sequence of instructions to be executed
repeatedly until a certain condition is reached.
• The looping structure has three forms:
o while
o do..while
o for
02/10/12
The essentials of looping (repetition)
• The loop counter (loop control variable ):
o Counts the number of repetitions in the counter-
controlled loop
o Controls the execution of loop
• The loop counter requires the following to be known in
advance:
1.Name of a loop counter
• Initial value of the loop counter
• Increment (or decrement) by which to modify the
loop counter in each traversal
• Condition to test the final value of loop counter
02/10/12
1. The while Construct
The looping control structure:
02/10/12
The while Construct
• The while construct has the general form:
while (expression) statement;
• where the expression is first evaluated.
• If it is true (not zero), the *statement(s) is
executed,
• else if it false (zero), the statement is bypassed.
* note: statement(s); which can be a block of statements –
more than 1 statement
02/10/12
Applying Algorithm in while Construct
• Pseudocode
start
initialize counter
while (expression)
statement(s)
counter increment
end
12
4
3
* note: please refer to slide #4 for numbering details
02/10/12
Applying Algorithm in while Construct
• Flow Chart start
initialize
counter
while
(expression)
statement(s)
True
counter increment
False
end
02/10/12
The Counter Table for while Construct
counter expression input process output counter
increment
initial value of
counter condition to
test
IPO analysis
increment to
modify the loop
i i <= 3 input process output i = i + 1
xample of while counter table:
02/10/12
Example 1: while Construct
• Problem Statement:
o Calculate the area of a rectangle for 3 times.
• Remember to plan your algorithm.
o Do Problem Analysis.
o Transfer to Pseudocode or Flow Chart.
width
height
02/10/12
i i <= 3 width,height area = width * height area i = i + 1
while counter table
Example 1: while Construct
Problem Analysis:
Input: width, height
Process: area = width * height
Output: area
02/10/12
i i <= 3 width,height area = width * height area i = i + 1
1 T 2
2 T 3
3 T 4
4 F Loop terminate
while counter table
Example 1: while Construct
Problem Analysis:
Input: width, height
Process: area = width * height
Output: area
02/10/12
• Pseudocode
Example 1: while Construct
start
1. initialize counter
i = 1
2. while (i <= 3)
3. input width, height
4. calculate area of rectangle
area = width * height
5. print area
6. counter increment
i = i + 1
7. repeat until i > 3
end
IPO analysis
initial value of counter
condition to test
increment to
modify the loop
Repeat step 2 – 6 until
expression is false
02/10/12
• Flow Chart
Example 1: while Construct
start
i = 1
(i <= 3)
width, height
True
i = i + 1
False
end
area = width *
height
area
02/10/12
2. The do..while Construct
The looping control structure:
02/10/12
The do..while Construct
• The do..while construct has the general form:
do
statement
while (expression);
• where the expression is last evaluated, after the
statement(s) is executed.
• This means the statement(s) in the do..while will
be executed at least once.
02/10/12
Applying Algorithm in do..while
Construct
• Pseudocode
start
initialize counter
do
statement(s)
counter increment
while (expression)
end
12
4
3
* note: please refer to slide #4 for numbering details
02/10/12
Applying Algorithm in do..while
Construct
• Flow Chart start
initialize
counter
while
(expression)
statement(s)
True
counter increment
False
end
02/10/12
The Counter Table for do..while
Construct
counter input process output counter
increment
expression
initial value of
counter
condition to
test
IPO analysis
increment to
modify the loop
i input process output i = i + 1 i <= 3
ample of do..while counter table:
02/10/12
Example 2: do..while Construct
• Problem Statement:
o Calculate the area of a rectangle for 3 times.
• Remember to plan your algorithm.
o Do Problem Analysis.
o Transfer to Pseudocode or Flow Chart.
width
height
02/10/12
i width,height area = width * height area i = i + 1 i <= 3
do..while counter table
Example 2: do..while Construct
Problem Analysis:
Input: width, height
Process: area = width * height
Output: area
02/10/12
i width,height area = width * height area i = i + 1 i <= 3
1 2 T
2 3 T
3 4 F
Loop terminate
do..while counter table
Example 2: do..while Construct
Problem Analysis:
Input: width, height
Process: area = width * height
Output: area
02/10/12
• Pseudocode
Example 2: do..while Construct
start
1. initialize counter
i = 1
2. do
3. input width, height
4. calculate area of rectangle
area = width * height
5. print area
6. counter increment
i = i + 1
7. repeat while (i <= 3)
end
IPO analysis
initial value of counter
condition to test
increment to
modify the loop
02/10/12
• Flow Chart
Example 2: do..while Construct
start
i = 1
(i <= 3)
width, height
True
i = i + 1
False
end
area = width *
height
area
02/10/12
3. The for Construct
The looping control structure:
02/10/12
The for Construct
• The for construct has the general form:
for (initialization; expression;
incrementation) statement;
• where the initialization refers to the initial value
of a loop counter.
• the expression determines whether the loop
should be continued; if it is true (not zero), the
*statement(s) is executed, else if it false (zero),
the for loop is terminated.
• The incrementation increments the loop counter.
* note: statement(s); which can be a block of statements –
more than 1 statement
02/10/12
Applying Algorithm in for Construct
• Pseudocode
start
for (initialize counter; expression; counter
increment)
statement(s);
end
2
* note: please refer to slide #4 for numbering details
1 4 3
02/10/12
The for loop is executed as follows:
start
for (initialize counter; expression; counter
increment)
statement(s);
end
o After one-time initialization, the expression is first
evaluated.
o If it is false, the for loop is terminated; if it is true the
statement(s) in the loop is executed.
o And then the incrementation is performed.
o The expression condition is again evaluated, until
the expression becomes false.
02/10/12
Applying Algorithm in for Construct
• Flow Chart start
initialize
counter
(expression)
statement(s)
True
counter increment
False
end
02/10/12
The Counter Table for for Construct
counter expression input process output counter
increment
initial value of
counter condition to
test
IPO analysis
increment to
modify the loop
i i <= 3 input process output i = i + 1
xample of for counter table:
02/10/12
Example 3: for Construct
• Problem Statement:
o Calculate the area of a rectangle for 3 times.
• Remember to plan your algorithm.
o Do Problem Analysis.
o Transfer to Pseudocode or Flow Chart.
width
height
02/10/12
i i <= 3 width,height area = width * height area i = i + 1
for counter table
Example 3: for Construct
Problem Analysis:
Input: width, height
Process: area = width * height
Output: area
02/10/12
i i <= 3 width,height area = width * height area i = i + 1
1 T 2
2 T 3
3 T 4
4 F Loop terminate
for counter table
Example 3: for Construct
Problem Analysis:
Input: width, height
Process: area = width * height
Output: area
02/10/12
• Pseudocode
Example 3: for Construct
start
1. initialize counter
i = 1
2. for (i <= 3)
3. read width, height
4. calculate area of rectangle
area = width * height
5. print area
6. counter increment
i = i + 1
7. repeat until i > 3
end
IPO analysis
initial value of counter
condition to test
increment to
modify the loop
Repeat step 3 – 7 until
expression is false
02/10/12
• Flow Chart
Example 3: for Construct
start
i = 1
(i <= 3)
width, height
True
i = i + 1
False
end
area = width *
height
area
02/10/12
Accumulating in Problem Solving
• Accumulating or summing is a task a program must
often perform.
• Accumulating means summing a group of numbers:
1.where a variable is added to another variable,
• which holds the value of the sum or total.
• The expression for accumulating is as follow:
sum = sum + num
total = total + marks
2 1
2 1
02/10/12
How to apply Accumulating in Problem
Solving?
Example 4: Accumulating in Sequence
Structure
• Problem Statement:
o Calculate the average of three numbers.
Problem Analysis:
Input: num1, num2, num3
Process: total = num1 + num2 + num3
average = total / 3
or
average = (num1 + num2 + num3) / 3
Output: average
Accumulating
02/10/12
How to apply Accumulating in
Looping?
Example 5: Accumulating in Looping
Structure
• Problem Statement:
o Calculate the average of three numbers.
Problem Analysis:
Input: num
Process: total = total + num
average = total / 3
Output: average
Accumulating
02/10/12
Example 5: Accumulating in Looping
Structure
Problem Analysis:
Input: num
Process: total = total + num
average = total / 3
Output: average
total i i <=
3
num total = total +
num
i = i + 1 average = total /
3
average
0 1 T 70 70 2
2 T 80 150 3
3 T 90 240 4
4 F Loop terminate average = 240 / 3 80
Using accumulating in while construct (while counter table)
02/10/12
Pseudocode for Accumulating in while
Construct:
start
1. initialize total
total = 0
2. initialize counter
i = 1
3. while (i <= 3)
4. input num
5. accumulate total
total = total + num
6. counter increment
i = i + 1
7. repeat until i > 3
8. calculate average
average = total / 3
9. print average
end
IPO analysis
initial value of counter
condition to test
increment to
modify the loop
Repeat step 3 – 6 until
expression is false
initial value of total
IPO analysis
02/10/12
Flow Chart for Accumulating in while
Construct:
start
i = 1
(i <= 3)
num
True
False
end
total = total + num
i = i + 1
total = 0
average = total / 3
average
02/10/12
Summary
• What have you learned?
o The three forms of looping control structure:
while
do..while
for
o The loop counter requirement.
o Apply looping control structure in problem
solving.
Remember to plan your algorithm.
Do Problem Analysis.
Transfer to Pseudocode or Flow Chart.
o Applying accumulating in looping.

Más contenido relacionado

La actualidad más candente

Algorithm and pseudo codes
Algorithm and pseudo codesAlgorithm and pseudo codes
Algorithm and pseudo codeshermiraguilar
 
Types of grammer - TOC
Types of grammer - TOCTypes of grammer - TOC
Types of grammer - TOCAbhayDhupar
 
Language Translator ( Compiler)
Language Translator ( Compiler)Language Translator ( Compiler)
Language Translator ( Compiler)Nazmul Hyder
 
CS304PC:Computer Organization and Architecture Session 11 general register or...
CS304PC:Computer Organization and Architecture Session 11 general register or...CS304PC:Computer Organization and Architecture Session 11 general register or...
CS304PC:Computer Organization and Architecture Session 11 general register or...Asst.prof M.Gokilavani
 
Csc1401 lecture03 - computer arithmetic - arithmetic and logic unit (alu)
Csc1401   lecture03 - computer arithmetic - arithmetic and logic unit (alu)Csc1401   lecture03 - computer arithmetic - arithmetic and logic unit (alu)
Csc1401 lecture03 - computer arithmetic - arithmetic and logic unit (alu)IIUM
 
Flow chart and pseudo code
Flow chart and pseudo code Flow chart and pseudo code
Flow chart and pseudo code Niva tharan
 
Procedure oriented programming
Procedure oriented programmingProcedure oriented programming
Procedure oriented programmingMrShahbazRafiq
 
Generations of Programming Languages
Generations of Programming LanguagesGenerations of Programming Languages
Generations of Programming LanguagesTarun Sharma
 
Basic programming concepts
Basic programming conceptsBasic programming concepts
Basic programming conceptssalmankhan570
 
Program logic and design
Program logic and designProgram logic and design
Program logic and designChaffey College
 
introduction to visual basic PPT.pptx
introduction to visual basic PPT.pptxintroduction to visual basic PPT.pptx
introduction to visual basic PPT.pptxclassall
 
Disk Scheduling Algorithm in Operating System
Disk Scheduling Algorithm in Operating SystemDisk Scheduling Algorithm in Operating System
Disk Scheduling Algorithm in Operating SystemMeghaj Mallick
 
Lect 1. introduction to programming languages
Lect 1. introduction to programming languagesLect 1. introduction to programming languages
Lect 1. introduction to programming languagesVarun Garg
 
2nd PUC Computer science chapter 5 review of c++
2nd PUC Computer science chapter 5   review of c++2nd PUC Computer science chapter 5   review of c++
2nd PUC Computer science chapter 5 review of c++Aahwini Esware gowda
 

La actualidad más candente (20)

Algorithm and pseudo codes
Algorithm and pseudo codesAlgorithm and pseudo codes
Algorithm and pseudo codes
 
Types of grammer - TOC
Types of grammer - TOCTypes of grammer - TOC
Types of grammer - TOC
 
Language Translator ( Compiler)
Language Translator ( Compiler)Language Translator ( Compiler)
Language Translator ( Compiler)
 
Vb introduction.
Vb introduction.Vb introduction.
Vb introduction.
 
Computer Programming- Lecture 3
Computer Programming- Lecture 3Computer Programming- Lecture 3
Computer Programming- Lecture 3
 
CS304PC:Computer Organization and Architecture Session 11 general register or...
CS304PC:Computer Organization and Architecture Session 11 general register or...CS304PC:Computer Organization and Architecture Session 11 general register or...
CS304PC:Computer Organization and Architecture Session 11 general register or...
 
Csc1401 lecture03 - computer arithmetic - arithmetic and logic unit (alu)
Csc1401   lecture03 - computer arithmetic - arithmetic and logic unit (alu)Csc1401   lecture03 - computer arithmetic - arithmetic and logic unit (alu)
Csc1401 lecture03 - computer arithmetic - arithmetic and logic unit (alu)
 
Flow chart and pseudo code
Flow chart and pseudo code Flow chart and pseudo code
Flow chart and pseudo code
 
Binary codes
Binary codesBinary codes
Binary codes
 
Procedure oriented programming
Procedure oriented programmingProcedure oriented programming
Procedure oriented programming
 
Generations of Programming Languages
Generations of Programming LanguagesGenerations of Programming Languages
Generations of Programming Languages
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
 
Basic programming concepts
Basic programming conceptsBasic programming concepts
Basic programming concepts
 
Program logic and design
Program logic and designProgram logic and design
Program logic and design
 
Presentation on C programming language
Presentation on C programming languagePresentation on C programming language
Presentation on C programming language
 
introduction to visual basic PPT.pptx
introduction to visual basic PPT.pptxintroduction to visual basic PPT.pptx
introduction to visual basic PPT.pptx
 
Disk Scheduling Algorithm in Operating System
Disk Scheduling Algorithm in Operating SystemDisk Scheduling Algorithm in Operating System
Disk Scheduling Algorithm in Operating System
 
Lect 1. introduction to programming languages
Lect 1. introduction to programming languagesLect 1. introduction to programming languages
Lect 1. introduction to programming languages
 
Introduction to java programming part 1
Introduction to java programming part 1Introduction to java programming part 1
Introduction to java programming part 1
 
2nd PUC Computer science chapter 5 review of c++
2nd PUC Computer science chapter 5   review of c++2nd PUC Computer science chapter 5   review of c++
2nd PUC Computer science chapter 5 review of c++
 

Destacado

The sales tax and the price of an item sold in a particular state
The sales tax and the price of an item sold in a particular stateThe sales tax and the price of an item sold in a particular state
The sales tax and the price of an item sold in a particular stateFaiz Nordin
 
Importance of Multimedia
Importance of MultimediaImportance of Multimedia
Importance of MultimediaSaíful Íslam
 
Immersive Multimedia (PowerPoint)
Immersive Multimedia (PowerPoint)Immersive Multimedia (PowerPoint)
Immersive Multimedia (PowerPoint)Blog
 
Bidang pembelajaran 4.2 Tingkatan 5
Bidang pembelajaran 4.2 Tingkatan 5Bidang pembelajaran 4.2 Tingkatan 5
Bidang pembelajaran 4.2 Tingkatan 5MOE
 
Importance of multimedia
Importance of multimediaImportance of multimedia
Importance of multimediaOnline
 
18 EXAMPLES of IMMERSIVE MULTIMEDIA
18 EXAMPLES of IMMERSIVE MULTIMEDIA18 EXAMPLES of IMMERSIVE MULTIMEDIA
18 EXAMPLES of IMMERSIVE MULTIMEDIAFarid Diah
 
Multimedia in education
Multimedia in educationMultimedia in education
Multimedia in educationMuhmmad Asif
 
Computer Systems - Input, Process, Output
Computer Systems - Input, Process, OutputComputer Systems - Input, Process, Output
Computer Systems - Input, Process, Outputcorb201
 
Algorithms and Flowcharts
Algorithms and FlowchartsAlgorithms and Flowcharts
Algorithms and FlowchartsDeva Singh
 

Destacado (14)

The sales tax and the price of an item sold in a particular state
The sales tax and the price of an item sold in a particular stateThe sales tax and the price of an item sold in a particular state
The sales tax and the price of an item sold in a particular state
 
Ch02
Ch02Ch02
Ch02
 
Importance of Multimedia
Importance of MultimediaImportance of Multimedia
Importance of Multimedia
 
Immersive Multimedia (PowerPoint)
Immersive Multimedia (PowerPoint)Immersive Multimedia (PowerPoint)
Immersive Multimedia (PowerPoint)
 
Bidang pembelajaran 4.2 Tingkatan 5
Bidang pembelajaran 4.2 Tingkatan 5Bidang pembelajaran 4.2 Tingkatan 5
Bidang pembelajaran 4.2 Tingkatan 5
 
Romantic poetry.
Romantic poetry.Romantic poetry.
Romantic poetry.
 
Importance of multimedia
Importance of multimediaImportance of multimedia
Importance of multimedia
 
8.2 Looping 1 Feb 2017
8.2 Looping 1 Feb 20178.2 Looping 1 Feb 2017
8.2 Looping 1 Feb 2017
 
18 EXAMPLES of IMMERSIVE MULTIMEDIA
18 EXAMPLES of IMMERSIVE MULTIMEDIA18 EXAMPLES of IMMERSIVE MULTIMEDIA
18 EXAMPLES of IMMERSIVE MULTIMEDIA
 
Multimedia in education
Multimedia in educationMultimedia in education
Multimedia in education
 
Computer Systems - Input, Process, Output
Computer Systems - Input, Process, OutputComputer Systems - Input, Process, Output
Computer Systems - Input, Process, Output
 
Advantages and disadvantages of multimedia
Advantages and disadvantages of multimediaAdvantages and disadvantages of multimedia
Advantages and disadvantages of multimedia
 
Algorithms and Flowcharts
Algorithms and FlowchartsAlgorithms and Flowcharts
Algorithms and Flowcharts
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchart
 

Similar a 8.2 approach in problem solving (9 hour)

Pj01 1-computer and programming fundamentals
Pj01 1-computer and programming fundamentalsPj01 1-computer and programming fundamentals
Pj01 1-computer and programming fundamentalsSasidharaRaoMarrapu
 
CSC1100 - Chapter12 - Flow Charts
CSC1100 - Chapter12 - Flow ChartsCSC1100 - Chapter12 - Flow Charts
CSC1100 - Chapter12 - Flow ChartsYhal Htet Aung
 
Ds03 part i algorithms by jyoti lakhani
Ds03 part i algorithms   by jyoti lakhaniDs03 part i algorithms   by jyoti lakhani
Ds03 part i algorithms by jyoti lakhanijyoti_lakhani
 
Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Mohamed El Desouki
 
Fundamental Programming Lect 2
Fundamental Programming Lect 2Fundamental Programming Lect 2
Fundamental Programming Lect 2Namrah Erum
 
Logic Development and Algorithm.
Logic Development and Algorithm.Logic Development and Algorithm.
Logic Development and Algorithm.NandiniSidana
 
Computer Fundamentals & Intro to C Programming module i
Computer Fundamentals & Intro to C Programming module iComputer Fundamentals & Intro to C Programming module i
Computer Fundamentals & Intro to C Programming module iAjit Nayak
 
L1. Basic Programming Concepts.pdf
L1. Basic Programming Concepts.pdfL1. Basic Programming Concepts.pdf
L1. Basic Programming Concepts.pdfMMRF2
 
Chapter0002222programming language2.pptx
Chapter0002222programming language2.pptxChapter0002222programming language2.pptx
Chapter0002222programming language2.pptxstephen972973
 

Similar a 8.2 approach in problem solving (9 hour) (20)

Algorithm.pdf
Algorithm.pdfAlgorithm.pdf
Algorithm.pdf
 
Cs 1114 - lecture-2
Cs 1114 - lecture-2Cs 1114 - lecture-2
Cs 1114 - lecture-2
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Programing Fundamental
Programing FundamentalPrograming Fundamental
Programing Fundamental
 
Lesson 2 beginning the problem solving process
Lesson 2 beginning the problem solving processLesson 2 beginning the problem solving process
Lesson 2 beginning the problem solving process
 
Practical 01 (detailed)
Practical 01 (detailed)Practical 01 (detailed)
Practical 01 (detailed)
 
Pj01 1-computer and programming fundamentals
Pj01 1-computer and programming fundamentalsPj01 1-computer and programming fundamentals
Pj01 1-computer and programming fundamentals
 
CSC1100 - Chapter12 - Flow Charts
CSC1100 - Chapter12 - Flow ChartsCSC1100 - Chapter12 - Flow Charts
CSC1100 - Chapter12 - Flow Charts
 
Ds03 part i algorithms by jyoti lakhani
Ds03 part i algorithms   by jyoti lakhaniDs03 part i algorithms   by jyoti lakhani
Ds03 part i algorithms by jyoti lakhani
 
Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++
 
Fundamental Programming Lect 2
Fundamental Programming Lect 2Fundamental Programming Lect 2
Fundamental Programming Lect 2
 
UNIT-1.pptx
UNIT-1.pptxUNIT-1.pptx
UNIT-1.pptx
 
4 design
4 design4 design
4 design
 
LMmanual.pdf
LMmanual.pdfLMmanual.pdf
LMmanual.pdf
 
Fundamentals of Programming Chapter 3
Fundamentals of Programming Chapter 3Fundamentals of Programming Chapter 3
Fundamentals of Programming Chapter 3
 
Chapter 02 - Problem Solving
Chapter 02 - Problem SolvingChapter 02 - Problem Solving
Chapter 02 - Problem Solving
 
Logic Development and Algorithm.
Logic Development and Algorithm.Logic Development and Algorithm.
Logic Development and Algorithm.
 
Computer Fundamentals & Intro to C Programming module i
Computer Fundamentals & Intro to C Programming module iComputer Fundamentals & Intro to C Programming module i
Computer Fundamentals & Intro to C Programming module i
 
L1. Basic Programming Concepts.pdf
L1. Basic Programming Concepts.pdfL1. Basic Programming Concepts.pdf
L1. Basic Programming Concepts.pdf
 
Chapter0002222programming language2.pptx
Chapter0002222programming language2.pptxChapter0002222programming language2.pptx
Chapter0002222programming language2.pptx
 

Último

Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 

Último (20)

Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 

8.2 approach in problem solving (9 hour)

  • 2. 02/10/12 • 8.2.1 Input Process Output (IPO) Analysis Approach in Problem Solving
  • 3. 02/10/12 Learning Outcome • Identify input, process and output from a given problem
  • 4. 02/10/12 Input Process Output (IPO) Analysis • The IPO is used to analyze problems and develop algorithms • Used to organize and summarize the results of a problem analysis • It shows where in the solution the processing takes place
  • 5. 02/10/12 Input Process Output (IPO) Analysis Input Processing Output Processing items: Algorithm • It can also be represent using IPO chart
  • 6. 02/10/12 Input Process Output (IPO) Analysis To do the IPO Analysis, start with: 1- Output (Display the results) 2- Input (Read the data) 3- Process (Perform the computation) Input : Process : Output : Identify
  • 7. 02/10/12 • Output should answer the following question: What does the user want to see printed on the printer, displayed on the screen, or stored in a file? Input Process Output (IPO) Analysis
  • 8. 02/10/12 • Input should answer the following question: What information will the computer need to know to print, display, or store the output items? Input Process Output (IPO) Analysis
  • 9. 02/10/12 • Processing item: An intermediate value that the algorithm uses when processing the input into the output Input Process Output (IPO) Analysis
  • 10. 02/10/12 Analyze the Problem • What is it you are trying to accomplish? • What outcome are you trying to arrive at? • List the Inputs and Outputs • Often you work backwards from the Output • List the Outputs, and then figure out what Inputs you need in order to arrive at the Outputs
  • 11. 02/10/12 Example 1 Problem statement: Calculate the area of a rectangle We can summarize the information contained in the problem statement as follows width height Analyze the Problem
  • 12. 02/10/12 Problem Analysis: To do the IPO Analysis, start with: 1 – Output 2 – Input 3 – Process INPUT : PROCESS : OUTPUT : Identify : Analyze the Problem
  • 13. 02/10/12 Problem Analysis: Input: width, height Process: area = width x height Output: area of rectangle Analyze the Problem
  • 14. 02/10/12 Input Processing Output width height Processing items: area = width x height Algorithm : area of rectangle Analyze the Problem The Problem Analysis, can also be represent using IPO (Input, Processing, Output) Chart
  • 15. 02/10/12 Example 2 Problem statement: Find the average of three numbers input by user We can summarize the information contained in the problem statement as follows Analyze the Problem
  • 16. 02/10/12 Problem Analysis: To do the IPO Analysis, start with: 1 – Output 2 – Input 3 – Process INPUT : PROCESS : OUTPUT : Identify : Analyze the Problem
  • 17. 02/10/12 Problem Analysis: Input: number1, number2, number3 Process: average = (number1 + number2 + number3) / 3 Output: average Analyze the Problem
  • 18. 02/10/12 Input Processing Output number1 number2 number3 Processing items: average = (number1 + number2 + number3) / 3 Algorithm : average Analyze the Problem The Problem Analysis, can also be represent using IPO (Input, Processing, Output) Chart
  • 19. 02/10/12 Example 3 Problem statement: Determine the total cost of apples, given the number of kilos of apples purchased and the cost of apples per kilo We can summarize the information contained in the problem statement as follows Analyze the Problem
  • 20. 02/10/12 Problem Analysis: To do the IPO Analysis, start with: 1 – Output 2 – Input 3 – Process INPUT : PROCESS : OUTPUT : Identify : Analyze the Problem
  • 21. 02/10/12 Problem Analysis: Input: Number of kilos of apples Cost of apples per kilo Process: Total cost = Number of kilos of apples × Cost of apples per kilo Output: Total cost of apples Analyze the Problem
  • 22. 02/10/12 Analyze the Problem The Problem Analysis, can also be represent using IPO (Input, Processing, Output) Chart Input Processing Output Number of kilos of apples Cost of apples per kilo Processing items: Total cost = Number of kilos of apples × Cost of apples per kilo Algorithm : Total cost of apples
  • 23. 02/10/12 Summary • Approaches in problem solving • Identify Input Process Output (IPO) from a problem statement What have you learned ?
  • 24. 02/10/12 • 8.2.2 Algorithm • 8.2.3 Pseudocode Approach in Problem Solving
  • 25. 02/10/12 Learning Outcome • Define algorithm • Solve problem using pseudocode
  • 26. 02/10/12 ALGORITHM • The algorithm is the abstract idea of solving a problem. • An algorithm is a step-by-step instructions that will transform the input into the output • It can be represent using pseudocode or flow chart
  • 27. 02/10/12 From Algorithms to Programs Problem C++ Program 1. Problem Analysis 2. Algorithm
  • 28. 02/10/12 Algorithm in everyday’s life • How to make a mug of hot coffee: 1.Start • Boil water • Prepare a mug • Put a tea spoon of coffee & sugar • Pour hot water • Stir • End
  • 29. 02/10/12 ALGORITHM can be described using three control structures (Bohm and Jacopini-1966); • A Sequence o is a series of statements that execute one after another • A Selection – (branch) o statement is used to determine which of two different statements to execute depending on certain conditions • A Looping – (repetition) o statement is used to repeat statements while certain
  • 30. 02/10/12 PLANNING THE ALGORITHM Identify: INPUT : PROCESS : OUTPUT : 2. Transfer to pseudo code or flow chart 3. a. Must start with a start b. Must close with an end 1. Do Problem Analysis
  • 31. 02/10/12 TECHNIQUES TO REPRESENT THE ALGORITHM Pseudocode Flow chart • Artificial, informal language used to develop algorithms • Similar to everyday English • Graphical representation of an algorithm • Special-purpose symbols connected by arrows (flow lines) 1.Pseudocod e 2.Flow Chart
  • 32. 02/10/12 TECHNIQUES TO REPRESENT THE ALGORITHM Calculate the area of a circle Pseudocode Flow chart Start: Input radius Calculate circle area circle area = 3.142 * radius * radius Print the circle area End: 1. Example of Pseudocode & Flow chart radius circle area = 3.142 * radius * radius circle area start end
  • 33. 02/10/12 1. Pseudocode - Pseudocode Format start statement 1 statement 2 end 1 3 2 TECHNIQUES TO REPRESENT THE ALGORITHM
  • 34. 02/10/12 Algorithm Design : Pseudocode Example 1 Problem statement: Calculate the area of a rectangle Remember! 1. Do Problem Analysis 2. Transfer to pseudo code or flow chart
  • 35. 02/10/12 1. Problem Analysis: Input: width, height Process: area = width x height Output: area of rectangle Algorithm Design : Pseudocode
  • 36. 02/10/12 Algorithm Design : Pseudocode start read width and height calculate area area of rectangle = width * height print area of rectangle end 2. Pseudocode
  • 37. 02/10/12 IPO Chart with Pseudocode Input Processing Output width height Processing items: area = width x height Algorithm - Pseudocode: Start Read width and height Calculate area area of rectangle = width * height Print area End area of rectangle
  • 38. 02/10/12 Algorithm Design : Pseudocode Example 2 Problem statement: Find the average of three numbers input by user Remember! 1. Do Problem Analysis 2. Transfer to pseudo code or flow chart
  • 39. 02/10/12 1. Problem Analysis: Input: number1, number2, number3 Process: average = (number1 + number2 + number3) / 3 Output: average Algorithm Design : Pseudocode
  • 40. 02/10/12 Algorithm Design : Pseudocode start input number1, number2, number3 calculate average average = (number1 + number2 + number3) / 3 print average end 2. Pseudocode
  • 41. 02/10/12 IPO Chart with Pseudocode Input Processing Output number1 number2 number3 Processing items: average = (number1 + number2 + number3) / 3 Algorithm - Pseudocode: Start Input number1, number2, number3 Calculate average average = (number1 + number2 + number3) / 3 Print average End average
  • 42. 02/10/12 Algorithm Design : Pseudocode Example 3 Problem statement: Determine the total cost of apples, given the number of kilos of apples purchased and the cost of apples per kilo Remember! 1. Do Problem Analysis 2. Transfer to pseudo code or flow chart
  • 43. 02/10/12 1. Problem Analysis: Input: Number of kilos of apples Cost of apples per kilo Process: Total cost = Number of kilos of apples × Cost of apples per kilo Output: Total cost of apples Algorithm Design : Pseudocode
  • 44. 02/10/12 Algorithm Design : Pseudocode start Read Number of kilos of apples and Cost of apples per kilo Calculate Total cost Total cost = Number of kilos of apples x Cost of apples per kilo Print Total cost of apples end 2. Pseudocode
  • 45. 02/10/12 Input Processing Output Number of kilos of apples Cost of apples per kilo Total cost = Number of kilos of apples × Cost of apples per kilo Algorithm - Pseudocode: Start Read Number of kilos of apples and Cost of apples per kilo Calculate Total Cost Total cost = Number of kilos of apples × Cost of apples per kilo Print Total cost of apples End Total cost of apples IPO Chart with Pseudocode
  • 46. 02/10/12 Summary • Approaches in problem solving • Planning the algorithm • Steps in building pseudocode • Problem solving using pseudocode What have you learned ?
  • 47. 02/10/12 • 8.2.4 Flow Chart Approach in Problem Solving
  • 48. 02/10/12 Learning Outcome • Solve problem using flow chart
  • 49. 02/10/12 2. Flow Chart - Flow Chart Format TECHNIQUES TO REPRESENT THE ALGORITHM Statement 1 Statement 2 The statement refers to any input, output & process involved start end
  • 50. 02/10/12 FLOW CHART - SYMBOLS Graphic Symbol Name Meaning Terminal Symbol (oval) indicates the beginning and end points of an algorithm Process Symbol (rectangle) shows an instruction other than input, output or selection Input-Output Symbol (parallelogram) shows an input or output operation
  • 51. 02/10/12 FLOW CHART - SYMBOLS Graphic Symbol Name Meaning Disk Storage Input-Output Symbol (cylinder) indicates input from or output to disk storage Printer Output Symbol shows hardcopy printer output Selection Symbol (diamond) shows a selection process for two- way selection
  • 52. 02/10/12 FLOW CHART - SYMBOLS Graphic Symbol Name Meaning Flow Lines (arrow) indicates the logical sequence of execution steps in the algorithm Off-Page Connector provides continuation of a logical path on another page On-Page Connector (circle) provides continuation of a logical path at another point in the same page
  • 53. 02/10/12 Algorithm Design : Flow Chart Example 1 Problem statement: Calculate the area of a rectangle Remember! 1. Do Problem Analysis 2. Transfer to pseudo code or flow chart
  • 54. 02/10/12 1. Problem Analysis: Input: width, height Process: area = width x height Output: area of rectangle Algorithm Design : Flow Chart
  • 55. 02/10/12 Algorithm Design : Flow Chart 2. Flow Chart area = width * height width, height area start end
  • 56. 02/10/12 IPO Chart with Flow Chart Input Processing Output width height Processing items: area = width x height Algorithm – Flow Chart: area of rectangle area = width * height width, height area start end
  • 57. 02/10/12 Algorithm Design : Flow Chart Example 2 Problem statement: Find the average of three numbers input by user Remember! 1. Do Problem Analysis 2. Transfer to pseudo code or flow chart
  • 58. 02/10/12 1. Problem Analysis: Input: number1, number2, number3 Process: average = (number1 + number2 + number3) / 3 Output: average Algorithm Design : Flow Chart
  • 59. 02/10/12 Algorithm Design : Flow Chart 2. Flow Chart average = (number1 + number2 + number3) / 3 number1, number2, number3 average start end
  • 60. 02/10/12 IPO Chart with Flow Chart Input Processing Output number1 number2 number3 Processing items: average = (num1 + num2 + num3) / 3 Algorithm – Flow Chart: average average = (number1 + number2 + number3) / 3 number1, number2, number3 average start end
  • 61. 02/10/12 Algorithm Design : Flow Chart Example 3 Problem statement: Determine the total cost of apples, given the number of kilos of apples purchased and the cost of apples per kilo Remember! 1. Do Problem Analysis 2. Transfer to pseudo code or flow chart
  • 62. 02/10/12 1. Problem Analysis: Input: Number of kilos of apples Cost of apples per kilo Process: Total cost = Number of kilos of apples × Cost of apples per kilo Output: Total cost of apples Algorithm Design : Flow Chart
  • 63. 02/10/12 Algorithm Design : Flow Chart 2. Flow Chart Number of kilos of apples, Cost of apples per kilo Total cost = Number of kilos of apples x Cost of apples per kilo Total cost of apples end start
  • 64. 02/10/12 IPO Chart with Flow Chart Input Processing Output Number of kilos of apples Cost of apples per kilo Total cost = Number of kilos of apples × Cost of apples per kilo Algorithm – Flow Chart: Total cost of apples Number of kilos of apples, Cost of apples per kilo Total cost = Number of kilos of apples x Cost of apples per kilo Total cost of apples end start
  • 65. 02/10/12 Summary • Approaches in problem solving • Steps in building flow chart • Problem solving using flow chart What have you learned ?
  • 66. 02/10/12 Revision • What is IPO analysis? • What is algorithm? • What are the steps in planning your algorithm? • Differentiate between pseudocode and flowchart. What have you learned ?
  • 67. 02/10/12 8.2 Approach in Problem Solving 8.2.5 Control Structure 8.2.5.2 Sequence
  • 68. 02/10/12 8.2.5.1 Sequence Learning Outcome At the end of the lesson, students should be able to : 1. Understand basic problem solving techniques. • Use the sequence structured in problem solving. • Develop flowchart through the process of top-down.
  • 69. 02/10/12 8.2.5.1 Sequence • The simplest programs consist just sequence of statements : • no loops, no selections amongst alternative actions, no use of subroutines.
  • 70. 02/10/12 8.2.5.1 Sequence • Instruction in sequence programming are executed sequentially one by one • The sequence structure directs the computer to process the instructions, one after another, in the order listed in the program. first instruction last instruction.
  • 72. 02/10/12 8.2.5.1 Sequence • Planning the Algorithm • Record the algorithm using IPO analysis / IPO chart. Problem Analysis: Input: sales Process: bonus = sales * (0.05) Output: bonus
  • 73. 02/10/12 Algorithm Design : Pseudocode start input sales bonus = sales * (0.05) print bonus end Pseudocode -tool programmers use to help them plan an algorithm -consist of short, English-like statements
  • 74. 02/10/12 Algorithm Design : Flow Chart Flow Chart - Use standardized symbols to show the steps the computer need to take to accomplish the program’s goal start bonus = sales * (0.05) end sales bonus
  • 76. 02/10/12 8.2.5.1 Sequence Problem Analysis: Input: width, length, price of tile Process: area = width * length total of price = area * price of tile Output: total of price • Planning the Algorithm • Record the algorithm using IPO analysis / IPO chart.
  • 77. 02/10/12 Algorithm Design : Pseudocode start input width, length, price of tile area = width * length total price of tile = area * price of tile print total price of tile end Pseudocode -tool programmers use to help them plan an algorithm -consist of short, English-like statements
  • 78. 02/10/12 Algorithm Design : Flow Chart start area = width * length total price of tile = area * price of tile end width length price of tile Total price of tile Flow Chart - Use standardized symbols to show the steps the computer need to take to accomplish the program’s goal
  • 80. 02/10/12 8.2.5.1 Sequence Problem Analysis: Input: hour Process: Total overtime payment = hour * 5.00 Output: total overtime payment • Planning the Algorithm • Record the algorithm using IPO analysis / IPO chart.
  • 81. 02/10/12 Algorithm Design : Pseudocode start input hour total overtime payment = hour * 5.00 print total overtime payment end Pseudocode -tool programmers use to help them plan an algorithm -consist of short, English-like statements
  • 82. 02/10/12 Algorithm Design : Flow Chart start Total overtime payment = hour * 5.00 end hour Total overtime payment Flow Chart - Use standardized symbols to show the steps the computer need to take to accomplish the program’s goal
  • 83. 02/10/12 Summary • Instruction in sequence programming are executed sequentially one by one • First step in the problem-solving process it to analyze the problem • Planning the algorithm is using IPO analysis / IPO chart • Programmer uses the pseudocode as a guide when coding the algorithm • Flow chart uses standardized symbols to show the steps the computer needs to take to accomplish the program’s goal
  • 84. 02/10/12 8.0 PROGRAMMING 8.2: Approach in Problem Solving 8.2.5: Control Structure 8.2.5.2  Selection
  • 85. 02/10/12 • At the end of this topic, students should be able to: a) explain the purpose of selection control structure. b) apply selection control structure in problem solving. Learning Outcome
  • 86. 02/10/12 Control Structure : Selection SELECTION Analogy You need to choose to make “Coffee O” or “Milk Coffee”
  • 87. 02/10/12 Control Structure : Selection SEQUENC E Control Structures SELECTION LOOPING
  • 88. 02/10/12 What is Selection Structure? • The selection structure allows instructions to be executed non-sequentially. • It allows the comparison of two expressions, and based on the comparison, to select certain course of action. Control Structure : Selection
  • 89. 02/10/12 Control Structure : Selection if-else Types of selection structure nested if-else switch
  • 90. 02/10/12 • If-else structure’s form if (expression) statement_1 else statement_2 Selection  (1) if-else
  • 91. 02/10/12 • In this form, the expression is first evaluated. • If it evaluates to non-zero (TRUE), statement_1 is executed. • Otherwise, statement_2 is executed. • Either statement_1 or statement_2 is executed but not BOTH. Selection  (1) if-else
  • 92. 02/10/12 • Pseudocode format: Selection  (1) if-else start if (expression) statement_1 else statement_2 end
  • 93. 02/10/12 • Flow chart format: Selection  (1) if-else
  • 94. 02/10/12 • Example 1: Print “Excellent!” when user enter marks greater than and equal to 80, else print “Sorry, try again”. Selection  (1) if-else Remember to plan your algorithm! 1.Do IPO Analysis • Transfer to Pseudocode or Flow Chart
  • 95. 02/10/12 • Example 1: Print “Excellent!” when user enter marks greater than and equal to 80, else print “Sorry, try again”. Selection  (1) if-else
  • 96. 02/10/12 IPO Analysis: Input: marks Process: if (marks >= 80) print “Excellent” else print “Sorry, try again” Output: “Excellent!” or “Sorry, try again” Selection  (1) if-else
  • 97. 02/10/12 • Pseudocode Selection  (1) if-else start read marks if (marks >= 80) print “Excellent!” else print “Sorry, try again” end
  • 98. 02/10/12 • Flow chart start end marks “Excellent!” marks >= 80 true “Sorry, try again” false Selection  (1) if-else
  • 99. 02/10/12 • Example 2: A high school poetry competition is open only for students above 15 years old. Display “Eligible” if the students meet the requirement, else display “Not eligible” if otherwise. Selection  (1) if-else
  • 100. 02/10/12 • Example 2: A high school poetry competition is open only for students above 15 years old. Display “Eligible” if the students meet the requirement, else display “Not eligible” if otherwise. Selection  (1) if-else
  • 101. 02/10/12 IPO Analysis: Input: age Process: if (age > 15) print “Eligible” else print “Not eligible” Output: “Eligible” or “Not eligible” Selection  (1) if-else
  • 102. 02/10/12 • Pseudocode Selection  (1) if-else start read age if (age > 15) print “Eligible” else print “Not eligible” end
  • 103. 02/10/12 • Flow chart start end age “Eligible” age > 15 true “Not eligible” false Selection  (1) if-else
  • 104. 02/10/12 • Example 3: If x is greater than y, display “x is bigger than y” else display “x is smaller than y”. Selection  (1) if-else
  • 105. 02/10/12 • Example 3: If x is greater than y, display “x is bigger than y” else display “x is smaller than y”. Selection  (1) if-else
  • 106. 02/10/12 IPO Analysis: Input: x,y Process: if (x > y) print “x is bigger than y” else print “x is smaller than y” Output: “x is bigger than y” OR “x is smaller than y” Selection  (1) if-else
  • 107. 02/10/12 • Pseudocode Selection  (1) if-else start read x,y if (x > y) print “x is bigger than y” else print “x is smaller than y” end
  • 108. 02/10/12 • Flow chart start end x,y “x is bigger than y” x > y true “x is smaller than y” false Selection  (1) if-else
  • 109. 02/10/12 Selection  (2) nested if-else if-else Types of selection structure nested if- else switch
  • 110. 02/10/12 • The if-else structure can also be nested to any depth. Selection  (2) nested if-else
  • 111. 02/10/12 • The nested if-else if structured takes the general form: if (expression_1) statement_1; else if (expression_2) statement_2; else if (expression_3) statement_3; else statement_4; Selection  (2) nested if-else
  • 112. 02/10/12 • In this nested form, expression_1 is evaluated. If it evaluates to non-zero (TRUE), statement_1 is executed. • If not, control goes to the second if, where expression_2 is evaluated. If it evaluates to non-zero (TRUE), statement_2 is executed. • If not, control goes to the third if, where expression_3 is evaluated. If it evaluates to non-zero (TRUE), statement_3 is executed. Selection  (2) nested if-else
  • 113. 02/10/12 • If not, statement_4 is executed. • Rules  Only ONE of the statements is executed. Selection  (2) nested if-else
  • 114. 02/10/12 • Example 1: if student’s grade is greater than or equal to 80 Print “A” else if student’s grade is greater than or equal to 60 Print “B” else if student’s grade is greater than or equal to 50 Print “C” else Print “Failed” Selection  (2) nested if-else
  • 115. 02/10/12 IPO Analysis: Input: grade Process: if (grade >= 80) print “A” else if (grade >= 60) print “B” else if (grade >= 50) print “C” else print “Failed” Output: “A” or “B” or “C” or “Failed” Selection  (2) nested if-else
  • 116. 02/10/12 • Pseudocode Selection  (2) nested if-else start read grade if (grade >= 80) print “A” else if (grade >= 60) print “B” else if (grade >= 50) print “C” else print “Failed” end
  • 117. 02/10/12 • Flow chart Selection  (2) nested if-else start end grade grade >= 80 true “A” false grade >= 60 grade >= 50 “B” “C” false false “Failed” true true
  • 118. 02/10/12 Selection  (2) nested if-else • Example 2: if (x > 0) display "x is positive" else if (x < 0) display "x is negative" else display "x is 0"
  • 119. 02/10/12 Selection  (2) nested if-else IPO Analysis: Input: x Process: if (x > 0) display "x is positive" else if (x < 0) display "x is negative" else display "x is 0“ Output: “x is positive“ or "x is negative“ or “x is 0”
  • 120. 02/10/12 Selection  (2) nested if-else • Pseudo code start read x if (x > 0) display "x is positive" else if (x < 0) display "x is negative" else display "x is 0" end
  • 121. 02/10/12 Selection  (2) nested if-else • Flow chart start end x (x > 0) true "x is positive” false (x < 0) “x is negative” false “x is 0” true
  • 122. 02/10/12 Selection  (3) Switch if-else Types of selection structure nested if-else switch
  • 123. 02/10/12 • Switch case statements are to check several possible constant values for an expression. • Switch form: switch (expression) { case constant1: group of statements1 break case constant2: group of statements2 break . . . default: default group of statements } Selection  (3) Switch
  • 124. 02/10/12 • switch evaluates expression and checks if it is equivalent to constant1, if it is, it executes group of statements1 until it finds the break statement. Selection  (3) Switch
  • 125. 02/10/12 • If expression was not equal to constant1 it will be checked against constant2. If it is equal to this, it will execute statements2. Selection  (3) Switch
  • 126. 02/10/12 • If the value of expression DID NOT match any of the previously specified constants (you can include as many case labels as values you want to check), the program will execute the statements included after the default. Selection  (3) Switch
  • 127. 02/10/12 iv) Selection - switch switch Flow chart
  • 128. 02/10/12 • Example 1: Selection  (3) Switch x Print 1 X is 1 2 X is 2 Other value Value of x unknown
  • 129. 02/10/12 Selection  (3) Switch IPO Analysis: Input: x Process: switch (x) { case 1: print "x is 1“ break case 2: print "x is 2“ break default: Print "value of x unknown” } Output: "x is 1" or "x is 2" or "value of x unknown”
  • 130. 02/10/12 Selection  (3) Switch • Pseudocode start read x switch (x) { case 1: print "x is 1“ break case 2: print "x is 2" break default: print "value of x unknown” } end
  • 131. 02/10/12 b) Flow chart iv) Selection - switch
  • 132. 02/10/12 SUMMARY if-else Types of selection structure nested if- else switch * Use an appropriate selection control structure in problem solving
  • 133. 02/10/12 Topic: 8.2 Approach in Problem Solving 8.2.5: Control Structure 8.2.5.3: Looping
  • 134. 02/10/12 Learning Outcome At the end of this topic, students should be able to: • explain the purpose of looping control structure. • apply looping control structure in problem solving.
  • 135. 02/10/12 What is looping control structure? • The looping (or repetition) structure allows a sequence of instructions to be executed repeatedly until a certain condition is reached. • The looping structure has three forms: o while o do..while o for
  • 136. 02/10/12 The essentials of looping (repetition) • The loop counter (loop control variable ): o Counts the number of repetitions in the counter- controlled loop o Controls the execution of loop • The loop counter requires the following to be known in advance: 1.Name of a loop counter • Initial value of the loop counter • Increment (or decrement) by which to modify the loop counter in each traversal • Condition to test the final value of loop counter
  • 137. 02/10/12 1. The while Construct The looping control structure:
  • 138. 02/10/12 The while Construct • The while construct has the general form: while (expression) statement; • where the expression is first evaluated. • If it is true (not zero), the *statement(s) is executed, • else if it false (zero), the statement is bypassed. * note: statement(s); which can be a block of statements – more than 1 statement
  • 139. 02/10/12 Applying Algorithm in while Construct • Pseudocode start initialize counter while (expression) statement(s) counter increment end 12 4 3 * note: please refer to slide #4 for numbering details
  • 140. 02/10/12 Applying Algorithm in while Construct • Flow Chart start initialize counter while (expression) statement(s) True counter increment False end
  • 141. 02/10/12 The Counter Table for while Construct counter expression input process output counter increment initial value of counter condition to test IPO analysis increment to modify the loop i i <= 3 input process output i = i + 1 xample of while counter table:
  • 142. 02/10/12 Example 1: while Construct • Problem Statement: o Calculate the area of a rectangle for 3 times. • Remember to plan your algorithm. o Do Problem Analysis. o Transfer to Pseudocode or Flow Chart. width height
  • 143. 02/10/12 i i <= 3 width,height area = width * height area i = i + 1 while counter table Example 1: while Construct Problem Analysis: Input: width, height Process: area = width * height Output: area
  • 144. 02/10/12 i i <= 3 width,height area = width * height area i = i + 1 1 T 2 2 T 3 3 T 4 4 F Loop terminate while counter table Example 1: while Construct Problem Analysis: Input: width, height Process: area = width * height Output: area
  • 145. 02/10/12 • Pseudocode Example 1: while Construct start 1. initialize counter i = 1 2. while (i <= 3) 3. input width, height 4. calculate area of rectangle area = width * height 5. print area 6. counter increment i = i + 1 7. repeat until i > 3 end IPO analysis initial value of counter condition to test increment to modify the loop Repeat step 2 – 6 until expression is false
  • 146. 02/10/12 • Flow Chart Example 1: while Construct start i = 1 (i <= 3) width, height True i = i + 1 False end area = width * height area
  • 147. 02/10/12 2. The do..while Construct The looping control structure:
  • 148. 02/10/12 The do..while Construct • The do..while construct has the general form: do statement while (expression); • where the expression is last evaluated, after the statement(s) is executed. • This means the statement(s) in the do..while will be executed at least once.
  • 149. 02/10/12 Applying Algorithm in do..while Construct • Pseudocode start initialize counter do statement(s) counter increment while (expression) end 12 4 3 * note: please refer to slide #4 for numbering details
  • 150. 02/10/12 Applying Algorithm in do..while Construct • Flow Chart start initialize counter while (expression) statement(s) True counter increment False end
  • 151. 02/10/12 The Counter Table for do..while Construct counter input process output counter increment expression initial value of counter condition to test IPO analysis increment to modify the loop i input process output i = i + 1 i <= 3 ample of do..while counter table:
  • 152. 02/10/12 Example 2: do..while Construct • Problem Statement: o Calculate the area of a rectangle for 3 times. • Remember to plan your algorithm. o Do Problem Analysis. o Transfer to Pseudocode or Flow Chart. width height
  • 153. 02/10/12 i width,height area = width * height area i = i + 1 i <= 3 do..while counter table Example 2: do..while Construct Problem Analysis: Input: width, height Process: area = width * height Output: area
  • 154. 02/10/12 i width,height area = width * height area i = i + 1 i <= 3 1 2 T 2 3 T 3 4 F Loop terminate do..while counter table Example 2: do..while Construct Problem Analysis: Input: width, height Process: area = width * height Output: area
  • 155. 02/10/12 • Pseudocode Example 2: do..while Construct start 1. initialize counter i = 1 2. do 3. input width, height 4. calculate area of rectangle area = width * height 5. print area 6. counter increment i = i + 1 7. repeat while (i <= 3) end IPO analysis initial value of counter condition to test increment to modify the loop
  • 156. 02/10/12 • Flow Chart Example 2: do..while Construct start i = 1 (i <= 3) width, height True i = i + 1 False end area = width * height area
  • 157. 02/10/12 3. The for Construct The looping control structure:
  • 158. 02/10/12 The for Construct • The for construct has the general form: for (initialization; expression; incrementation) statement; • where the initialization refers to the initial value of a loop counter. • the expression determines whether the loop should be continued; if it is true (not zero), the *statement(s) is executed, else if it false (zero), the for loop is terminated. • The incrementation increments the loop counter. * note: statement(s); which can be a block of statements – more than 1 statement
  • 159. 02/10/12 Applying Algorithm in for Construct • Pseudocode start for (initialize counter; expression; counter increment) statement(s); end 2 * note: please refer to slide #4 for numbering details 1 4 3
  • 160. 02/10/12 The for loop is executed as follows: start for (initialize counter; expression; counter increment) statement(s); end o After one-time initialization, the expression is first evaluated. o If it is false, the for loop is terminated; if it is true the statement(s) in the loop is executed. o And then the incrementation is performed. o The expression condition is again evaluated, until the expression becomes false.
  • 161. 02/10/12 Applying Algorithm in for Construct • Flow Chart start initialize counter (expression) statement(s) True counter increment False end
  • 162. 02/10/12 The Counter Table for for Construct counter expression input process output counter increment initial value of counter condition to test IPO analysis increment to modify the loop i i <= 3 input process output i = i + 1 xample of for counter table:
  • 163. 02/10/12 Example 3: for Construct • Problem Statement: o Calculate the area of a rectangle for 3 times. • Remember to plan your algorithm. o Do Problem Analysis. o Transfer to Pseudocode or Flow Chart. width height
  • 164. 02/10/12 i i <= 3 width,height area = width * height area i = i + 1 for counter table Example 3: for Construct Problem Analysis: Input: width, height Process: area = width * height Output: area
  • 165. 02/10/12 i i <= 3 width,height area = width * height area i = i + 1 1 T 2 2 T 3 3 T 4 4 F Loop terminate for counter table Example 3: for Construct Problem Analysis: Input: width, height Process: area = width * height Output: area
  • 166. 02/10/12 • Pseudocode Example 3: for Construct start 1. initialize counter i = 1 2. for (i <= 3) 3. read width, height 4. calculate area of rectangle area = width * height 5. print area 6. counter increment i = i + 1 7. repeat until i > 3 end IPO analysis initial value of counter condition to test increment to modify the loop Repeat step 3 – 7 until expression is false
  • 167. 02/10/12 • Flow Chart Example 3: for Construct start i = 1 (i <= 3) width, height True i = i + 1 False end area = width * height area
  • 168. 02/10/12 Accumulating in Problem Solving • Accumulating or summing is a task a program must often perform. • Accumulating means summing a group of numbers: 1.where a variable is added to another variable, • which holds the value of the sum or total. • The expression for accumulating is as follow: sum = sum + num total = total + marks 2 1 2 1
  • 169. 02/10/12 How to apply Accumulating in Problem Solving? Example 4: Accumulating in Sequence Structure • Problem Statement: o Calculate the average of three numbers. Problem Analysis: Input: num1, num2, num3 Process: total = num1 + num2 + num3 average = total / 3 or average = (num1 + num2 + num3) / 3 Output: average Accumulating
  • 170. 02/10/12 How to apply Accumulating in Looping? Example 5: Accumulating in Looping Structure • Problem Statement: o Calculate the average of three numbers. Problem Analysis: Input: num Process: total = total + num average = total / 3 Output: average Accumulating
  • 171. 02/10/12 Example 5: Accumulating in Looping Structure Problem Analysis: Input: num Process: total = total + num average = total / 3 Output: average total i i <= 3 num total = total + num i = i + 1 average = total / 3 average 0 1 T 70 70 2 2 T 80 150 3 3 T 90 240 4 4 F Loop terminate average = 240 / 3 80 Using accumulating in while construct (while counter table)
  • 172. 02/10/12 Pseudocode for Accumulating in while Construct: start 1. initialize total total = 0 2. initialize counter i = 1 3. while (i <= 3) 4. input num 5. accumulate total total = total + num 6. counter increment i = i + 1 7. repeat until i > 3 8. calculate average average = total / 3 9. print average end IPO analysis initial value of counter condition to test increment to modify the loop Repeat step 3 – 6 until expression is false initial value of total IPO analysis
  • 173. 02/10/12 Flow Chart for Accumulating in while Construct: start i = 1 (i <= 3) num True False end total = total + num i = i + 1 total = 0 average = total / 3 average
  • 174. 02/10/12 Summary • What have you learned? o The three forms of looping control structure: while do..while for o The loop counter requirement. o Apply looping control structure in problem solving. Remember to plan your algorithm. Do Problem Analysis. Transfer to Pseudocode or Flow Chart. o Applying accumulating in looping.

Notas del editor

  1. Another name of the method of solution
  2. Another name of the method of solution
  3. Pseudocode - a tool programmers use to help them plan an algorithm Flowchart - uses standardized symbols to show the steps the computer needs to take to accomplish the program’s goal