logical thinking

Post on 22-Jan-2016

53 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

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

TRANSCRIPT

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

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

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

Comparison Operators:You Answer

5 < 7 TRUE3 > 9 FALSE

5 <= 4FALSE

5 > 4 TRUE

Comparison Operators:You Answer

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

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

(5 / 2) > 4 FALSE

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 < 7TRUE

3 < 4TRUE

TRUE AND TRUE = TRUE

Boolean Operators: ORYou Answer

8 < 7 OR 4 >= 48 < 7 FALSE

4 >= 4 TRUE

FALSE OR TRUE = TRUE

Boolean Operators: ComplexYou Answer

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

8 < 7FALSE

4 >= 4 TRUE

FALSE OR TRUE = TRUE

Boolean Operators: ComplexYou Answer

TRUE AND (NOT (1 > 9))

1 > 9 FALSE

NOT FALSETRUE

TRUE AND TRUETRUE

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

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

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

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

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

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

top related