Logic and Excel

Preview:

Citation preview

Logic and Excel

AND

NOT

OR

IF

Announcements

• We are at the mid point in the semester• Mid semester feedback surveys • Next few weeks will target quantitative

approaches to data analysis, reduction, and decision-making

Logic and Excel

AND

NOT

OR

IF

Poll QuestionI know a lot about logic and its usage in Excel.

A. Strongly agreeB. AgreeC. Don’t agree or disagreeD. DisagreeE. Strongly disagree

Overview

• Logic– IF, AND, NOT, OR– COUNTIF, SUMIF, AVERAGEIF

• Truth Tables

Logic: IF

• Identify conditions and determine actions

• Example: IF it is hot, I will wear beach attire to school. Otherwise, I will wear business casual attire.

IF

Logic: AND

• Identify all conditions being true

• Example: If it is sunny AND hot, I will wear beach attire to school. Otherwise, I will wear business casual attire to school.

AND

Logic: NOT

• Identify conditions not being true

• Example: If it is NOT cold, I will wear beach attire to school. Otherwise, I will wear business casual attire to school.

NOT

Logic: OR

• Identify any condition being true

• Example: If it is sunny OR hot, I will wear beach attire to school. Otherwise, I will wear business casual attire to school.

OR

Excel formulas and functions

• Formulas: equation used [with operators] to calculate a value– Operators: +, -, /, *, ^, >, <, =, >=, <=, <>, &– e.g. =A1+A3

• Functions: preprogrammed shortcuts for calculating equations– e.g. =sum(A1:A3)

Poll QuestionFunctions are preprogrammed shortcuts for calculating equations.

A. TrueB. False

Poll QuestionFormulas are equations used with operators to calculate values.

A. TrueB. False

Practice problems

• How do you use the following with logical statements?– IF– AND– NOT– OR

• What is the difference between functions and formulas?

• What operators are used in Excel?

Excel: IF function

• =IF(condition,value if true,value if false)• =IF(A1>=80,“hot”,“cool”)

Excel logic

• Identify conditions and determine actions

• Example: IF it is hot, I will wear beach attire to school. Otherwise, I will wear business casual attire.

Let’s assume hot is 80 degrees or more

Excel logic

• Identify conditions and determine actions

• Example: IF it is hot, I will wear beach attire to school. Otherwise, I will wear business casual attire.

B4: =IF(B2>=80, “beach attire”, “business casual attire”)

Excel logic

• Identify conditions and determine actions

• Example: IF it is hot, I will wear beach attire to school. Otherwise, I will wear business casual attire.

B4: =IF(B2>=80, “beach attire”, “business casual attire”)

Excel logic

• Identify conditions and determine actions

• Example: IF it is hot, I will wear beach attire to school. Otherwise, I will wear business casual attire.

B4: =IF(B2>=80, “beach attire”, “business casual attire”)

Excel logic

• NOTE: B2>=80 (Hot)

Let’s assume hot is 80 degrees or more

Excel logic

• B2>=80 (Hot)• B2>79 (Hot)• B2<80 (Cold)• B2<=79 (Cold)

Let’s assume hot is 80 degrees or more

Excel logic

• Identify conditions and determine actions

• Example: IF it is hot, I will wear beach attire to school. Otherwise, I will wear business casual attire.

Logic: AND

• Identify all conditions being true

• Example: If it is sunny AND hot, I will wear beach attire to school. Otherwise, I will wear business casual attire to school.

B4: =IF(AND(B3=“Sunny”,B2>=80), “beach attire”, “business casual attire”)

Logic: AND

• Identify all conditions being true

• Example: If it is sunny AND hot, I will wear beach attire to school. Otherwise, I will wear business casual attire to school.

B4: =IF(AND(B3=“Sunny”,B2>=80), “beach attire”, “business casual attire”)

Logic: NOT

• Identify conditions not being true

• Example: If it is NOT cold, I will wear beach attire to school. Otherwise, I will wear business casual attire to school.

B4: =IF(NOT(B2<80), “beach attire”, “business casual attire”)

Logic: NOT

• Identify conditions not being true

• Example: If it is NOT cold, I will wear beach attire to school. Otherwise, I will wear business casual attire to school.

B4: =IF(NOT(B2<80), “beach attire”, “business casual attire”)

