csc 107 – programming for science. follow-up from last lecture

17
LECTURE 8: BOOLEAN LOGIC CSC 107 – Programming For Science

Upload: ross-spencer

Post on 28-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CSC 107 – Programming For Science. Follow-Up From Last Lecture

LECTURE 8:BOOLEAN LOGIC

CSC 107 – Programming For Science

Page 2: CSC 107 – Programming For Science. Follow-Up From Last Lecture

Follow-Up From Last Lecture

Page 3: CSC 107 – Programming For Science. Follow-Up From Last Lecture

Announcements

Lectures may not cover all material from book Lectures will cover most difficult or

challenging topics All material is important & you are

responsible for it For class to work, you must read & ask

questions

Page 4: CSC 107 – Programming For Science. Follow-Up From Last Lecture

George Boole

Mathematician from English middle-class Lived from 1815 – 1864 Started work at age 16 as a teaching

assistant Held two assistantships to support family Opened own school after many years of

work In 1847 wrote Mathematical Analysis of

Logic

Page 5: CSC 107 – Programming For Science. Follow-Up From Last Lecture

Mathematical Analysis of Logic Boole’s book proposed new logical

system World began with 2 values– though more

created Devised rules to add, subtract, & multiply

Work ignored during Boole’s lifetime System only had 2 values, so what was the

point? What is done with developer of pointless

knowledge? Basis for most technology in the

modern age All it took was a simple little discovery…

Page 6: CSC 107 – Programming For Science. Follow-Up From Last Lecture

Gate

Combines input(s) to generate output signal Like most electronics, uses “on-off” state Input is "off", if line drops below 3.3 volts From 3.3 - 5 volts, an input is considered on Gate is deep fried silicon if line goes above

5 volts, Like Boole’s logic, electronics have 2

values Simple gates combine to make modern

circuitry All initially part of Boolean algebra Basis of programming at the lowest, rawest

level

Page 7: CSC 107 – Programming For Science. Follow-Up From Last Lecture

Truth Table

Normal way that Boolean functions presented

All combinations of inputs shown in this table This is really easy, inputs must be true or

false Output shown for each of the possible

inputs Given how it sounds, not at all

complicated Very simple rules to follow to construct Does requires you count up to 2

Page 8: CSC 107 – Programming For Science. Follow-Up From Last Lecture

NOT Gate

Simplest gate: computes opposite of input Output false when input true; Output true when input false;

Written in C++ as !a a is gate’s inputa !a

true

false

a !a

Page 9: CSC 107 – Programming For Science. Follow-Up From Last Lecture

OR Gate

Equivalent to addition in Boolean algebra If either input is true is going to be checked true when either a OR b are true; false

otherwise Written in C++ as a || b

a & b are both inputs to gatea b a || b

false falsefalse truetrue falsetrue true

a

ba || b

Page 10: CSC 107 – Programming For Science. Follow-Up From Last Lecture

AND Gate

Equivalent to multiplication in Boolean algebra If both inputs are true is going to be

checked True when a AND b are true; false otherwise

Written in C++ as a && b a & b are both inputs to gate

a

ba && b

a b a && b

false falsefalse truetrue falsetrue true

Page 11: CSC 107 – Programming For Science. Follow-Up From Last Lecture

Boolean Values

3 boolean functions can combine into computer Billions of gates combined by engineers to

make CPU Could use, for example: (a && b) || (a && !c)

But how to generate inputs for these functions First need boolean values of true and false Easy in hardware (true is any signal > 3.3V) But how could we do get values within our

program?

Page 12: CSC 107 – Programming For Science. Follow-Up From Last Lecture

Relational Operators

< (less than) > (greater than) <= (less than of equal to) >= (greater than of equal to) != (inequality ≠) == (equality – if two things have same

value)

Page 13: CSC 107 – Programming For Science. Follow-Up From Last Lecture

Relational Operators

< (less than) > (greater than) <= (less than of equal to) >= (greater than of equal to) != (inequality ≠) == (equality – if two things have same

value) Equality (==) and assignment (=) are NOT

the same

Page 14: CSC 107 – Programming For Science. Follow-Up From Last Lecture

Assignment vs. Comparison

Assign values using "=" (single equals sign) Sets left-side variable equal to value on

right Compare values using "==" (double

equal sign) However it is used, computes boolean value

Very easy to get them confused Frequently (& easy) bug found everywhere Assignment treated as boolean expression,

also Code will compile, but will not like

unexpected results

Page 15: CSC 107 – Programming For Science. Follow-Up From Last Lecture

Relational Operators

Relational operators compute bool Like any expression, can be used in any

statements

int nfl = 32;bool team = 0 > 6;bool group = 45 <= nfl;bool gang = nfl == 32;team = sqrt(144) == 12.0;group = (133 == pow(12, 2));gang = (group == team);

Page 16: CSC 107 – Programming For Science. Follow-Up From Last Lecture

Your Turn

Get in groups & work on following activity

Page 17: CSC 107 – Programming For Science. Follow-Up From Last Lecture

For Next Lecture

Read sections 2.1 – 2.9 for Monday Why do programmers never wash their

hair? How do we discuss & display program

ideas? Why do coders have flow? How can we

chart this?

Week #3 weekly assignment due Tuesday Problems available on Angel If problem takes more than 10 minutes,

TALK TO ME!