Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.

Similar presentations


Presentation on theme: "Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars."— Presentation transcript:

1 Computer Science 1620 boolean

2 Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars

3 The boolean a logical value can either be true or false to make a boolean variable, indicate the type as bool #include using namespace std; int main() { bool valid; }

4 Bool values a boolean can be assigned a value of true or false #include using namespace std; int main() { bool valid; valid = true; // assigns a value of true to valid's memory valid = false; // assigns a value of false to valid's memory return 0; }

5 Booleans as expressions #include using namespace std; int main() { cout << true << endl; bool b = false; cout << false << endl; return 0; }

6 Booleans as expressions why did we see a 1 and 0, instead of true and false a bool is considered an integer type a true is stored as a 1, and a false as a zero if you wish to see true and false instead, you can use the boolalpha format flag note: does not require iomanip header file

7 Booleans as expressions #include using namespace std; int main() { cout << boolalpha; cout << true << endl; bool b = false; cout << b << endl; return 0; }

8 Our conversion rules (approximate) when integer promoted to floating point: integer is converted to decimal representation no cast required when floating point demoted to an integer: floating point converted to integer fractional part of number dropped cast required to override warning how does a boolean fit into this? what happens when a boolean is converted to a number? what happens when a number is converted to a boolean?

9 Booleans to Integers #include using namespace std; int main() { bool b = true; cout << b << endl; cout (false) << endl; return 0; }

10 Booleans to Floating Point #include using namespace std; int main() { cout << showpoint; double i = true; cout << i << endl; cout (false) << endl; return 0; }

11 Rule: when a boolean is converted to a number: a true value becomes the number 1 (or 1.0) a false value becomes the number 0 (or 0.0) no cast is necessary

12 Numbers to Booleans #include using namespace std; int main() { cout << boolalpha; cout ( 5 ) << endl; cout ( -5) << endl; cout ( 0) << endl; cout ( 5.0 ) << endl; cout ( -5.0) << endl; cout ( 0.0) << endl; return 0; }

13 Rule: when a number is converted to a boolean: any non-zero value becomes true any zero value becomes false no cast is necessary

14 What about conversions in an expression? #include using namespace std; int main() { cout << (5 + false) << endl; cout << (6.2 + true) << endl; return 0; }

15 Rule: when a boolean is used where a numeric expression is expected, it is converted to a number false = 0 true = 1 when a number is used where a boolean expression is expected, it is converted to a boolean non-zero = true zero = false

16 Boolean Expressions two types: 1. Relational operators used for comparing values 2. Logical operators used for combining boolean values

17 Relational Operators syntax: ExpressionOpExpression OperatorValue of Expression ==true if left side and right side evaluate to the same value, false otherwise !=false if left side and right side evaluate to the same value, true otherwise <true if left side is less than right side, false otherwise >true if left side is greater than right side, false otherwise <=true if left side is less than or equal to right side, false otherwise >=true if left side is greater than or equal to right side, false otherwise

18 Example #include using namespace std; int main() { cout << boolalpha; cout << "(3 == 3)=" << (3 == 3) << endl; cout << "(3 == 4)=" << (3 == 4) << endl; cout << "(3.0 != 3.0)=" << (3.0 != 3.0) << endl; cout << "(3.0 != 4.0)=" << (3.0 != 4.0) << endl; cout << "(3 < 4)=" << (3 < 4) << endl; cout 3)=" 4) << endl; cout << "(3.0 <= 3.0)=" << (3.0 <= 3.0) << endl; cout = 4.0)=" = 4.0) << endl; return 0; }

19 What happens if I compare different types? bool test = (7.0 < 4); // what happens here? Same rules as for arithmetic expression: smaller type is converted to larger type relational operators are left to right associative

20 Example: Write a program that reads your age, and displays whether you can (a) drive (b) vote, (c) buy beer in Saskatchewan, and (d) buy beer in Montana

