Programming in C++ int age bool is senior hasFever float temperature age= 20; temperature =102.0; sSenior =(age >=55); ∥/ is Senior is false has Fever=(temperature>98.6);/hasFever is true EXPRESSION VALUE is Senior & hasFever false is Senior I‖ hasFever true I seNior true hasFever false 16
16 int age ; bool isSenior, hasFever ; float temperature ; age = 20; temperature = 102.0 ; isSenior = (age >= 55) ; // isSenior is false hasFever = (temperature > 98.6) ; // hasFever is true EXPRESSION VALUE isSenior && hasFever false isSenior || hasFever true ! isSenior true ! hasFever false
Programming in C++ What is the value? int age, height; age 25: height =70; EXPRESSION VALUE Hage <10 (height >60
17 What is the value? int age, height; age = 25; height = 70; EXPRESSION VALUE !(age < 10) ? !(height > 60) ?
Programming in C++ Short-Circuit Evaluation C++ uses short circuit evaluation of logical expressions this means logical expressions are evaluated left to right and evaluation stops as soon as the final truth value can be determined
18 “Short-Circuit” Evaluation ❖C++ uses short circuit evaluation of logical expressions ❖this means logical expressions are evaluated left to right and evaluation stops as soon as the final truth value can be determined
Programming in C++ Short-Circuit Example int age, height; age = 25 height =70; EXPRESSION (age >50) &&(height> 60) false Evaluation can stop now because result of & is only true when both sides are true. It is already determined that the entire expression will be false 19
19 Short-Circuit Example int age, height; age = 25; height = 70; EXPRESSION (age > 50) && (height > 60) false Evaluation can stop now because result of && is only true when both sides are true. It is already determined that the entire expression will be false
Programming in C++ More Short-Circuiting int age, height; age= 25, height =70; EXPRESSION (height> 60)(age 40 true Evaluation can stop now because result of is true if one side is true. It is already determined that the entire expression will be true 20
20 More Short-Circuiting int age, height; age = 25; height = 70; EXPRESSION (height > 60) || (age > 40) true Evaluation can stop now because result of || is true if one side is true. It is already determined that the entire expression will be true