Engineering Science 1036A/B Lecture Notes - Lecture 7: Increment And Decrement Operators, Operand
Operators take one or more input values and produce one output value
•
Operators come in different types:
Unary operators - take 1 operand (value)
○
Binary operators - take 2 operands (values)
○
Ternary operators - take 3 operands (values)
○
•
•
Arithmetic Operators
Operator
Meaning
!"#
Addition
!$#
Subtraction
!%#
Multiplication
!&#
Division
!'#
Modulus
Integer division: ((
)gives *
•
Floating point division: ((+,
)gives *+-.
•
Modulo Operator: % (only for integers)
Outputs the remainder after integer division
11&4 gives 3
§
10&5 gives 0
§
5&10 gives 5
§
○
If an operand in modulo operations is not an integer, compilation error
will occur
○
•
Comparison Operators
Logical Operators
Called "short-circuit" operators•
Truth Tables
Logical Operator in expressions
Unary Operators
int !/90,#;
Unary Expression Symbol Example
Negate −$!1 2 1 $3
Logical invert !4 ! 2 5
Increment + + " "! 2 65
Decrement − − $ $! 2 7
Pre-Increment b = ++a; b=65 a=65
Post-Increment b = a++; b=3 a=65
Pre-Decrement b = --a; b=7 a=7
Post-Decrement b = a--;b=3 a=7
Copy (Assignment) Operator
Simple copy: a = b
Overwrites the left object with the result of expression on the right value
○
Left value must be writable (not a const)
○
•
Copy with arithmetic operator
a += b; is the same as a = a + b;
○
a -= b; is the same as a = a -b;
○
a *= b; is the same as a = a * b;
○
a /= b; is the same as a = a / b;
○
•
Operator Precedence
Unary (pre-increment/decrement)
Left to right
○
1.
Unary (!, -)
Left to right
○
2.
Arithmetic (+,-, *,/, %)
Left to right
○
BEDMAS
○
3.
Comparison (<, <=, >, >=,==, !=)
Left to right
○
4.
Logical binary (&&, ||)
Left to right, if no brackets are used
○
5.
Assignment (=, *=, /=, %=)
Right to left
○
6.
Unary (pre-increment/decrement)
Left to right
○
7.
Operator: sizeof
sizeof() can be used to find the size of any variable/object in terms of bytes•
Unary operator•
Operators
Operators take one or more input values and produce one output value•
Operators come in different types:
Unary operators - take 1 operand (value)
○
Binary operators - take 2 operands (values)
○
Ternary operators - take 3 operands (values)
○
•
•
Arithmetic Operators
Operator Meaning
!"#Addition
!$#Subtraction
!%#Multiplication
!&#Division
!'#Modulus
Integer division: ((
)gives *
•
Floating point division: ((+,
)gives *+-.
•
Modulo Operator: % (only for integers)
Outputs the remainder after integer division
11&4 gives 3
§
10&5 gives 0
§
5&10 gives 5
§
○
If an operand in modulo operations is not an integer, compilation error
will occur
○
•
Comparison Operators
Logical Operators
Called "short-circuit" operators•
Truth Tables
Logical Operator in expressions
Unary Operators
int !/90,#;
Unary Expression Symbol Example
Negate −$!1 2 1 $3
Logical invert !4 ! 2 5
Increment + + " "! 2 65
Decrement − − $ $! 2 7
Pre-Increment b = ++a; b=65 a=65
Post-Increment b = a++; b=3 a=65
Pre-Decrement b = --a; b=7 a=7
Post-Decrement b = a--;b=3 a=7
Copy (Assignment) Operator
Simple copy: a = b
Overwrites the left object with the result of expression on the right value
○
Left value must be writable (not a const)
○
•
Copy with arithmetic operator
a += b; is the same as a = a + b;
○
a -= b; is the same as a = a -b;
○
a *= b; is the same as a = a * b;
○
a /= b; is the same as a = a / b;
○
•
Operator Precedence
Unary (pre-increment/decrement)
Left to right
○
1.
Unary (!, -)
Left to right
○
2.
Arithmetic (+,-, *,/, %)
Left to right
○
BEDMAS
○
3.
Comparison (<, <=, >, >=,==, !=)
Left to right
○
4.
Logical binary (&&, ||)
Left to right, if no brackets are used
○
5.
Assignment (=, *=, /=, %=)
Right to left
○
6.
Unary (pre-increment/decrement)
Left to right
○
7.
Operator: sizeof
sizeof() can be used to find the size of any variable/object in terms of bytes•
Unary operator•
Operators
Operators take one or more input values and produce one output value•
Operators come in different types:
Unary operators - take 1 operand (value)
○
Binary operators - take 2 operands (values)
○
Ternary operators - take 3 operands (values)
○
•
•
Arithmetic Operators
Operator Meaning
!"#Addition
!$#Subtraction
!%#Multiplication
!&#Division
!'#Modulus
Integer division:
((
)
gives
*
•
Floating point division:
((+,
)
gives
*+-.
•
Modulo Operator: % (only for integers)
Outputs the remainder after integer division
11&4
gives 3
§
10&5
gives 0
§
5&10
gives 5
§
○
If an operand in modulo operations is not an integer, compilation error
will occur
○
•
Comparison Operators
Logical Operators
Called "short-circuit" operators•
Truth Tables
Logical Operator in expressions
Unary Operators
int !/90,#;
Unary Expression Symbol Example
Negate −$!1 2 1 $3
Logical invert !4 ! 2 5
Increment + + " "! 2 65
Decrement − − $ $! 2 7
Pre-Increment b = ++a; b=65 a=65
Post-Increment b = a++; b=3 a=65
Pre-Decrement b = --a; b=7 a=7
Post-Decrement b = a--;b=3 a=7
Copy (Assignment) Operator
Simple copy: a = b
Overwrites the left object with the result of expression on the right value
○
Left value must be writable (not a const)
○
•
Copy with arithmetic operator
a += b; is the same as a = a + b;
○
a -= b; is the same as a = a -b;
○
a *= b; is the same as a = a * b;
○
a /= b; is the same as a = a / b;
○
•
Operator Precedence
Unary (pre-increment/decrement)
Left to right
○
1.
Unary (!, -)
Left to right
○
2.
Arithmetic (+,-, *,/, %)
Left to right
○
BEDMAS
○
3.
Comparison (<, <=, >, >=,==, !=)
Left to right
○
4.
Logical binary (&&, ||)
Left to right, if no brackets are used
○
5.
Assignment (=, *=, /=, %=)
Right to left
○
6.
Unary (pre-increment/decrement)
Left to right
○
7.
Operator: sizeof
sizeof() can be used to find the size of any variable/object in terms of bytes•
Unary operator•
Operators