Logic: OR

• Identify any condition being true

• Example: If it is sunny OR hot, I will wear beach attire to school. Otherwise, I will wear business casual attire to school.

B4: =IF(OR(B3=“Sunny”,B2>=80), “beach attire”, “business casual attire”)

Logic: OR

• Identify any condition being true

• Example: If it is sunny OR hot, I will wear beach attire to school. Otherwise, I will wear business casual attire to school.

B4: =IF(OR(B3=“Sunny”,B2>=80), “beach attire”, “business casual attire”)

Poll QuestionIf I eat more than 2000 calories, I will work out more. If not, no additional action is needed.

What function(s) can you enter into B4? A. =IF(B2>2000, “Work out more”, “no action needed”)B. =IF(B2>=2000, “Work out more”, “no action needed”)C. =IF(B2<2000, “no action needed”, “Work out more”)D. =IF(B2<=1999, “no action needed”, “Work out more”)E. Not enough information

Poll QuestionIf I eat more calories than I burn, I will work out more. If not, no action is needed.

What function(s) can you enter into B4? A. =IF(B3>=B2, “no action needed”, “Work out more”)B. =IF(B2>B3, “Work out more”, “no action needed”)C. =IF(B3>B2, “no action needed”, “Work out more”)D. A and BE. B and CF. A and C

What constitutes no action/work out more

• If I eat more calories than I burn, I will work out more. If not, no action is needed. – Calories eaten > calories burned [WO]– Calories eaten <= calories burned [NA]

WO=work outNA=no action

What constitutes no action/work out more

• If I eat more calories than I burn, I will work out more. If not, no action is needed. – calories eaten > calories burned [WO]– calories burned < calories eaten [WO]– calories eaten <= calories burned [NA]– calories burned >= calories eaten [NA]

WO=work outNA=no action

v

• If I eat more calories than I burn, I will work out more. If not, no action is needed.

A. B3>=B2 : calories burned >= calories eaten [NA]B. B2>B3 : calories eaten >calories burned [WO]C. B3>B2 : calories burned > calories eaten [NA]

v

• If I eat more calories than I burn, I will work out more. If not, no action is needed.

A. B3>=B2 : calories burned >= calories eaten [NA]B. B2>B3 : calories eaten >calories burned [WO]C. B3>B2 : calories burned > calories eaten [NA]

calories eaten > calories burned [WO]calories burned < calories eaten [WO]calories eaten <= calories burned [NA]calories burned >= calories eaten [NA]

• If I eat more calories than I burn, I will work out more. If not, no action is needed.

A. B3>=B2 : calories burned >= calories eaten [NA]B. B2>B3 : calories eaten >calories burned [WO]C. B3>B2 : calories burned > calories eaten [NA]

calories eaten > calories burned [WO]calories burned < calories eaten [WO]calories eaten <= calories burned [NA]calories burned >= calories eaten [NA]

Poll QuestionIf I eat less than 2000 calories and burn more than 2000 calories, I will eat more. If not, no additional action is needed.

What function should you enter into B4? A. =IF(AND(B2>2000, B3<2000), “eat more”, “no additional action needed”)B. =IF(AND(B2<2000, B3>2000), “eat more”, “no additional action needed”)C. =IF(AND(B2<=2000, B3>=2000), “eat more”, “no additional action

needed”)D. =IF(NOT(B2<2000, B3>2000), “eat more”, “no additional action needed”)E. Cannot determine from information given

Embedded IF

Embedded IF

• Grades– A: 900 or higher– B: 800 to 899– C: 700 to 799– D: 600 to 699– F: 0-599

Embedded IF

=IF(H2>=900,"A",IF(H2>=800,"B",IF(H2>=700,"C",IF(H2>=600,"D","F"))))

Embedded IF

Embedded IF

Or, we can use lookup functions to calculate grades.

Other IF functions

• SUMIF: sum a range if criteria is met• COUNTIF: count a range if criteria is met• AVERAGEIF: average of a range if criteria is

met

AVERAGEIF

AVERAGEIF

Cell C13: =AVERAGEIF(B2:B11,"Teacher A",I2:I11)

AVERAGEIF

Cell C13: =AVERAGEIF(B2:B11,"Teacher A",I2:I11)

What is the function in cell C14?

