230 likes | 421 Views
Logical Thinking. CS 104 9/12/11. Agenda. Today College Board survey reminder Note: Simple “how to” guide on Scratch posted on eLearning Review HW #2 Review of control constructs Examples in BYOB Scratch Wednesday Quiz #2 on Wednesday Review Research Paper #1 More BYOB Scratch ideas.
E N D
Logical Thinking CS 104 9/12/11
Agenda • Today • College Board survey reminder • Note: Simple “how to” guide on Scratch posted on eLearning • Review HW #2 • Review of control constructs • Examples in BYOB Scratch • Wednesday • Quiz #2 on Wednesday • Review Research Paper #1 • More BYOB Scratch ideas
Review: Decision Structure • “if” statements are used to test whether a certain condition is met • This requires comparison operators and a way to connect multiple comparisons to create complex statements
Comparison Operators • Relational Operators: Evaluate to true or false • Scratch does not provide <>, >= or <= • These can be simulated as • x > 10 or x = 10, and not x = 10
Operator Precedence • What is 2 + 3 * 4? • What about (2 + 3) * 4? • The concept of operator precedence dictates which operators are applied first and have higher importance • In most programming languages, the order is: • terms inside parentheses or brackets • exponents and roots • multiplication and division As they appear left to right • addition and subtraction As they appear left to right
Comparison Operators:You Answer TRUE 5 < 7 FALSE 3>9 FALSE 5 <= 4 TRUE 5 > 4
Comparison Operators:You Answer (5 + 2) < 7 FALSE FALSE 3> (9 / 3) (5 * 4) >= (4 * 5) TRUE FALSE (5 / 2) > 4
Boolean Operators • Boolean logic – basis of computer logic, used to create complex comparisons • Invented by George Boole, an English mathematician and philosopher ... no general method for the solution of questions in the theory of probabilities can be established which does not explicitly recognise ... those universal laws of thought which are the basis of all reasoning ... http://en.wikipedia.org/wiki/George_Boole
Boolean Operators • AND • Both conditions must be true in order for the entire condition to be true • OR • At least one condition must be true in order for the entire condition to be true • NOT • Opposite; if the condition is true, the result is false, and if the condition is false, the result is true • Operator Precedence • Not • And • Or
Boolean Operators: ANDYou Answer 5 < 7 AND 3 < (2 * 2) 5 < 7 true 3 < 4 true TRUE and true = true
Boolean Operators: ORYou Answer 8 < 7 or 4 >= 4 8 < 7 false 4 >= 4 true false or true = true
Boolean Operators: ComplexYou Answer (8 < 7 or 4 >= 4) AND (Not (1 > 9)) 8 < 7 false true 4 >= 4 false or true = true
Boolean Operators: ComplexYou Answer True AND (Not (1 > 9)) 1 > 9 false Not false true True AND true true
Truth Tables • Implication, we denote as => • "If it rains, I will stay at home"There are 4 possibilities:1) it rains and he stays at home This is compatible with the statement: he did not lie.2) it rains and he does not stay at home This is NOT compatible with the statement: he lied.3) it does not rain and he stays at home This is compatible with the statement: he did not lie.4) it does not rain and he does not stay at home This is compatible with the statement: he did not lie.
Grader A B • Write a program to assign a letter grade to a given numeric grade. • If the given grade is between 90 and 100, print an A. • If the given grade is between 80 and 89, print a B. • If the given grade is between 70 and 79, print a C. • If the given grade is less than 70, print an F. F C
Review: Loops • Loops are used to perform a single task repetitively until a specified condition is met
Types of Loops • Infinite loops • The command(s) placed inside a never ending loop will repeat until the program is stopped • For loops • The loop will execute for a specified number of times
Types of Loops • While loop • This loop will repeat while the specified condition is true • Do Until loop • This loop will repeat until the specified condition is true
Class Grader A B • Write a program to average three grades per student. The grades will be submitted by the user, and the number of students in the class should be a random number. • If the average grade is between 90 and 100, print an A. • If the average grade is between 80 and 89, print a B. • If the average grade is between 70 and 79, print a C. • If the average grade is less than 70, print an F. F C
Next Time • Quiz #2 9/14/11 • HW #2 due 9/21/11 • Feel free to email both Dr. Gray and Amber with any questions