21
Logical Thinking CS 104 9/12/11

Logical Thinking

  • Upload
    ogden

  • View
    57

  • Download
    0

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Logical Thinking

Logical Thinking

CS 104

9/12/11

Page 2: Logical Thinking

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

Page 3: Logical Thinking

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

Page 4: Logical Thinking

Comparison Operators Relational Operators: Evaluate to true or

falseSymbol Meaning< Less than

<= Less than or equal to= Equal to> Greater than

>= Greater than or equal to<> Not equal to

• Scratch does not provide <>, >= or <=– These can be simulated as

• x > 10 or x = 10, and not x = 10

Page 5: Logical Thinking

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:1. terms inside parentheses or brackets

2. exponents and roots

3. multiplication and division As they appear left to right

4. addition and subtraction As they appear left to right

Page 6: Logical Thinking

Comparison Operators:You Answer

5 < 7 TRUE3 > 9 FALSE

5 <= 4FALSE

5 > 4 TRUE

Page 7: Logical Thinking

Comparison Operators:You Answer

(5 + 2) < 7 FALSE3 > (9 / 3) FALSE

(5 * 4) >= (4 * 5)TRUE

(5 / 2) > 4 FALSE

Page 8: Logical Thinking

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

Page 9: Logical Thinking

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

Page 10: Logical Thinking

Boolean Operators: ANDYou Answer

5 < 7 AND 3 < (2 * 2)5 < 7TRUE

3 < 4TRUE

TRUE AND TRUE = TRUE

Page 11: Logical Thinking

Boolean Operators: ORYou Answer

8 < 7 OR 4 >= 48 < 7 FALSE

4 >= 4 TRUE

FALSE OR TRUE = TRUE

Page 12: Logical Thinking

Boolean Operators: ComplexYou Answer

(8 < 7 OR 4 >= 4) AND (NOT (1 > 9))

8 < 7FALSE

4 >= 4 TRUE

FALSE OR TRUE = TRUE

Page 13: Logical Thinking

Boolean Operators: ComplexYou Answer

TRUE AND (NOT (1 > 9))

1 > 9 FALSE

NOT FALSETRUE

TRUE AND TRUETRUE

Page 14: Logical Thinking

Truth Tables

P Q P or Q

T T

T F

F T

F F

P Q P and Q

T T

T F

F T

F F

P Not P

T

F

Page 15: Logical Thinking

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.

Truth TablesP Q P => Q

T T

T F

F T

F F

Page 16: Logical Thinking

Grader

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.

A B

FC

Page 17: Logical Thinking

Review: Loops

Loops are used to perform a single task repetitively until a specified condition is met

Page 18: Logical Thinking

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

Page 19: Logical Thinking

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

Page 20: Logical Thinking

Class Grader

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.

A B

FC

Page 21: Logical Thinking

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