Class 8 Lecture Notes

9,419 views

Published on

This week's topic is Conditional Logic. The exercises presented will help you to understand and applyconditional operators to the development of solutions and algorithms.

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
9,419
On SlideShare
0
From Embeds
0
Number of Embeds
34
Actions
Shares
0
Downloads
66
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • Programmatic Algorithms will be interested in efficiency and effectiveness – elegance
  • Class 8 Lecture Notes

    1. 1. Class 8 – Conditional Logic
    2. 2. Agenda <ul><li>Review Pseudocode & Flowchart Basics </li></ul><ul><li>Discuss Conditions for Decisions </li></ul><ul><li>Develop Notation for Conditions </li></ul><ul><li>Develop Algorithms Incorporating Conditions </li></ul><ul><li>Write Code to Accommodate Conditions </li></ul><ul><li>Math Review </li></ul><ul><li>Quest: Project Work </li></ul>
    3. 3. Pseudocode <ul><li>Pseudo-code is structured English that states the steps to the problem solution </li></ul><ul><ul><li>Statements are written in simple English </li></ul></ul><ul><ul><li>Each instruction/step is written on a separate line </li></ul></ul><ul><ul><li>Keywords and indentation are used to signify particular control structures </li></ul></ul><ul><ul><li>Each set of instructions is written from top to bottom, with only one entry and one exit </li></ul></ul>
    4. 4. Pseudocode in Programs <ul><li>Start Program </li></ul><ul><li># Prompt for Assignment Name </li></ul><ul><li># Get Assignment Name and assign to variable </li></ul><ul><li># For Student 1-12 </li></ul><ul><li># prompt for grade </li></ul><ul><li># get grade and assign to variable </li></ul><ul><li># End Loop </li></ul>
    5. 5. Reviewing Flow Chart Notation <ul><li>Terminal symbol- indicates the starting or stopping point of logic: </li></ul><ul><li>Input/Output symbol- reading or writing input/output: </li></ul><ul><li>Process Symbol- any single process, such as assigning a value or performing a calculation: </li></ul><ul><li>Decision Symbol- comparisons/ T or F decision: </li></ul><ul><li>Flowlines- connect symbols in the flowchart: </li></ul><ul><ul><li>arrow head suggests flow of data </li></ul></ul><ul><ul><li>a straight line may be used to indicate a relationship </li></ul></ul>
    6. 6. Sample FlowChart <ul><li>Pseudocode </li></ul><ul><ul><li>Start Program </li></ul></ul><ul><ul><li>Prompt for assignment </li></ul></ul><ul><ul><li>Loop through grades </li></ul></ul><ul><ul><ul><li>Get student grade </li></ul></ul></ul><ul><ul><ul><li>Increment counter </li></ul></ul></ul><ul><ul><li>End loop </li></ul></ul><ul><ul><li>Calculate Average </li></ul></ul><ul><ul><li>Print Average </li></ul></ul><ul><ul><li>End Program </li></ul></ul>start end Assignment name? Set counter = 1 Counter>10? Calculate Average Print Average Increment Counter Student Grade? Yes No
    7. 7. Conditions <ul><li>Example: </li></ul><ul><ul><li>Input: 2 integers </li></ul></ul><ul><ul><li>Output: sum, difference, product and quotient </li></ul></ul><ul><ul><li>Processing:? </li></ul></ul><ul><li>Conditional Operators </li></ul><ul><ul><li>Relational </li></ul></ul><ul><ul><li>Logical </li></ul></ul>
    8. 8. Relational Operators <ul><li>How does one item relate or compare to another? </li></ul><ul><ul><li>a>b </li></ul></ul><ul><ul><li>a<b </li></ul></ul><ul><ul><li>a=b </li></ul></ul><ul><ul><li>a<>b </li></ul></ul><ul><ul><li>a>=b </li></ul></ul><ul><ul><li>a<=b </li></ul></ul>Consider a=12 b=43 What is the effect on each of the conditions?
    9. 9. Logical Operators <ul><li>For complex conditions </li></ul><ul><ul><li>AND </li></ul></ul><ul><ul><li>OR </li></ul></ul><ul><ul><li>NOT </li></ul></ul><ul><ul><li>A AND B </li></ul></ul><ul><ul><li>A OR B </li></ul></ul><ul><ul><li>NOT A </li></ul></ul><ul><ul><li>A OR NOT B </li></ul></ul>Consider a=Today is Tuesday b=It is Raining What is the effect on each of the conditions?
    10. 10. Exercise 8-1 <ul><li>Handout </li></ul><ul><li>Evaluate Conditional Operators </li></ul><ul><li>Excel Example </li></ul>
    11. 11. Branching <ul><li>Single Condition </li></ul><ul><ul><li>if - then </li></ul></ul><ul><li>Dual Condition </li></ul><ul><ul><li>if – else – end if </li></ul></ul><ul><li>Multiple Condition </li></ul><ul><ul><li>select/ case </li></ul></ul>
    12. 12. Notation - Single <ul><li>Pseudocode </li></ul><ul><ul><li>START </li></ul></ul><ul><ul><ul><li>Prompt for input </li></ul></ul></ul><ul><ul><ul><li>Get input x </li></ul></ul></ul><ul><ul><ul><li>If x>4 then </li></ul></ul></ul><ul><ul><ul><ul><li>Display “Hurray” </li></ul></ul></ul></ul><ul><ul><ul><li>End if </li></ul></ul></ul><ul><ul><li>END </li></ul></ul><ul><li>Flowchart </li></ul>start end x>4? get x Print “Hurray” T F
    13. 13. Notation - Dual <ul><li>Pseudocode </li></ul><ul><ul><li>START </li></ul></ul><ul><ul><ul><li>Prompt for input </li></ul></ul></ul><ul><ul><ul><li>Get input x </li></ul></ul></ul><ul><ul><ul><li>If x>4 then </li></ul></ul></ul><ul><ul><ul><ul><li>Display “Hurray” </li></ul></ul></ul></ul><ul><ul><ul><li>else </li></ul></ul></ul><ul><ul><ul><ul><li>Display “Sorry” </li></ul></ul></ul></ul><ul><ul><ul><li>End if </li></ul></ul></ul><ul><ul><li>END </li></ul></ul><ul><li>Flowchart </li></ul>start end x>4? Get x Print “Hurray” Print “Sorry” T F
    14. 14. Notation – Multiple (Pseudocode) <ul><ul><li>START </li></ul></ul><ul><ul><ul><li>Prompt for input </li></ul></ul></ul><ul><ul><ul><li>Get input x </li></ul></ul></ul><ul><ul><ul><li>CASE FOR X </li></ul></ul></ul><ul><ul><ul><li>case x= 3 </li></ul></ul></ul><ul><ul><ul><li> print “close” </li></ul></ul></ul><ul><ul><ul><li>else case x= 2 </li></ul></ul></ul><ul><ul><ul><li> print “very close” </li></ul></ul></ul><ul><ul><ul><li>else case x= 1 </li></ul></ul></ul><ul><ul><ul><li> print “bang on” </li></ul></ul></ul><ul><ul><ul><li>else </li></ul></ul></ul><ul><ul><ul><li> print “not even close” </li></ul></ul></ul><ul><ul><ul><li>END CASE </li></ul></ul></ul><ul><ul><li>END </li></ul></ul>
    15. 15. Notation – Multiple (Flowchart) start end X=3 Print “very close” T F eval x X=2 Get x X=1 Print “close” Print “bang on” Print “not even close” T T F F
    16. 16. Case versus Nested IF <ul><ul><li>START </li></ul></ul><ul><ul><ul><li>Prompt for input </li></ul></ul></ul><ul><ul><ul><li>Get input x </li></ul></ul></ul><ul><ul><ul><li>CASE FOR X </li></ul></ul></ul><ul><ul><ul><li>case x= 3 </li></ul></ul></ul><ul><ul><ul><li> print “close” </li></ul></ul></ul><ul><ul><ul><li>case x= 2 </li></ul></ul></ul><ul><ul><ul><li> print “very close” </li></ul></ul></ul><ul><ul><ul><li>case x= 1 </li></ul></ul></ul><ul><ul><ul><li> print “bang on” </li></ul></ul></ul><ul><ul><ul><li>case other </li></ul></ul></ul><ul><ul><ul><li> print “not even close” </li></ul></ul></ul><ul><ul><ul><li>END CASE </li></ul></ul></ul><ul><ul><li>END </li></ul></ul><ul><ul><li>START </li></ul></ul><ul><ul><ul><li>Prompt for input </li></ul></ul></ul><ul><ul><ul><li>Get input x </li></ul></ul></ul><ul><ul><ul><li>if x= 3 </li></ul></ul></ul><ul><ul><ul><li> print “close” </li></ul></ul></ul><ul><ul><ul><li>else </li></ul></ul></ul><ul><ul><ul><li> if x= 2 </li></ul></ul></ul><ul><ul><ul><li> print “very close” </li></ul></ul></ul><ul><ul><ul><li> else </li></ul></ul></ul><ul><ul><ul><li> if x= 1 </li></ul></ul></ul><ul><ul><ul><li> print “bang on” </li></ul></ul></ul><ul><ul><ul><li> else </li></ul></ul></ul><ul><ul><ul><li> print “not even close” </li></ul></ul></ul><ul><ul><ul><li> end if </li></ul></ul></ul><ul><ul><ul><li>end if </li></ul></ul></ul><ul><ul><ul><li>end if </li></ul></ul></ul><ul><ul><li>END </li></ul></ul>
    17. 17. Exercise 8-2 <ul><li>Handout </li></ul><ul><li>Develop Pseudocode and Flowchart </li></ul>
    18. 18. Conditional Code <ul><li>Ruby Demo </li></ul>
    19. 19. Math Review <ul><li>Assignment 5 – any problems? </li></ul><ul><li>Go through answers </li></ul><ul><li>Likely question types </li></ul><ul><li>Schedule for exam night </li></ul>
    20. 20. Quest: Project Work <ul><li>Due Next week at START of class </li></ul><ul><li>Rubric </li></ul><ul><li>1. Application Interface is simple and functional (3) </li></ul><ul><li>2. Application functions all work with appropriate and intuitive prompts (4) </li></ul><ul><li>3. Application presents an attractive output and then exits cleanly without any errors. (3) </li></ul>

    ×