C++ Fundamentals

advertisement
Expression Review
what is the result and type of these expressions?
int x=2, y=15;
double
u=2.0, v=15.0;
-x
x+y
x-y
x*v
y / x
x/y
y%x
u*v
u/v
v/u
u%v
x * u
(x+y)*u
u /(x-x)
x++;
u++;
u =
--x;
u *= ++x;
x%y
u = x--;
v /= x;
1
Constants and Variables Review



are these constants legal?
 .5
5.E
0.5e3
 ’a’
’%’
’Ab’
0.5E-3
”Ab”
are these variable names legal?
 MyTotal
My_Total
what does this mean?
int mytotal=0;

what would be stored in myvar?
int myvar;
myvar=2.56;

is this code correct?
const int i=55;
++i;
0.5E-3.5
__MyTotal
My Total
int yourtotal(1);
what would be stored in yourvar?
double yourvar;
yourvar=5/2;
2
Logical Expressions, IF
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
4
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
q
false
true
false
true
p && q
false
false
false
true
5
Boolean Algebra

truth table for || (logical “or”)
p
false
false
true
true
q
false
true
false
true
p || q
false
true
true
true
6
Boolean Algebra

truth table for ! (logical “not”)
p
!p
false
true
true
false
7
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
8
Example Logical Expressions
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);
9
Relational Operators

equality operators
== note the two equal signs
!=

examples
int i = 32;
int k = 45;
bool q = i == k;
bool r = i != k;
10
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;
11
Operator Precedence Expanded
precedence of operators (from highest to lowest)
()
Unary + * / %
+ >
<
>=
>=
!=
==
&&
||
=
12
Examples of Logical Expressions
int a = 5,
bool
bool
bool
bool
bool
bool
bool
bool
bool
d
e
f
g
h
j
l
m
n
=
=
=
=
=
=
=
=
=
b = 10,
c = 20;
a < b;
a > b;
(a > b) || (b <
(a > b) && (b <
!(a < b); bool
2*a == b; bool
!((a+b) != c);
(a+b) == (c-a);
(a+b) >= (c-a);
c
c
i
k
);
);
= !(a==b);
= (a+b) >= c;
int o=a;
int p=o=b; // what is the outcome of this statement?
bool q=true; q = d = false;
13
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
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
14
Conditional Constructs


provide ability to control whether a statement is executed
constructs
 if-statement
– if
– if-else
– if-else-if
 switch-statement

conditional statement
15
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 * taxRate;
}
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
16
The Basic If-Statement

syntax
if (expression) body

if the expression is true then execute action
body is either a single statement or a block
example 1:
if (v > 0) v =0;



example 2:
if (v < 0) {
v = -v;
++i;
}
expression
true
false
action
17
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
18
The If-Else Statement


syntax
if (expression)
body1
else
body
if expression is true then
execute body1 otherwise
execute body2
if (v == 0)
cout << "v is 0";
else
cout << "v is not 0";
expression
true
false
body1
body2
19
Selection


want to carry out actions depending on the value of an expression
two major ways to do this
 multiway if-statement
– if-else statements “glued” together
 switch statement
20
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!”;
21
Switch Statement


syntax
switch (expression){
case constant:
statements
• expression of any “countable”
break;
type (int, char)
case constant:
• literal or named constant of
statements
same type as expression
default:
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
22
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
}
23
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 +
case '-' : cout << Left case '*' : cout << Left *
case '/' : cout << Left /
default: cout << "Illegal
}
Right << endl; break;
Right << endl; break;
Right << endl; break;
Right << endl; break;
operation" << endl;
24
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 trueexpression, or false-expression otherwise
conditional assignment - if conditional operator is used to assign value to variable
 what branching construct is this assignment equivalent to?
int i = j>0 ? j : -j;
program that calculates the larger 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?
25
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
26
Download