Upload
juan
View
232
Download
0
Embed Size (px)
Citation preview
7/24/2019 4.1 Boolean Operations
1/24
PIC 10ALecture 10 Boolean Operations
7/24/2019 4.1 Boolean Operations
2/24
if(n==5)
We saw last time how to runcode depending on the stateof an internal variale! "ore#ample$
We also saw that for doulevariales it is etter to test forine%ualit&
if(n==5)cout''n is
5!n*+else
cout''n is no5!n*+
doules%rt, = s%rtdouletwo = ,!+douletol = 10e-1.if( as(s%rt,/s%rt,-ttol )
cout''loseenoughn+else
cout''2ot cloenoughn+
7/24/2019 4.1 Boolean Operations
3/24
0 3 # 3 1
4ow would &ou test a compound ine%ualit& 6!e!$ how tois etween 0 and 1
7nli8e mathematical statements$ 99 re%uires ver& specforms of compound conditional statements!
6n other words$ we have to separate each conditional sta
;his is the 99 wa& of testing :rst for 0'=#$ and then tefor # '= 1! 6f oth are true the code inside the rac8ets e#ecutes!
if(0 '= # '= 1) ??@AAOA
if(0 '= # CC # '=1) < !!! >
7/24/2019 4.1 Boolean Operations
4/24
# D 5 OA # 3 -E
Fometimes we wish to have a statement that wrun in eitherof two cases! When either case issuGcient$ we use HH$ for e#ample$
;his code will run in all cases e#cept when # isetween -E and 5 (e#clusive)!
"or e#ample$ suppose we have a pol&nomial p(#-5)/(#9E)$ which is negative for # etween -E5! ;o evaluate s%rt(p(#)) we would want to e
we are not in the range of negative values!
if( # I= 5 HH # '= -E )< >
7/24/2019 4.1 Boolean Operations
5/24
2on-e#clusive OA
6n order for a statement li8e
to e true$ we need BO;4 conditions to e true!
6n the case of or HH$
either one or oth can e true for the statement inside rac8ets to run!
;his is called non-e#clusive OA!
if( name == John*CC age '=E5 ) < >
if( name == John*HH age '=E5) < >
7/24/2019 4.1 Boolean Operations
6/24
Opposite
We can also negatea statement! Fo for e#ample$ supwe wanted to test for # 2O; negative!
;he test for # eing negative is
;o negate a statement$ use (statement)! Fo to test fo
2O; negative$ &ou can use
Of course$ # 2O; negative is the same as # D 0$ so it ie%uivalent to
if (# ' 0) < >
if ( (# ' 0))< >
if (# I= 0) < >
7/24/2019 4.1 Boolean Operations
7/24
CC$ HH$
7/24/2019 4.1 Boolean Operations
8/24
rememer ool
Aecall there is a non-numerical data t&pe called ool!
6n general$ a oolean is an& data t&pe designed for truthin8ing!
K statement li8e #'=5 is going to e replaced with a variale with value depending on whether or not # is l
than or e%ual to 5!doule# = E!.5+ ?? 6nitialie #ooltest = (#'=5)+ ?? test will etrueif(test) < > ?? ;his set of code wille run
7/24/2019 4.1 Boolean Operations
9/24
K oolMs value
;here are several diNerent wa&s to set the valueoolvariale!
;he 8e&words trueand falsemust e in KLL lowe
ooltest+
test = false+ cout''false is''test''endl+ ?? 0
test = true+ cout''true is''test''endl+ ?? 1
test = 0+ cout''0 is ''test''endl+?? 0test = 1+ cout''1 is ''test''endl+?? 1
7/24/2019 4.1 Boolean Operations
10/24
K oolMs value
6 would suggest thin8ing in terms of 0 and not 0! "or e#a
test will onl& ever print out as a 0 or 1$ ut as the last twe#amples show &ou can t&pe cast an& integer t&pe or opoint into a ool$ and it will e a 1 unless the value isrepresented as e#actl& 0!
ooltest+
test = 100+ cout''100 is''test''endl+ ?? 1
test = -50+ cout''-50 is
''test''endl+ ?? 1
test = 0!1,E+ cout''0!1,E is''test''endl+ ?? 1
test = 0!00+cout''0!00 is''test''endl+ ?? 0test = s%rt(,!)/s%rt(,!)-,+
cout''test''endl+ ?? 1
7/24/2019 4.1 Boolean Operations
11/24
Pe QorganMs Law
Logical conditions can e %uite confusing! onsider following e#amples
1! Aaise &our hand if &ou are under the age of ,0 K2PdriverMs license!
,! Aaise &our hand if &ou are under the age of ,0 OA hdriverMs license!
E! Aaise &our hand if &ou are not under the age of ,0$ donMt have a driverMs license!
.! Aaise &our hand if &ou are not under the age of ,0$
donMt have a driverMs license!
7/24/2019 4.1 Boolean Operations
12/24
7/24/2019 4.1 Boolean Operations
13/24
(K CC B) vs K HH B
K is (#'=5)$ B is (name == John*)!
KCCB is true if BO;4 are true!
KCCB is false if either K is false or B is false!
K is false is denoted & K$ B is false is denoted & B
(KCCB) is true if KCCB is false!;hus$ (KCCB) is e%uivalent to K HH B
7/24/2019 4.1 Boolean Operations
14/24
Pe QorganMs Law
4ere are two e%uivalent formulas that are good to 8now$can e :gured out using asic logic!
;hese two formulas in particular are referred to as Pe QoLaw! ;he& generalie nicel& to more than two conditions
(KCCBCCCCP) = K HH B HH HH P
(KHHBHHHHP) = K CC B CC CC P
7/24/2019 4.1 Boolean Operations
15/24
Pe QorganMs Law
Kvoid negations when possile!
@#R 7se PeQorganMs Law to simplif& the following oolstatements!
7/24/2019 4.1 Boolean Operations
16/24
;ruth ;ale
We can form whatMs called a truth tale to :gure out whetnot a compound statement is true & loo8ing at all possi
Sou oo8 has a more complicated tale in prolem AE!11
A B !A !B A&&B !(A&&B) (A&&B)
0 0 1 1 0 1 1
0 1 1 0 0 1 01 0 0 1 0 1 1
1 1 0 0 1 0 1
7/24/2019 4.1 Boolean Operations
17/24
6nput Talidation
Fo far weMve assumed the user gives us properinput! But what if the& donMt
When &ou do a cin statement$ it actuall& return
ooleanR trueif &ou were ale to read the valucorrectl&$ falseif it failed to read the speci:ed
Aeturns the oolean truefor a success$ so we use cin in an if statement!
Klternativel& &ou could use the memer funct
cin!fail( ) descried in Fec .!.!
7/24/2019 4.1 Boolean Operations
18/24
6nput Talidation
;he line
if(cin II n)
does , thingsR
1!) Uets input n from user!
,!) hec8s if read
successful!
returnwill end the program!
7se the returncommandwith caution!
7/24/2019 4.1 Boolean Operations
19/24
6nput TalidationR cin!fail()
Knother wa& is to use the cin!fail() function!
cin II n returns trueif cin!fail() is false!
cin II n returns falseif cin!fail() is true!
if(cin II n)cin II n+if((cin!fail()))
is e%uivalentto
7/24/2019 4.1 Boolean Operations
20/24
Loop$ there it is
WeMve seen that & default$ code starts from the top agoes to the ottom$ reading and e#ecuting line & line
We Vust saw how the if-elsestatements allow us to s8icertain parts of code on our wa& down!
2ow we are going to e ale to repeat segments of co
7/24/2019 4.1 Boolean Operations
21/24
Fta& for a while
;he most asic loop is a whileloop! 6t loo8s li8e thefollowing$ doule # = 1,E.5+
while( # I= 10) cout'';he :nal value of # is''#''endl+
7/24/2019 4.1 Boolean Operations
22/24
while
;his will store # as 1,E.5$ and then test to see if # I= 10 is tru
6f true$ it will run the lines of code inside the rac8ets$ and when to the end it will test the condition #I=10 again!
6f true$ it will run the lines of code inside the rac8ets$ and when to the end it will test the condition #I=10 again!
;his will continue until # I= 10 is false$ at which point the loopinand the remaining code will e e#ecuted as usual!
doule # = 1,E.5+while( # I= 10) cout'';he :nal value of # is''#''endl+
7/24/2019 4.1 Boolean Operations
23/24
6n:nit& loop
Kn in:nite loop is an& loop that will not terminate on it"or e#ample$
doule # = 1,+while( # I= 10)
7/24/2019 4.1 Boolean Operations
24/24
K more sutle ug
Qost in:nite loops are simpl& caused & carelessness
@ven though we are printing out # and testing for #$ wnot changing the value of # inside of the loop!
doule # = 1,+doule & = 15+while( # I= 10)