21 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads your age, and displays whether you can (a) drive (b) vote, (c) buy beer in Saskatchewan, and (d) buy beer in Montana #include using namespace std; int main() { int age; cout << "Please enter your age: "; cin >> age; cout << boolalpha; cout << "You can: " << endl; cout drive legally: " = 16) << endl; cout vote: " = 18) << endl; cout buy beer in Saskatchewan: " = 19) << endl; cout buy beer in Montana: " = 21) << endl; return 0; }

22 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads your age, and displays whether you can (a) drive (b) vote, (c) buy beer in Saskatchewan, and (d) buy beer in Montana #include using namespace std; int main() { int age; cout << "Please enter your age: "; cin >> age; cout << boolalpha; cout << "You can: " << endl; cout drive legally: " = 16) << endl; cout vote: " = 18) << endl; cout buy beer in Saskatchewan: " = 19) << endl; cout buy beer in Montana: " = 21) << endl; return 0; }

23 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads your age, and displays whether you can (a) drive (b) vote, (c) buy beer in Saskatchewan, and (d) buy beer in Montana #include using namespace std; int main() { int age; cout << "Please enter your age: "; cin >> age; cout << boolalpha; cout << "You can: " << endl; cout drive legally: " = 16) << endl; cout vote: " = 18) << endl; cout buy beer in Saskatchewan: " = 19) << endl; cout buy beer in Montana: " = 21) << endl; return 0; }

24 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads your age, and displays whether you can (a) drive (b) vote, (c) buy beer in Saskatchewan, and (d) buy beer in Montana #include using namespace std; int main() { int age; cout << "Please enter your age: "; cin >> age; cout << boolalpha; cout << "You can: " << endl; cout drive legally: " = 16) << endl; cout vote: " = 18) << endl; cout buy beer in Saskatchewan: " = 19) << endl; cout buy beer in Montana: " = 21) << endl; return 0; }

25 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads your age, and displays whether you can (a) drive (b) vote, (c) buy beer in Saskatchewan, and (d) buy beer in Montana #include using namespace std; int main() { int age; cout << "Please enter your age: "; cin >> age; cout << boolalpha; cout << "You can: " << endl; cout drive legally: " = 16) << endl; cout vote: " = 18) << endl; cout buy beer in Saskatchewan: " = 19) << endl; cout buy beer in Montana: " = 21) << endl; return 0; }

26 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads your age, and displays whether you can (a) drive (b) vote, (c) buy beer in Saskatchewan, and (d) buy beer in Montana #include using namespace std; int main() { int age; cout << "Please enter your age: "; cin >> age; cout << boolalpha; cout << "You can: " << endl; cout drive legally: " = 16) << endl; cout vote: " = 18) << endl; cout buy beer in Saskatchewan: " = 19) << endl; cout buy beer in Montana: " = 21) << endl; return 0; }

27 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads your age, and displays whether you can (a) drive (b) vote, (c) buy beer in Saskatchewan, and (d) buy beer in Montana #include using namespace std; int main() { int age; cout << "Please enter your age: "; cin >> age; cout << boolalpha; cout << "You can: " << endl; cout drive legally: " = 16) << endl; cout vote: " = 18) << endl; cout buy beer in Saskatchewan: " = 19) << endl; cout buy beer in Montana: " = 21) << endl; return 0; }

28

29

30

31 Relational Operators on Strings you can use the relational operators on strings string s = "kev"; cout << (s < "math") << endl; // returns true or 1

32 String comparison – how it works: strings are compared character by character, starting from left when a character in string A is found to be less than a corresponding character in string B, then A is less than B

33 kev math String 1 String 2

34 kev math String 1 String 2 Since k < m, string 1 is said to be less than string 2

35 key kev String 1 String 2

36 key kev String 1 String 2 k = k, so we move to the next character

37 key kev String 1 String 2 e = e, so we move to the next character

38 key kev String 1 String 2 v < y, so string 2 is considered to be smaller

39 kev kevin String 1 String 2

40 kev kevin String 1 String 2 string 1 is out of characters, so it is considered smaller than string 2

41 What characters are smaller than others? it is based on their ascii ordering Some general rules: Numbers before letters All capital letters come before any lowercase letters Punctuation, spaces, etc, are mixed in, but are typically low in the ordering

