Lecturer 3 Logical Expressions

advertisement
Logical Expressions, IF
M A Rahim Khan
Boolean Algebra
 logical expressions have the one of two values - true or false
 a rectangle has three sides.
 the instructor has a pleasant smile
 the branch of mathematics that deals with this type of logic is called Boolean algebra
 developed by the British mathematician George Boole in the 19th century
 C++ makes extensive use of Boolean algebra in the form of logical expressions; what is an
expression again?
 three key logical operators in C++:
 &&
- logical “and”, binary
 || - logical “or”, binary
 !
- logical “not”, unary
2
Boolean Algebra
 truth tables
 Lists all combinations of operand values and the result of the operator for each
combination
 truth table for && (logical “and’)
P
False
False
True
True
3
Q
False
True
False
True
P && Q
False
False
False
True
Boolean Algebra
 truth table for || (logical “or”)
P
False
False
True
True
4
Q
False
True
False
True
P || Q
False
True
True
True
Boolean Algebra
 truth table for ! (logical “not”)
P
False
True
5
! P
True
False
Boolean Algebra
 can create complex logical expressions by combining simple logical expressions
 example
 ! (P && Q)
 a truth table can be used to determine when a logical expression is true
P
False
False
True
True
Q
False
True
False
True
P && Q
! (P && Q)
False
False
False
True
True
True
True
False
 note that & and | are also legal operators, make sure to use correct ones
6
Example Logical Expressions
7
bool
bool
bool
bool
bool
bool
bool
bool
bool
bool
bool
P
Q
R
S
T
U
V
W
X
Y
Z
=
=
=
=
=
=
=
=
=
=
=
true;
false;
true;
P && Q;
P && !Q
!Q || R;
P || !Q || !R;
P && Q && !R;
Q || (P && R);
!(R && !Q);
!(P && Q && R);
Relational Operators
 equality operators
==
!=
note the two equal signs
 examples
int i = 32;
int k = 45;
bool q = i == k;
bool r = i != k;
8
Relational Operators
 ordering operators
<
>
>= ()
<= ()
 examples
int i = 5;
int k = 12;
bool p = i < 10;
bool q = k > i;
bool r = i >= k;
bool s = k <= 12;
9
Operator Precedence Expanded
 precedence of operators (from highest to lowest)
()
Unary + * / %
+ >
<
>=
==
&&
||
=
10
>=
!=
Examples of Logical Expressions









int a = 5;
int b = 10
int c = 20;
bool d = a < b;
bool e = a > b;
bool f = (a > b) || (b < c );
bool g = (a > b) && (b < c );
bool h = !(a < b); bool i = !(a==b);
bool j = 2*a == b; bool k = (a+b) >= c;
bool l = !((a+b) != c);
bool m = (a+b) == (c-a);
bool n = (a+b) >= (c-a);
 int o=a;
 int p=o=b; what is the outcome of this statement?
 bool q=true; q = d = false;
11
Operator Precedence Revisited
 same or different?
(a*b)+c
a*(b+c)
(a+b) > c
a+(b>c)
(a > b) == (b > c)
(a == b) > (b == c)
(a != b) && (c <= d)
(a > b) && (c || d)
(a = b) && c
12
a*b + c
a*b + c
a + b > c
a + b > c
a > b == b > c
a == b > b == c
a != b && c <= d
a > b && c || d
a = b && c
Conditional Constructs
 provide ability to control whether a statement is executed
 two constructs
 if-statement
 if
 if-else
 if-else-if
 switch-statement
13
Blocks and Local Variables
 a list of statements enclosed in curly brackets is called a block
 block may be placed anywhere a statement can be placed (note the placement of brackets:
if ((saleType == ’W’) || (saleType == ’w’)) {
total = price * number;
}
 a variable can be declared and used within block, such variable is local to the block and does not
exist outside of it
else if ((saleType == ’R’) || (saleType == ’R’)){
double subtotal;
subtotal = price * number;
total = subtotal + subtotal * TAX_RATE;
}
variable scope – area in program where a variable can be used
 what’s the scope of a variable local to function? block?
 pitfall: a local variable is accessed outside of the block
14
The Basic If-Statement
 syntax
if (expression) action
 if the expression is true then execute action
 action is either a single statement or a block
expression
 example 1:
if (value > 0) value =0;
 example 2:
if (value < 0) {
true
value = -value;
++i;
}
15
action
false
Sorting Two Numbers
cout << "Enter two integers: ";
int n1, n2;
cin >> n1 >> n2;
if (n1 > n2) {
int tmp = n1;
n1 = n2;
n2 = tmp;
}
cout << ”Numbers in order: “
<< n1 << " " << n2 << endl;
programming idiom – a common way of accomplishing a simple task
swapping values of two variables with a third is an idiom
16
The If-Else Statement
 syntax
if (expression)
action1
else
action2
 if expression is true then
execute action1 otherwise
execute action2
if (v == 0)
cout << "v is 0";
else
cout << "v is not 0";
17
expression
true
false
action1
action2
Selection
 it is often the case that depending upon the value of an expression we want to perform a
particular action
 two major ways of accomplishing this
 multiway if-statement
 if-else statements “glued” together
 switch statement
18
Multiway If-Statement
 example
int vclass;
cout << "Enter the vehicle class: ";
cin >> vclass;
if (vclass == 1)
cout << ”Passenger car”;
else if (vclass == 2)
cout << ”Bus”;
else if (vclass == 3)
cout << ”Truck”;
else
cout << ch << ”Unknown vehicle class!”;
19
Switch Statement
syntax
switch (expression) {
case constant:
statements
break;
case constant:
statements
default:
statements
}
20
Switch Example 1
int vclass;
cout << "Enter the vehicle class: ";
cin >> vclass;
switch (vclass){
case 1:
cout << "Passenger car";
break;
case 2:
cout << "Bus";
break;
default:
cout << "Unknown vehicle class! ";
break; // unnecessary but used for consistency
}
21
Switch Example 2
cout << "Enter simple expression: ";
int Left;
int Right;
char Operator;
cin >> Left >> Operator >> Right;
cout << Left << " " << Operator << " " << Right
<< " = ";
switch (Operator) {
case '+' : cout << Left + Right << endl; break;
case '-' : cout << Left - Right << endl; break;
case '*' : cout << Left * Right << endl; break;
case '/' : cout << Left / Right << endl; break;
default: cout << "Illegal operation" << endl;
22
}
Arity and Conditional Operator
 ternary operator – operator accepting three operands
 conditional operator is used as an abbreviated form of branching




23
boolean-expression ? true-expression : false-expression
if boolean-expression is true, then the value of whole expression is true-expression, or falseexpression otherwise
conditional assignment - if conditional operator is used to assign value to variable
 what branching construct is this assignment equivalent to?
 example: int i = j>0 ? j : -j;
program that calculates the largest number ( of two)
int main() {
int n1, n2;
cin >> n1 >> n2;
int max = n1 > n2 ? n1 : n2;
cout << ”maximum is ” << max << endl;
}
arity (again) – number of operands an operator accepts? What arities have we studied?
Named Constants
 there are problems with using literal constants
 9.8 does not give an idea as to what it means in the program
 hard to modify if used in multiple places in program
 named constant provides a name to a constant:
const int windowCount = 5;
const double taxRate = 9.8;
 named constants are usually declared at the beginning of the program. capital letters are
usually used to give the programmer a hint that this is a constant when he encounters it in
the program
 note to C programmers: #define is completely replaced by const in C++
24
Related documents
Download