CSCI 15a: Chapter 6 (4 in second ed)

CSCI 15a: Notes Decisions
logic tests


Logical Expressions (produce Boolean results for Decisions)

Easy

Hopefully...

Let i1=10, i2=-20, i3=30.

i1 < 12          true
i1 < 10          true
i3 == i1          false
i1 != 2          true
i1 < 10          false
i2 > 0          false
2*i1 == i2+40          true
i2*(-1) != i1+i1          false

Booleans can be stored as variables.

An expression like a < b < c is illegal.

Again let i1=10, i2=-20, i3=30.

(i1>10) || (i1==10)         false || true          true
(i1>10) || (i1<0)          false || false          false
(i1>0) && (i1<5)          true && false          false
(i1>0) && (i1<20)          true && true          true

The && and || have lower precedence than the relational operators, but parentheses are still useful because they enhance readability.

The && and || are lazy operators that evaluate only the left operand only when that suffices.

Consider:

Does it matter what the second operands are?

Harder

Now you write them.
Consider

Don't forget to try the problems in the book as well and see how well you know how to do these (Chapter 6 (4 in new edition)) has solutions at the bottom of the page. Also truth tables are good to remember.