Professional Documents
Culture Documents
DECISION-BASED ALGORITHMS
Agenda
Lateral Thinking Warm-up
Review Pseudocode & Flowchart Basics Discuss Conditions for Decisions Develop Notation for Conditions Develop Algorithms Incorporating Conditions Write Code to Accommodate Conditions Project Work
Pseudocode Review
Pseudo-code is structured English that states
Statements are written in simple English Each instruction/step is written on a separate line Keywords and indentation are used to signify particular control structures Each set of instructions is written from top to bottom, with only one entry and one exit
Pseudocode in Programs
Start Program # Prompt for Assignment Name # Get Assignment Name and assign to variable
# Loop through Students 1-12 # prompt for grade # get grade and assign to variable # End Loop
Process Symbol- any single process, such as assigning a value or performing a calculation:
Decision Symbol- comparisons/ T or F decision:
arrow head suggests flow of data a straight line may be used to indicate a relationship
start
Sample FlowChart
Pseudocode
# Start Program
# Prompt for assignment # Set a counter # Loop through grades
# Get student grade # Increment counter
Assignment
name?
Set counter = 1
Counter>10?
Student
Grade? No
Yes
Increment Counter
Calculate Average
# End Program
Print Average
end
Conditions
Example:
Input: 2 integers Choose result type: sum, difference, product,
Conditional Operators
Relational
Logical
Relational Operators
How does one item relate or compare to another?
a>b
a<b
a=b a<>b
a>=b
a<=b
Relational Operators
How does one item relate or compare to another?
a>b false true false
a<b
a=b
a<>b true
a>=b false
a<=b true
Logical Operators
For complex conditions
AND OR
Consider a=Today is Tuesday b=It is Raining What is the effect on each of the conditions?
NOT
A AND B A OR B
NOT A
A OR NOT B
Exercise 8-1
Handout
Evaluate Conditional Operators Excel solutions
Dual Condition
if else end if
Multiple Condition
select/ case
Notation - Single
Pseudocode
START
Prompt for input Get input x If x>4 then
Display Hurray
Flowchart
start
get x
End if
T
x>4?
Print Hurray
END
F
end
Notation - Dual
Pseudocode
START
Prompt for input Get input x If x>4 then
Display Hurray
Flowchart
start
Get x
else
Display Sorry
T
x>4?
Print Hurray
F
Print Sorry
End if
END
end
Get x F eval x T
Print close
F
X=2 X=1
X=3
T
Print very close
T
Print bang on Print not even close
end
START Prompt for input Get input x CASE FOR X case x= 3 print close case x= 2 print very close case x= 1 print bang on case other print not even close END CASE END
Exercise 8-2
Handout
Develop Pseudocode and Flowchart
Logical && (AND), || (OR), ! (NOT) Requires parenthesis E.g. if (A>6 && C!=Math)
Ruby Demo
functional (6) 2. Application functions all work with appropriate and intuitive prompts (8) 3. Application presents an attractive output and then exits cleanly without any errors. (6) (hint: pseudocode will allow me to substitute for any lost marks)
Project Work
Total value=20% Due by start of class next week Prefer you zip the file and email to me
Otherwise Ill copy it to my flash disk at start
of class
Summary
Review Notation
Pseudocode and flowcharts Conditional logic Relational operators Logical operators
Ruby language
Project evaluation
Questions?
Enjoy the holiday
Lest we forget