42 How good are you with a mouse? Test yourself here - from the time you start ticking the boxes you have 20 seconds to tick as many as you can. Click restart to clear the boxes and try again. Good luck! How quick are you? Press 'start', then press 'stop' when the border goes red www.asciitable.com

43 cat Dog String 1 String 2

44 cat Dog String 1 String 2 D < c, so string 2 is smaller

45 A note about string comparison you cannot compare two string literals cout << ("Math" < "Kev") << endl; this will compile, but the answer might be unexpected at least one of your operands should be a string variable

46 Example: A particular ride at a Canadian amusement park requires that its guests must be between 32" and 60" tall. Write a program that reads a height (in inches), and computes whether that person is eligible.

47 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads a height (in inches), and computes whether that person is eligible. #include using namespace std; int main() { int height; cout << "Please enter your height: "; cin >> height; cout << "This person is eligible to ride: " << (32 <= height <= 60) << endl; return 0; }

48 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads a height (in inches), and computes whether that person is eligible. #include using namespace std; int main() { int height; cout << "Please enter your height: "; cin >> height; cout << "This person is eligible to ride: " << (32 <= height <= 60) << endl; return 0; }

49 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads a height (in inches), and computes whether that person is eligible. #include using namespace std; int main() { int height; cout << "Please enter your height: "; cin >> height; cout << "This person is eligible to ride: " << (32 <= height <= 60) << endl; return 0; }

50 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads a height (in inches), and computes whether that person is eligible. #include using namespace std; int main() { int height; cout << "Please enter your height: "; cin >> height; cout << boolalpha; cout << "This person is eligible to ride: " << (32 <= height <= 60) << endl; return 0; }

51 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads a height (in inches), and computes whether that person is eligible. #include using namespace std; int main() { int height; cout << "Please enter your height: "; cin >> height; cout << boolalpha; cout << "This person is eligible to ride: " << (32 <= height <= 60) << endl; return 0; }

52

53

54 What happened? the relational operators are a binary operator read left to right 32 <= height <= 60 false <= 60 0 true

55 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads a height (in inches), and computes whether that person is eligible. #include using namespace std; int main() { int height; cout << "Please enter your height: "; cin >> height; cout << boolalpha; cout << "This person is eligible to ride: " << (32 <= height <= 60) << endl; return 0; } This statement is saying: compute the result of 32 <= height compute whether that boolean answer is less than 60

56 Logical Operators a way to combine boolean expressions OperatorValue of Expression !true if its associated expression is false, false otherwise &&true if its left and right side are true, false otherwise ||false if its left and right side are false, true otherwise

57 The ! (Not) Operator syntax the not operator creates an expression that returns the opposite value of the input expression ! Boolean Expression

58 Example #include using namespace std; int main() { cout << boolalpha; cout << "(3 == 3)=" << (3 == 3) << endl; cout << "!(3 == 3)=" << !(3 == 3) << endl; cout << "(3.0 != 3.0)=" << !(3.0 != 3.0) << endl; cout << "!(3 == 3)=" << !(3 == 3) << endl; cout << "(3 < 4)=" << (3 < 4) << endl; cout << "!(3 < 4)=" << !(3 < 4) << endl; cout << "!25=" << !25 << endl; cout << "!0.0=" << !0.0 << endl; return 0; }

59 The || (or) and && (and) Operators syntax the or operator creates a boolean expression that is true if at least one of its operands are true the and operator creates a boolean expression that is true if both of its operands are true Boolean Expression op

60 Example #include using namespace std; int main() { cout << boolalpha; cout << "(f || f)=" << false || false << endl; cout << "(f || t)=" << false || true << endl; cout << "(t || f)=" << true || false << endl; cout << "(t || t)=" << true || true << endl; cout << "(f && f)=" << false && false << endl; cout << "(f && t)=" << false && true << endl; cout << "(t && f)=" << true && false << endl; cout << "(t && t)=" << true && true << endl; return 0; }

