|
1
|
|
|
2
|
- Basic arithmetic operators
- Arithmetic operators (+, -, *, /)
- Relational operators (=3D=3D, !=3D, >, <, >=3D, <=3D )=
li>
- Logical operators (!, &&, ||)
|
|
3
|
- Unary operators (-, +, --, ++)
- Binary operators (+, -, *, /, %)
- remember that integer / rounds off the decimal part
- 11/3 =3D ?
|
|
4
|
- The easiest example of an expression in the assignment statement.
Example:
- x=3D4 ;
- This assignment will save value 4 in variable x
- y=3Dx*2+10;
- The expression is evaluated, the result is saved in variable y
|
|
5
|
- The arithmetic's operators are five:
- multiply =
(*),
divide (/), addition(+), subtraction (-), modulus(%).
- Example:<=
/li>
- count=3Dc=
ount+1;
-
force=3Dmass*acceleration;
|
|
6
|
- C has some operators which allow abbreviation of arithmetic assignme=
nt
statements. These operators are: increment (++), decrement (--).
- i++; or ++i; is equivalent to i=3Di+1;
- i--; or --i; is equivalent to i=3Di-1;
- They can be combined w=
ith
other expression. Example:
-  =
; &n=
bsp;  =
; &n=
bsp;
i=3Di-1;
- x=3D --i*(a+b); =
is
equivalent to
-  =
; &n=
bsp;  =
; &n=
bsp;
x=3Di*(a+b);
|
|
7
|
- Another abbreviation of arithmetic assignment statements and shorthand notation are:
- i +=3D 10; is equivalent to i =3D i + 10;
- i -=3D 10; is
equivalent to i =
=3D i - 10;
- i *=3D 10; is equivalent to i =3D i * 10;
- i /=3D 10; is equivalent to i =3D i / 10;
|
|
8
|
- The relational operators are used to compare two numeric values and
produce a logical result.
- Comparison operators are: equal to (=3D=3D) , not equal (!=3D), less=
than
(<), less than or equal to (<=3D), greater than (>), greate=
r than
or equal to (>=3D).
- Example:<=
/li>
-  =
;
i <=3D100;
-  =
;
a+b !=3D c;
-
x=3D=3Dy ; =
/* note that =3D=3D is used for comparison,
-  =
; &n=
bsp;
rather than =3D assignment */
|
|
9
|
- Logical connectors are frequently used to combine relational operato=
rs.
These are and (&&), or (||), and, not (!) operators.
- Example:<=
/li>
-  =
;
if (!found)
- printf(“Not found! /n”);
-  =
;
x<100 && x>=3D10;
|
|
10
|
- There is an established order with the priority of each operator, and
not only the arithmetic ones (those whose preference come from
mathematics) but for all the operators which can appear in C. From
greatest to lowest priority, the priority order is as follows:
- Level Operator Description Grouping
- 1 :: scope Left-to-right
2 () [] . -> ++ -- dynamic_cast static_cast reinterpret_ca=
st
const_cast typeid postfix Left-to-right
3 ++ -- ~ ! sizeof new delete unary (prefix) Right-to-left
* & indirection and reference (pointers)
+ - unary sign operator
4 (type) type casting Right-to-left
5 .* ->* pointer-to-member Left-to-right
6 * / % multiplicative Left-to-right
7 + - additive Left-to-right
8 << >> shift Left-to-right
9 < > <=3D >=3D relational Left-to-right
10 =3D=3D !=3D equality Left-to-right
11 & bitwise AND Left-to-right
12 ^ bitwise XOR Left-to-right
13 | bitwise OR Left-to-right
14 && logical AND Left-to-right
15 || logical OR Left-to-right
16 ?: conditional Right-to-left
17 =3D *=3D /=3D %=3D +=3D -=3D >>=3D <<=3D &=
=3D ^=3D !=3D assignment Right-to-left
18 , comma Left-to-right
- Grouping defines the precedence order in which operators are evaluat=
ed
in the case that there are several operators of the same level in an
expression.
|
|
11
|
- Remember to follow the precedence table.
- What is the value of this expression:
|
|
12
|
- Remember to follow the precedence table.
- What is the value of this expression:
|
|
13
|
- Remember to follow the precedence table.
- What is the value of this expression:
|
|
14
|
- Remember to follow the precedence table.
- What is the value of this expression:
|
|
15
|
- Remember to follow the precedence table.
- What is the value of this expression:
|
|
16
|
- We do stuff like this a lot:
- There’s a built-in operator for it
- x++ increments by one
- y-- decrements by one
- Post vs Pre
|
|
17
|
- Combine an assignment and an operation:
- +=3D, -+, *=3D, /=3D %=3D
- eg, x +=3D 5 is the same as x =3D x+ 5;
|
|
18
|
- As we know, all variables must have a type.
- As it turns out, so must all expressions.
- We don’t declare types for expressions so how does the compiler
figure it out?
|
|
19
|
- 1. If one or more are doubles then type is double.
- 2. If all are int then the type is int.
|
|
20
|
|
|
21
|
- The book says only use () to change the order of operation.
- Use simple expressions when possible
- Don’t use ++ or -- in the middle of an expression.
- Break long expressions into smaller ones with temporary values.
|
|
22
|
- We’ve seen int, double, char (mostly).
- Integers can be “signed” or “unsigned”,
“short” or “long”.
- Floating-point (float) can also be double or long double.
|
|
23
|
- Errors
- Division by zero
- overflow
- underflow
- Inaccuracies
- integer division
- representation problems
|
|
24
|
- You can also think of this as “rollover”.
- We only have so many bits to hold an answer. Once we try to compute an a=
nswer
too big the number just rolls over.
- No error message is generated, just wrong answers.
|
|
25
|
- A result that is too small to store.
- The computer simply uses 0
|
|
26
|
- Representation (round-off error)
- Cancellation:
- using both large and small numbers in the same operation
|