Constants and Variables Review: Are These Constants Legal? What Is The Term For These Constants?
Constants and Variables Review: Are These Constants Legal? What Is The Term For These Constants?
are these constants legal? what is the term for these constants?
77 .5 5.
’a’ ’5’ ’Ab’ ”Ab” true
what is
an expression? expression evaluation? simple? complex?
operand? operator? arity? unary/binary/ternary?
precedence? compound assignment?
increment? decrement? suffix/prefix form?
1
Expression Review
what is the value and type of these expressions?
-x x+y x-y
x++; ++u;
u = --x; u = x--;
u += x; v -= ++x;
2
Logical Expressions, IF
Boolean Algebra
Boolean (logical) expressions can have two values true or false
branch of mathematics that deals with this type of logic is called
Boolean algebra
developed by British mathematician George Boole in the 19th
century
logical operators in C++
&& - logical “and”, binary
|| - logical “or”, binary
! - logical “not”, unary
4
Truth Table for &&
truth table - lists all combinations of operand values and the value of the
expression for each combinatino
truth table for && (logical “and’)
p q p && q
5
Truth Tables for II and !
p q p || q p !p
6
Complex Boolean Expressions
can create complex logical expressions by combining subexpressions
example
! (p && q)
a truth table can be used to determine when a logical expression is true
note that & and | are also legal operators, make sure to use correct ones
7
Example Boolean Expressions
bool p = true;
bool q = false;
bool r = true;
bool s = p && q;
bool t = p && !q
bool u = !q || r;
bool v = p || !q || !r;
bool w = p && q && !r;
bool x = q || (p && r);
bool y = !(r && !q);
bool z = !(p && q && r);
8
Relational Operators: Equality
relational operators take two operands of any
type, the expression type is boolean
equality operators
== note the two equal signs
!=
examples
int i = 32;
int k = 45;
bool q = i == k;
bool r = i != k;
9
Relational Operators: Ordering
<
>
>=()
<=()
examples
int i = 5;
int k = 12;
bool p = i < 10;
bool q = k > i;
bool r = i >= k;
bool s = k <= 12;
10
Operator Precedence Expanded
precedence of operators (from highest to lowest)
()
++ --
unary + -
* / %
+ -
> < >= <= !=
==
&&
||
=, compound assignment
11
Examples of Relational Operators
int a = 5, b = 10, 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;
12
Operator Precedence Revisited
same or different?
(a*b)+c a*b + c
a*(b+c) a*b + c
(a+b) > c a + b > c
a+(b>c) a + b > c
(a > b) == (b > c) a > b == b > c
(a == b) > (b == c) a == b > b == c
(a != b) && (c <= d) a != b && c <= d
(a > b) && (c || d) a > b && c || d
(a = b) && c a = b && c
13
Conditional Constructs
provide ability to alter the order of statement execution
constructs
branching
– if-statement
basic if
if-else
selection
– multiway if
– switch-statement
14
Blocks and Local Variables
block – a sequence of statements enclosed in curly brackets
block may be placed anywhere a statement can be placed (note curly bracket
position):
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 * taxRate;
}
15
Basic If-Statement
syntax
if (expression) body
semantics: if the expression is true then
execute body
body is either a single statement or a block
expression
example 1:
if (v > 0) v = 0;
true false
example 2:
if (v < 0) {
v = -v; body
++i;
}
16
Sorting Two Numbers
cout << "Enter two integers: ";
int n1, n2;
cin >> n1 >> n2;
18
Arity and Conditional Operator
ternary operator – operator accepting three operands
conditional operator is used as an abbreviated form of branching
boolean-expression ? true-expression : false-expression
if boolean-expression is true, then the value of whole expression is true-expression, or
false-expression otherwise
conditional assignment statement – conditional operator is used to assign value to
variable
what branching construct is this assignment statement equivalent to?
19
Nested Ifs
nested if - one if is the body or inside the block of another
outer if – enclosing if
inner If – enclosed if
21
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!”;
22
Switch Statement
syntax
switch (expression){
case constant:
statements
break; • expression of any “countable”
case constant: type (int, char)
statements • literal or named constant of
default: same type as expression
statements
}
semantics
expression is evaluated, execution continues in first matching case
(optional) default matches any expression value
break-statement terminates the switch statement, execution continues with
a statement following switch-block
if case does not end with break, execution continues to next case
23
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
}
24
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;
}
25
Selection
multiway if vs. switch
switch
only integers, characters, etc. for options
easier to read: specific for selection
multiway if
more general (not just integers for options)
ex: grade > 90 or grade > 80, or grade > 70, etc.
switch can be recoded as multiway if, reverse not always true
harder to read, use switch if possible
note, unlike regular nested if, multiway if shows options of the same selection
specific indentation style
26
Debugging and Tracing Programs
specially compiled executables leave information the original source file:
names of variables and source lines
this allows
program tracing – suspending program execution at specific source
line and executing program one source line at a time
variable watching – observing values stored in source program
variables
breakpoint – line in the source program where execution has to be
suspended
27