Poll QuestionWhat is the function used in cell C14 to calculate average total score for teacher B?

A. =AVERAGEIF(I2:I11,"Teacher B", B2:B11) B. =IFAVERAGE(AVERAGEIF(I2:I11,"Teacher B", B2:B11) C. =AVERAGEIF(A2:A11,"Teacher B",I2:I11) D. =IFAVERAGE(B2:B11,"Teacher B",I2:I11)E. =AVERAGEIF(B2:B11,"Teacher B",I2:I11)

COUNTIF

COUNTIF

C16: =COUNTIF(J2:J11,"A")

Poll QuestionIf I used the COUNTIF function to count grades in ICS 101 (A+ to F), how many possible grades exist?

A. 1B. 4C. 5D. 13E. 15

Practice problems• How do the IF, AND, NOT, and OR functions work in Excel?

– What could you use these functions for in your checklist?– Hint: Do any of these functions help you to make a decision?– Example: Return rates and reverse logistics

• Use an embedded IF statement to calculate grades for ICS 101. Check the syllabus for the grade breakdown.– Hint: The embedded IF statement will be longer than the

example in this lecture due to the +/- scale.• What is the arguments list for SUMIF if it uses the same

arguments list as the COUNTIF and AVERAGEIF functions?• How do the COUNTIF, and AVERAGEIF functions work?

– What could you use these functions for in your checklist?– Hint: When do you want to search and conduct one of these

analyses?

XOR: Exclusive OR(Cats OR Dogs) AND NOT (Cats AND Dogs)

Logic Symbols

Expression Meaning¬ x not x

x ∧ y x and yx ∨ y x or y

Compound Logic

x ¬ x

FALSE TRUE

TRUE FALSE

x y x ∧ y

FALSE FALSE FALSE

FALSE TRUE FALSE

TRUE FALSE FALSE

TRUE TRUE TRUE

x y x ∨ y

FALSE FALSE FALSE

FALSE TRUE TRUE

TRUE FALSE TRUE

TRUE TRUE TRUE

Truth TablesA quick way to understand the nature of a function, or to see if

two functions are equivalent (have the same truth values). Show every possible combination of the variables. Here is a truth table for XOR (the exclusive OR)*:

(x ∨ y) ∧ ¬ (x ∧ y)

Truth TablesA quick way to understand the nature of a function, or to see if

two functions are equivalent (have the same truth values). Show every possible combination of the variables. Here is a truth table for XOR (the exclusive OR)*:

x y (x ∨ y) (x ∧ y) ¬ (x ∧ y) (x ∨ y) ∧ ¬ (x ∧ y)

FALSE FALSE

FALSE TRUE

TRUE FALSE

TRUE TRUE

(x ∨ y) ∧ ¬ (x ∧ y)

Truth TablesA quick way to understand the nature of a function, or to see if

two functions are equivalent (have the same truth values). Show every possible combination of the variables. Here is a truth table for XOR (the exclusive OR)*:

x y (x ∨ y) (x ∧ y) ¬ (x ∧ y) (x ∨ y) ∧ ¬ (x ∧ y)

FALSE FALSE FALSE

FALSE TRUE TRUE

TRUE FALSE TRUE

TRUE TRUE TRUE

(x ∨ y) ∧ ¬ (x ∧ y)

Truth TablesA quick way to understand the nature of a function, or to see if

two functions are equivalent (have the same truth values). Show every possible combination of the variables. Here is a truth table for XOR (the exclusive OR)*:

x y (x ∨ y) (x ∧ y) ¬ (x ∧ y) (x ∨ y) ∧ ¬ (x ∧ y)

FALSE FALSE FALSE FALSE

FALSE TRUE TRUE FALSE

TRUE FALSE TRUE FALSE

TRUE TRUE TRUE TRUE

(x ∨ y) ∧ ¬ (x ∧ y)

Truth TablesA quick way to understand the nature of a function, or to see if

two functions are equivalent (have the same truth values). Show every possible combination of the variables. Here is a truth table for XOR (the exclusive OR)*:

x y (x ∨ y) (x ∧ y) ¬ (x ∧ y) (x ∨ y) ∧ ¬ (x ∧ y)

FALSE FALSE FALSE FALSE TRUE

FALSE TRUE TRUE FALSE TRUE

TRUE FALSE TRUE FALSE TRUE

TRUE TRUE TRUE TRUE FALSE

(x ∨ y) ∧ ¬ (x ∧ y)

Truth TablesA quick way to understand the nature of a function, or to see if

two functions are equivalent (have the same truth values). Show every possible combination of the variables. Here is a truth table for XOR (the exclusive OR)*:

x y (x ∨ y) (x ∧ y) ¬ (x ∧ y) (x ∨ y) ∧ ¬ (x ∧ y)

FALSE FALSE FALSE FALSE TRUE FALSE

FALSE TRUE TRUE FALSE TRUE TRUE

TRUE FALSE TRUE FALSE TRUE TRUE

TRUE TRUE TRUE TRUE FALSE FALSE

(x ∨ y) ∧ ¬ (x ∧ y)

Live Practice

On a sheet of paper or on your computing device, draw a truth table for:

(x ∨ y) ∧ (¬ x ∨ ¬ y)

In-class practice

Draw a truth table for:

(x ∨ y) ∧ (¬ x ∨ ¬ y)

x y (x ∨ y) ¬ x ¬ y (¬x ∨ ¬y) (x ∨ y) ∧ (¬ x ∨ ¬ y)

FALSE FALSE

FALSE TRUE

TRUE FALSE

TRUE TRUE

Truth Tables

(x ∨ y) ∧ (¬ x ∨ ¬ y)

x y (x ∨ y) ¬ x ¬ y (¬x ∨ ¬y) (x ∨ y) ∧ (¬ x ∨ ¬ y)

FALSE FALSE FALSE TRUE TRUE TRUE FALSE

FALSE TRUE TRUE TRUE FALSE TRUE TRUE

TRUE FALSE TRUE FALSE TRUE TRUE TRUE

TRUE TRUE TRUE FALSE FALSE FALSE FALSE

x y (x ∨ y) (x ∧ y) ¬ (x ∧ y) (x ∨ y) ∧ ¬ (x ∧ y)

FALSE FALSE FALSE FALSE TRUE FALSE

FALSE TRUE TRUE FALSE TRUE TRUE

TRUE FALSE TRUE FALSE TRUE TRUE

TRUE TRUE TRUE TRUE FALSE FALSE

(x ∨ y) ∧ (¬ x ∨ ¬ y)

(x ∨ y) ∧ ¬ (x ∧ y)

x y (x ∨ y) ¬ x ¬ y (¬x ∨ ¬y) (x ∨ y) ∧ (¬ x ∨ ¬ y)

FALSE FALSE FALSE TRUE TRUE TRUE FALSE

FALSE TRUE TRUE TRUE FALSE TRUE TRUE

TRUE FALSE TRUE FALSE TRUE TRUE TRUE

TRUE TRUE TRUE FALSE FALSE FALSE FALSE

XOR: Exclusive OR(Cats OR Dogs) AND NOT (Cats AND Dogs) = (x ∨ y) ∧ ¬ (x ∧ y)

(Cats OR Dogs) AND (NOT Cats OR NOT Dogs) = (x ∨ y) ∧ (¬ x ∨ ¬ y)

Cats OR Dogs ≠ (Cats OR Dogs) AND NOT (Cats AND Dogs)

Hamilton Library Ground Floor, Courtesy of Sean Mosier

Poll QuestionI find this sign to be ominous.

A. Strongly agreeB. AgreeC. Don’t agree or disagreeD. DisagreeE. Strongly disagree

(Read OR Die) AND NOT (Read AND Die)

Read OR Die

Practice problems

• Given a logical statement can you create a truth table?

• Can you think of other logical statements that could be the equivalent of the exclusive or (XOR)?– Test it by creating a truth table

On-line lecture

• Review logic and truth tables• Apply logic and truth tables to Excel• Practice using Excel.

Reminders

• Reminders: – Plagiarism tutorial 2 will open soon

It is the middle of the semester. Would you like a midterm surprise?

We normally have the midterm exam around this time of the semester.

Since we have an extra week of lecture, my teaching team and I decided to makethis week’s lecture quiz EXTRA CREDIT. The content is still required for the course, but the points you earn will be EC.

Poll QuestionI learned a lot about logic and its usage in Excel.

A. Strongly agreeB. AgreeC. Don’t agree or disagreeD. DisagreeE. Strongly disagree

Recommended