Boolean Expressions
Problem – Professor Imberman
wishes to use a grading program to give students feedback on their work. When a student enters his/her grade into the
program, the program will provide the student with an appropriate comment assessing
the student’s performance. Students
entering values of 98, 99, or 100 will receive the message “Great Work”. All other grades lower than 98 will get the
comment, “Better luck next time”.
Software Development
Procedure:
Inputs – A grade
Output – Comment based on
grades value
Extended Analysis
if grade
is 98 or greater print “Great Work”
if it is
less than 98 print “Better luck next time!”
Algorithm:
1.
Prompt user for
grade
2.
Read in grade
3.
if grade >= 98
print “Great work”
4.
if it is less than 98 print “Better luck next time!”
We need a C++ statement that
can do steps 3 and 4.
·
Means of
evaluating relationships between things
·
Based on the
relationship, make a decision
Given the statement a = b,
mathematically, the statement is true or false depending on the values of a and b.
For example: if a = 2 and b = 5 the statement is false.
The same is true for the
relational operators < and > for
a < b, if a = 2 and b = 5 then 2 < 5 is true.
Any expression for which we can
determine the truth value is called a boolean
expression.
A boolean
variable is a variable that can take on the values of true and false.
In C++ the bool data type allows for a the
declaration of boolean variables.
bool truthvalue;
truthvalue = 1;
truthvalue = 0;
truthvalue = true;
truthvalue = false;
In C++ a value of 1 is
interpreted as being true, and a value of 0 is false.
Boolean expressions are
expressions that evaluate to being either 1 or 0, true or false. The simplest boolean
expression are the values 0, 1, true, or false.
Boolean variables can be assigned the resulting value of a boolean expression.
We can use relational
operators to create complex boolean expressions. Below are the relational operators.
Operator |
Meaning |
< |
Less than |
> |
Greater than |
== |
Equal to (Notice there are two equal signs as opposed to the assignment operator which is a single equal sign |
<= |
Less than or equal to |
>= |
Greater than or equal to |
!= |
Not equal to |
Examples of boolean expressions
using relational operators
Expression |
Evaluates to |
4 > = 3 |
1 (true) |
7 < = 126 |
1 (true) |
5 == (3 + 3) |
0 (false) |
‘A’ < ‘B’ |
1 (true) |
“Absorb” < “Abduct” |
0 (false) |
“Absorb” < “abduct” |
1 (true) |
#include <iostream>
using namespace std;
int
main()
{
bool truthvalue;
truthvalue = 4 >= 3;
cout << "4 >= 3" << "
evaluates to ";
cout << truthvalue << endl;
truthvalue = 7 <= 126;
cout << "7 <= 126 "<< "
evaluates to ";
cout << truthvalue << endl;
truthvalue = 5 == (3+3);
cout << "5 == (3+3)" << "
evaluates to ";
cout << truthvalue << endl;
truthvalue = 'A' < 'B';
cout << "\'A\' < \'B\'" << "
evaluates to ";
cout << truthvalue << endl;
truthvalue = 'a' < 'b';
cout << "\'a\' < \'b\'" << "
evaluates to ";
cout << truthvalue << endl;
truthvalue = 'A' < 'a';
cout << "\'A\' < \'a\'" << "
evaluates to ";
cout << truthvalue << endl;
return 0;
}
4 >= 3 evaluates to 1
7 <=126 evaluates to 1
5 == (3+3) evaluates to 0
'A' < 'B' evaluates to 1
'a' < 'b' evaluates to 1
'A' < 'a' evaluates to 1
Press any key to continue
For ASCII characters, ‘A’ < ‘B’ <
…< ‘Z’
‘0’ < ‘1’ < … <’9’
‘a’ < ‘b’ < … <’z’
‘0’ < ‘1’ < … <’9’
< ‘A’ < ‘B’ < …< ‘Z’ < ‘a’ < ‘b’ < … <’z’
Recall, each ASCII character
evaluates to a numeric value. The above
represents the underlying numeric value of each characgter.
#include
<iostream>
using namespace std;
void
main ()
{
cout <<
"The truth value of A < a is: " << ('a' < 'A') << endl;
cout <<
"The numeric value of a is: " << int('a')
<< endl;
cout <<
"The numeric value of A is: " << int('A')
<< endl;
cout <<
"The letter value of 78 is: " << char(78) << endl;
}
The
truth value of ‘a’ < ‘A’ is: 0
The
numeric value of a is: 97
The
numeric value of A is: 65
The
letter value of 78 is: N
Press
any key to continue
#include
<iostream>
using namespace std;
int
main ()
{
bool
truthvalue1, truthvalue2;
int
num1, num2;
num1 = 3;
num2 = 6;
truthvalue1 = num2 < num1;
cout <<
"The truth of num2 < num1 is: " << truthvalue1 << endl;
truthvalue1 = num2 > num1;
cout <<
"The truth of num2 > num1: " << truthvalue1 << endl;
truthvalue2 = num2 == num1;
cout <<
"The truth of num2 == num1: " << truthvalue2 << endl;
return 0;
}
The
truth of num2 < num1 is: 0
The
truth of num2 > num1: 1
The
truth of num2 == num1: 0
Press
any key to continue
#include
<iostream>
using namespace std;
int
main ()
{
bool
truthvalue1, truthvalue2;
int
num1, num2;
num1 = 3;
num2 = 6;
truthvalue1 = num2 < num1;
cout <<
"The truth of num2 < num1 is: " << truthvalue1 << endl;
truthvalue1 = num2 > num1;
cout <<
"The truth of num2 > num1: " << truthvalue1 << endl;
truthvalue2 = num2 == num1;
cout <<
"The truth of num2 == num1: " << truthvalue2 << endl;
truthvalue2 = num2 = num1;
cout <<
"The truth of num2 == num1: " << truthvalue2 << endl;
cout <<
"The value of num1 is: " << num1 << endl;
cout <<
"The value of num2 is: " << num2 << endl;
return 0;
}
--------------------Configuration:
bool2 - Win32 Debug----------------
Compiling...
bool2.cpp
c:\csc1\bool2.cpp(26) : warning C4800: 'int'
: forcing value to bool 'true' or 'false'
(performance warning)
bool2.obj
- 0 error(s), 1 warning(s)
The
truth of num2 < num1 is: 0
The
truth of num2 > num1: 1
The
truth of num2 == num1: 0
The
truth of num2 == num1: 1
The
value of num1 is: 3
The
value of num2 is: 3
Press
any key to continue
#include
<iostream>
using namespace std;
int
main ()
{
bool
truthvalue1, truthvalue2;
int
num1, num2;
num1 = 3;
num2 = 6;
truthvalue1 = num2 < num1;
cout <<
"The truth of num2 < num1 is: " << truthvalue1 << endl;
truthvalue1 = num2 > num1;
cout <<
"The truth of num2 > num1: " << truthvalue1 << endl;
truthvalue2 = num2 == num1;
cout <<
"The truth of num2 == num1: " << truthvalue2 << endl;
num2 = 3;
truthvalue2 = num2 == num1;
cout <<
"The truth of num2 == num1: " << truthvalue2 << endl;
return 0;
}
The
truth of num2 < num1 is: 0
The
truth of num2 > num1: 1
The
truth of num2 == num1: 0
The
truth of num2 == num1: 1
Press
any key to continue
#include
<iostream>
#include
<string>
using namespace std;
int
main()
{
bool
truthvalue1;
string word1,
word2;
word1 = "repeat";
word2 = "reptile";
truthvalue1 = word1 < word2;
cout <<
"The truth value of word1 < word2 is: " << truthvalue1
<< endl;
word1 = "bill";
word2 = "billygoat";
truthvalue1 = word1 < word2;
cout <<
"The truth value of word1 < word2 is: " << truthvalue1
<< endl;
word1 = "123";
word2 = "1234";
truthvalue1 = word1 < word2;
cout <<
"The truth value of word1 < word2 is: " << truthvalue1
<< endl;
return 0;
}
The
truth value of word1 < word2 is: 1
The
truth value of word1 < word2 is: 1
The
truth value of word1 < word2 is: 1
Press
any key to continue
In addition to the boolean operators, we also have special boolean
operators. These allow for more complex boolean expressions.
AND &&, OR ||, and NOT !
How are boolean
expressions with boolean operators evaluated?
Assume that P represents one boolean expression that can evaluate to true (1) or false
(0) and Q represents another boolean expression.
&&, || and ! are binary operators which means the work on two expressions.
P |
Q |
P && Q |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
0 |
1 |
1 |
1 |
Both P and Q must be true
individually for P&&Q to be true
P |
Q |
P || Q |
0 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
1 |
1 |
1 |
1 |
Either P or Q must be true
individually for P||Q to be true.
Negation (!) or NOT is a
unary operator
P |
!P |
0 |
1 |
1 |
0 |
Evaluate:
ans = !P || Q && P || Q
if P = 1, Q = 1
1.
parenthesized
expressions
2. unary operators !, -
3. *, /, %
4.
binary +, -
5. <. >, <=, >=
1. ==, !=
2. && and
3. || or
4. assignment statements =, +=, -=, *=, %=, /=
#include <iostream>
using namespace std;
int
main()
{
bool truthvalue;
truthvalue = 4 >= 3;
cout << "4 >= 3" << "
evaluates to ";
cout << truthvalue << endl;
truthvalue = 7 <=126;
cout << "7 <=126 "<< "
evaluates to ";
cout << truthvalue << endl;
truthvalue = 5 == (3+3);
cout << "5 == (3+3)" << "
evaluates to ";
cout << truthvalue << endl;
truthvalue = 2 < 3;
cout << "2 < 3 "<< " evaluates
to ";
cout << truthvalue << endl;
//Which
is evaluated first, the relational or boolean
operators?
truthvalue = 4 >= 3 && 7 <= 126;
cout << "4 >= 3 && 7 <= 126"
<< " evaluates to ";
cout << truthvalue << endl;
truthvalue = 5 == (3+3) || 2 <
3;
cout << "5 == (3+3) || 2 < 3" <<
" evaluates to ";
cout << truthvalue << endl;
return 0;
}
4 >= 3 evaluates to 1
7 <=126 evaluates to 1
5 == (3+3) evaluates to 0
2 < 3 evaluates to 1
4 >= 3 && 7 <= 126 evaluates to 1
5 == (3+3) || 2 < 3 evaluates to 1
Press any key to continue
bool
truthvalue;
truthvalue
= (2>5) || 3 ==(2 + 1) || (5 < 10) || (6 != (5 + 1)) || (7 <= 6);
Short circuit evaluation –
Boolean, or logical, expressions are evaluated up to the point at which the
truth value is known. If a portion of
the expression is not needed to make a truth evaluation, it is not evaluated.