61 Back to our example: to be eligible to ride the amusement ride, a person's height has to be 32 <= h <= 60 another way of saying this is: the person's height has to be >= 32 AND the person's height has to be <= 60

62 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads a height (in inches), and computes whether that person is eligible. #include using namespace std; int main() { int height; cout << "Please enter your height: "; cin >> height; cout << boolalpha; cout = 32) && (height <= 60)) << endl; return 0; } height must be >= 32 AND height must be <= 60

63 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads a height (in inches), and computes whether that person is eligible. #include using namespace std; int main() { int height; cout << "Please enter your height: "; cin >> height; cout << boolalpha; cout = 32) && (height <= 60)) << endl; return 0; } height must be >= 32 AND height must be <= 60

64 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads a height (in inches), and computes whether that person is eligible. #include using namespace std; int main() { int height; cout << "Please enter your height: "; cin >> height; cout << boolalpha; cout = 32) && (height <= 60)) << endl; return 0; } height must be >= 32 AND height must be <= 60

65 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads a height (in inches), and computes whether that person is eligible. #include using namespace std; int main() { int height; cout << "Please enter your height: "; cin >> height; cout << boolalpha; cout = 32) && (height <= 60)) << endl; return 0; } height must be >= 32 AND height must be <= 60

66 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads a height (in inches), and computes whether that person is eligible. #include using namespace std; int main() { int height; cout << "Please enter your height: "; cin >> height; cout << boolalpha; cout = 32) && (height <= 60)) << endl; return 0; } height must be >= 32 AND height must be <= 60

67 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads a height (in inches), and computes whether that person is eligible. #include using namespace std; int main() { int height; cout << "Please enter your height: "; cin >> height; cout << boolalpha; cout = 32) && (height <= 60)) << endl; return 0; } height must be >= 32 AND height must be <= 60

68 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads a height (in inches), and computes whether that person is eligible. #include using namespace std; int main() { int height; cout << "Please enter your height: "; cin >> height; cout << boolalpha; cout = 32) && (height <= 60)) << endl; return 0; } height must be >= 32 AND height must be <= 60

69

70 Logical Operators with non-booleans numbers can be used with logical operators since operators expect booleans, the numbers will be converted to boolean non-zero = true zero = false

71 ExpressionValue true && falsefalse 0 || 4true true && 0false int a = 3, b = 4; (a == 3) && (b == 3) false int a = 3, b = 4; (a = 3) && (b = 3) true true && !(false || true)false !3.3false

72 Operator Precedence our precedence so far has been highest precedence lowest precedence Operator ( ), ++ (postfix), -- (postfix) - (unary), ++(prefix), --(prefix) *, /, % +, - =, +=, -=, *=, /=, %= Where do the relational/logical operators fit?

73 Operator Precedence our precedence so far has been highest precedence lowest precedence Operator ( ), ++ (postfix), -- (postfix) - (unary), ++(prefix), --(prefix) *, /, % +, - =, > ==, != && || =, +=, -=, *=, /=, %=

