Class 8 Lecture Notes
-
Upload
stephen-parsons -
Category
Documents
-
view
3.614 -
download
2
description
Transcript of Class 8 Lecture Notes
![Page 1: Class 8 Lecture Notes](https://reader033.fdocuments.net/reader033/viewer/2022061120/546cad23b4af9f892c8b5152/html5/thumbnails/1.jpg)
Class 8 – Conditional Logic
![Page 2: Class 8 Lecture Notes](https://reader033.fdocuments.net/reader033/viewer/2022061120/546cad23b4af9f892c8b5152/html5/thumbnails/2.jpg)
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
![Page 3: Class 8 Lecture Notes](https://reader033.fdocuments.net/reader033/viewer/2022061120/546cad23b4af9f892c8b5152/html5/thumbnails/3.jpg)
Pseudocode Review
Pseudo-code is structured English that states the steps to the problem solution
1. Statements are written in simple English
2. Each instruction/step is written on a separate line
3. Keywords and indentation are used to signify particular control structures
4. Each set of instructions is written from top to bottom, with only one entry and one exit
![Page 4: Class 8 Lecture Notes](https://reader033.fdocuments.net/reader033/viewer/2022061120/546cad23b4af9f892c8b5152/html5/thumbnails/4.jpg)
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
![Page 5: Class 8 Lecture Notes](https://reader033.fdocuments.net/reader033/viewer/2022061120/546cad23b4af9f892c8b5152/html5/thumbnails/5.jpg)
Flow Chart Notation Review
Terminal symbol- indicates the starting or stopping point of logic:
Input/Output symbol- reading orwriting input/output:
Process Symbol- any single process, such as assigning a value or performinga calculation:
Decision Symbol- comparisons/ T or F decision:
Flowlines- connect symbols in the flowchart: arrow head suggests flow of data a straight line may be used to indicate a relationship
![Page 6: Class 8 Lecture Notes](https://reader033.fdocuments.net/reader033/viewer/2022061120/546cad23b4af9f892c8b5152/html5/thumbnails/6.jpg)
Sample FlowChart
Pseudocode# Start Program
#Prompt for assignment
#Set a counter#Loop through grades
# Get student grade# Increment counter
#End loop#Calculate Average#Print Average
# End Program
start
end
Assignment name?
Set counter = 1
Counter>10?
Calculate Average
Print Average
Increment Counter
Student Grade? Ye
sNo
![Page 7: Class 8 Lecture Notes](https://reader033.fdocuments.net/reader033/viewer/2022061120/546cad23b4af9f892c8b5152/html5/thumbnails/7.jpg)
Conditions
Example: Input: 2 integers Choose result type: sum, difference,
product, quotient, modulus, or exponent Processing:?
Conditional Operators Relational Logical
![Page 8: Class 8 Lecture Notes](https://reader033.fdocuments.net/reader033/viewer/2022061120/546cad23b4af9f892c8b5152/html5/thumbnails/8.jpg)
Relational Operators
How does one item relate or compare to another? a>b a<b a=b a<>b a>=b a<=b
Considera=12b=43What is the effect on each of the conditions?
![Page 9: Class 8 Lecture Notes](https://reader033.fdocuments.net/reader033/viewer/2022061120/546cad23b4af9f892c8b5152/html5/thumbnails/9.jpg)
Relational Operators
How does one item relate or compare to another? a>b a<b a=b a<>b a>=b a<=b
Considera=12b=43What is the effect on each of the conditions?
false true false true false true
![Page 10: Class 8 Lecture Notes](https://reader033.fdocuments.net/reader033/viewer/2022061120/546cad23b4af9f892c8b5152/html5/thumbnails/10.jpg)
Logical Operators
For complex conditions AND OR NOT A AND B A OR B NOT A A OR NOT B
Considera=Today is Tuesdayb=It is RainingWhat is the effect on each of the conditions?
![Page 11: Class 8 Lecture Notes](https://reader033.fdocuments.net/reader033/viewer/2022061120/546cad23b4af9f892c8b5152/html5/thumbnails/11.jpg)
Exercise 8-1
Handout Evaluate Conditional Operators
Excel solutions
![Page 12: Class 8 Lecture Notes](https://reader033.fdocuments.net/reader033/viewer/2022061120/546cad23b4af9f892c8b5152/html5/thumbnails/12.jpg)
Branching (Flow Control)
Single Condition if - then
Dual Condition if – else – end if
Multiple Condition select/ case
![Page 13: Class 8 Lecture Notes](https://reader033.fdocuments.net/reader033/viewer/2022061120/546cad23b4af9f892c8b5152/html5/thumbnails/13.jpg)
Notation - Single
PseudocodeSTART
Prompt for inputGet input xIf x>4 then
Display “Hurray”
End if
END
Flowchart
start
end
x>4?
get x
Print “Hurray”
T
F
![Page 14: Class 8 Lecture Notes](https://reader033.fdocuments.net/reader033/viewer/2022061120/546cad23b4af9f892c8b5152/html5/thumbnails/14.jpg)
Notation - Dual
PseudocodeSTART
Prompt for inputGet input xIf x>4 then
Display “Hurray”
elseDisplay “Sorry”
End if
END
Flowchart
start
end
x>4?
Get x
Print “Hurray”
Print “Sorry”
T F
![Page 15: Class 8 Lecture Notes](https://reader033.fdocuments.net/reader033/viewer/2022061120/546cad23b4af9f892c8b5152/html5/thumbnails/15.jpg)
Notation – Multiple (Pseudocode)# START
#Prompt for input#Get input x#Evaluate (Case for x)
# where x= 3#print “close”
# where x= 2#print “very close”
# where x= 1#print “bang on”
# where x is anything else#print “not even
close”
#End case# END
![Page 16: Class 8 Lecture Notes](https://reader033.fdocuments.net/reader033/viewer/2022061120/546cad23b4af9f892c8b5152/html5/thumbnails/16.jpg)
Notation – Multiple (Flowchart)
start
end
X=3
Print “very close”
T
Feval x X=2
Get x
X=1
Print “close”
Print “bang on”
Print “not even
close”
T T
F F
![Page 17: Class 8 Lecture Notes](https://reader033.fdocuments.net/reader033/viewer/2022061120/546cad23b4af9f892c8b5152/html5/thumbnails/17.jpg)
Case versus Nested IF
STARTPrompt for inputGet input xCASE 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 CASEEND
STARTPrompt for inputGet input x
if x= 3 print “close”else if x= 2 print “very close” else if x= 1 print “bang on” else print “not even close” end if
end if end if
END
![Page 18: Class 8 Lecture Notes](https://reader033.fdocuments.net/reader033/viewer/2022061120/546cad23b4af9f892c8b5152/html5/thumbnails/18.jpg)
Exercise 8-2
Handout Develop Pseudocode and Flowchart
![Page 19: Class 8 Lecture Notes](https://reader033.fdocuments.net/reader033/viewer/2022061120/546cad23b4af9f892c8b5152/html5/thumbnails/19.jpg)
Conditional Code in Ruby
Conditionals Relational
a>b, a<b, a<=b, a>=b, a==b, a!=b Logical
&& (AND), || (OR), ! (NOT) Requires parenthesis E.g. if (A>6 && C!=“Math”)…
Ruby Demo
![Page 20: Class 8 Lecture Notes](https://reader033.fdocuments.net/reader033/viewer/2022061120/546cad23b4af9f892c8b5152/html5/thumbnails/20.jpg)
Quest: Project Work
Rubric 1. Application Interface is simple and
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)
![Page 21: Class 8 Lecture Notes](https://reader033.fdocuments.net/reader033/viewer/2022061120/546cad23b4af9f892c8b5152/html5/thumbnails/21.jpg)
Project Work
Total value=20% Due by start of class next
week Prefer you zip the file and
email to me Otherwise I’ll copy it to my flash
disk at start of class
![Page 22: Class 8 Lecture Notes](https://reader033.fdocuments.net/reader033/viewer/2022061120/546cad23b4af9f892c8b5152/html5/thumbnails/22.jpg)
Summary
Review Notation Pseudocode and flowcharts Conditional logic
Relational operators Logical operators
Branching/ Flow Control Ruby language Project evaluation
![Page 23: Class 8 Lecture Notes](https://reader033.fdocuments.net/reader033/viewer/2022061120/546cad23b4af9f892c8b5152/html5/thumbnails/23.jpg)
Questions?
Enjoy the holiday Lest we forget…