OOP1 N
OOP1 N
1 of 90.
5 of 90.
6 of 90.
7 of 90.
8 of 90.
9 of 90.
10 of 90.
11 of 90.
• Implementation dependent
13 of 90.
is equivalent to
“How many”
“ numbers?”
is equivalent to
16 of 90.
17 of 90.
18 of 90.
19 of 90.
20 of 90.
• Statements
a + b;
x = p + q * r;
• Operators
+, *, =
• Operands
a, b, p, q, r, x
21 of 90.
• Unary operator: -, +
neg = -epsilon;
pos = +epsilon;
22 of 90.
a = b + c;
23 of 90.
24 of 90.
x = x + a
x += a
y = 3 * (x += a) + 2; //!!!
25 of 90.
x -= a
x *= a
x /= a
x %= a
26 of 90.
27 of 90.
28 of 90.
29 of 90.
30 of 90.
• syntax:
printf(<format string>, <list of variables>);
• <format string>
String containing text to be printed and
conversion specifications
31 of 90.
%c characters
%d decimals
%f floats or doubles
%s strings
32 of 90.
today = Monday;
the_day_after_tomorrow = Tuesday;
35 of 90.
register int i;
36 of 90.
weeklength = 7; // Error
37 of 90.
38 of 90.
39 of 90.
40 of 90.
41 of 90.
43 of 90.
• semantics
– statement is executed (repeatedly) as long as expression
is non-zero (true)
– expression is evaluated before entry to the loop
46 of 90.
s = 0;
i = 1;
while (i <= n)
{ s += i;
i++;
}
47 of 90.
• semantics
– statement is executed (repeatedly) as long as expression
is non-zero (true)
– expression is evaluated after entry to the loop
48 of 90.
s = 0;
i = 1;
do // incorrect if n == 0
{ s += i;
i++;
} while (i <= n)
49 of 90.
• semantics
– statement1 is executed
– statement2 is executed (repeatedly) as long as
expression2 is true (non-zero)
– expression3 is executed after each iteration (i.e. after
each execution of statement2)
– expression2 is evaluated before entry to the loop
50 of 90.
s = 0;
for (i = 1; i <= n; i++)
s += i;
51 of 90.
s = 0;
for (int i = 1; i <= n; i++)
s += i;
53 of 90.
54 of 90.
55 of 90.
56 of 90.
57 of 90.
58 of 90.
switch (letter)
{ case ‘N’: cout < “New York\n”;
break;
case ‘L’: cout < “London\n”;
break;
case ‘A’: cout < “Amsterdam\n”;
break;
default: cout < “Somewhere else\n”;
break;
}
59 of 90.
switch (letter)
{ case ‘N’: case ‘n’: cout < “New York\n”;
break;
case ‘L’: case ‘l’: cout < “London\n”;
break;
case ‘A’: case ‘a’: cout < “Amsterdam\n”;
break;
default: cout < “Somewhere else\n”;
break;
}
60 of 90.
61 of 90.
• semantics
– if the value of expression1 is true (non-zero)
– then expression2 is evaluated and this is the
value of the entire conditional expression
– otherwise expression3 is evaluated and this is the
value of the entire conditional expression
62 of 90.
z = 3 * (a < b ? a + 1 : b - 1) + 2;
// alternative
if (a < b)
z = 3 * (a + 1) + 2;
else
z = 3 * (b - 1) + 2;
63 of 90.
// alternative
cout << “The greater of a and b is”
if (a < b)
cout << a;
else
cout << b;
64 of 90.
expression1 , expression2
• semantics
– expression1 and expression2 are evaluated in
turn and the value of the entire (compound)
expression is equal to the value of expression2
65 of 90.
s = 0;
while (cin >> i, i > 0)
s += i;
// or ...
s = 0;
while (scanf (“%d”, &i), i > 0)
s += i;
66 of 90.
s = 0;
while (scanf (“%d”, &i) == 1) // terminate on
s += i; // non-integer
// input
67 of 90.
int a[100]
69 of 90.
70 of 90.
71 of 90.
int main()
{ const int LENGTH = 30;
int i, a[LENGTH];
cout << “Enter “ << LENGTH << “ integers:\n”;
for (i=0; i<LENGTH; i++) cin >> a[i];
cout << “\nThe same integers in reverse order:\n”;
for (i=0; i<LENGTH; i++)
cout << setw(6) << a[LENGTH - i - 1]
<< (i % 10 == 9 ? ‘\n’ : ‘ ‘);
return 0;
}
72 of 90.
73 of 90.
a - b * c // ((a - b) * c)
// or (a - (b * c))
• Right-associative operators
– all unary operators
– the operator ?:, used in expressions
– the assignment operators
=, +=, *=, /=, %=, &=, |=, ^=, <<=, >>=
74 of 90.
75 of 90.
() [] . -> ::
! ~ ++ + - (type) * & sizeof new delete // all unary
.* ->*
* / %
+ -
<< >>
< <= > >=
== !=
76 of 90.
&
^
|
&&
||
?:
= += -= *= /= %= &= |= ^= <<= >>=
,
77 of 90.
Operator Meaning
:: scope resolution
() function calls
[] subscripting
. selecting a component of a structure
-> selecting a component of a structure by
means of a pointer
.* pointers to class members
->* pointers to class members
! NOT, unary operator
~ inversion of all bits, unary operator
78 of 90.
Operator Meaning
++ increment, unary operator
-- decrement, unary operator
+ plus, unary operator
+ addition, binary operator
- minus, unary operator
- minus, binary operator
(type) cast, unary operator
new create (allocate memory)
delete delete (free memory)
* ‘contents of address’, unary operator
* multiplication, binary operator
79 of 90.
Operator Meaning
& bitwise AND, binary operator
& ‘address of’, unary operator
sizeof number of bytes inm memory, unary operator
/ division, either floating point or integer
% remainder with integer division
<< shift left; stream output
>> shift right; stream input
< less than
> greater than
<= less than or equal to
>= greater than or equal to
80 of 90.
Operator Meaning
== equal to
!= not equal to
^ bitwise exclusive OR (XOR)
| bitwise OR
&& logical AND
|| logical OR
?: conditional expression
= assignment
+= addition combined with assignent
(other operators can also be combined with
assignment)
81 of 90.
82 of 90.
83 of 90.
84 of 90.
86 of 90.
E1 = E2
3 * 5
i + 1
printf(“&d”, a)
88 of 90.
a = b; // error
• lvalues
(i < j ? i : j) = 0;// assign 0 to
// smaller of i and j
– Since ?: has higher precedence than =, we can
write as:
i < j ? i : j = 0; // !!!
89 of 90.
90 of 90.