74 Example (text): (17 < 4 * 3 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || =

75 Example (text): (17 < 4 * 3 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || =

76 Example (text): (17 < 4 * 3 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || =

77 Example (text): (17 < 4 * 3 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || = (17 < 12 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6)

78 Example (text): (17 < 4 * 3 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || = (17 < 12 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6)

79 Example (text): (17 < 4 * 3 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || = (17 < 12 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) (17 < 17) || (8 * 2 == 4 * 4) && !(3 + 3 == 6)

80 Example (text): (17 < 4 * 3 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || = (17 < 12 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) (17 < 17) || (8 * 2 == 4 * 4) && !(3 + 3 == 6)

81 Example (text): (17 < 4 * 3 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || = (17 < 12 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) (17 < 17) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) false || (8 * 2 == 4 * 4) && !(3 + 3 == 6)

82 Example (text): (17 < 4 * 3 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || = (17 < 12 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) (17 < 17) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) false || (8 * 2 == 4 * 4) && !(3 + 3 == 6)

83 Example (text): (17 < 4 * 3 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || = (17 < 12 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) (17 < 17) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) false || (8 * 2 == 4 * 4) && !(3 + 3 == 6)

84 Example (text): (17 < 4 * 3 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || = (17 < 12 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) (17 < 17) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) false || (8 * 2 == 4 * 4) && !(3 + 3 == 6) false || (16 == 4 * 4) && !(3 + 3 == 6)

85 Example (text): (17 < 4 * 3 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || = (17 < 12 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) (17 < 17) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) false || (8 * 2 == 4 * 4) && !(3 + 3 == 6) false || (16 == 4 * 4) && !(3 + 3 == 6)

86 Example (text): (17 < 4 * 3 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || = (17 < 12 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) (17 < 17) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) false || (8 * 2 == 4 * 4) && !(3 + 3 == 6) false || (16 == 4 * 4) && !(3 + 3 == 6) false || (16 == 16) && !(3 + 3 == 6)

87 Example (text): (17 < 4 * 3 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || = (17 < 12 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) (17 < 17) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) false || (8 * 2 == 4 * 4) && !(3 + 3 == 6) false || (16 == 4 * 4) && !(3 + 3 == 6) false || (16 == 16) && !(3 + 3 == 6)

88 Example (text): (17 < 4 * 3 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || = (17 < 12 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) (17 < 17) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) false || (8 * 2 == 4 * 4) && !(3 + 3 == 6) false || (16 == 4 * 4) && !(3 + 3 == 6) false || (16 == 16) && !(3 + 3 == 6) false || true && !(3 + 3 == 6)

89 Example (text): Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || = false || true && !(3 + 3 == 6)

90 Example (text): Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || = false || true && !(3 + 3 == 6)

91 Example (text): Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || = false || true && !(3 + 3 == 6) false || true && !(6 == 6)

92 Example (text): Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || = false || true && !(3 + 3 == 6) false || true && !(6 == 6)

93 Example (text): Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || = false || true && !(3 + 3 == 6) false || true && !(6 == 6) false || true && ! true

94 Example (text): Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || = false || true && !(3 + 3 == 6) false || true && !(6 == 6) false || true && ! true

95 Example (text): Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || = false || true && !(3 + 3 == 6) false || true && !(6 == 6) false || true && ! true false || true && false

96 Example (text): Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || = false || true && !(3 + 3 == 6) false || true && !(6 == 6) false || true && ! true false || true && false

97 Example (text): Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || = false || true && !(3 + 3 == 6) false || true && !(6 == 6) false || true && ! true false || true && false false || false

98 Example (text): Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || = false || true && !(3 + 3 == 6) false || true && !(6 == 6) false || true && ! true false || true && false false || false

99 Example (text): Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || = false || true && !(3 + 3 == 6) false || true && !(6 == 6) false || true && ! true false || true && false false || false false

100 Example (text): Other examples from the text are very good, and come with explanations (17 < 4 * 3 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) Operator ( ) !, - (unary) *, /, % +, - =, > ==, != && || = = FALSE

101 Short Circuiting what is the value of these statements? (3 3)= false (3 < 2) && (false)= false (3 < 2) &&= false ???? Do you need to know what is behind the box?

102 Short Circuiting logical expressions evaluated left to right if the left side of an AND expression is false, then the entire expression is false, regardless of the right side if the left side of an OR expression is true, then the entire expression is true, regardless of the right side

103 Short Circuiting in C++ when the left side of an AND is false, C++ never executes the right side when the left side of an OR is true, C++ never executes the right side

104 Example #include using namespace std; int main() { int x = 0; cout << x << endl; ( 3 < 4 ) && (x = 2); // assignment! cout << x << endl; ( 3 > 4) && (x = 4); cout << x << endl; (3 > 4) || (x = 6); cout << x << endl; (3 < 4) || (x = 8); cout << x << endl; return 0; } Right side not executed applepie $ g++ -o bool bool.cc applepie $./bool 0 2 6 applepie $


Download ppt "Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars."

Similar presentations


Ads by Google