Transcript Week3-ch4a
Chapter 4
Operators
Precedence - Operators with the highest precedence will be
executed first. Page 54 of the book and Appendix B list C's operator
precedence. Parenthesis () may be used to force the precedence.
Numerical Operators - The standard operators used in math.
Addition (+)
Adds two numbers together. 10 + 8 = 18
Subtraction (-)
Subtracts second number from first number. 7 - 3 = 4
Multiplication (*)
Multiplies two numbers together. 7 * 8 = 56
Division (/) - Divides first number by second number. If both numbers are integers the
result is an integer truncated towards zero and remainder is lost. If either number is a
floating-point the result is floating-point. Integer division by zero causes an error.
Example 7/3 // evaluates to 7/3.0F // evaluates to 2.333333f 7/0 // causes an error
Modulo (%) - Returns the remainder of the first number divided by the second number.
The sign of the result stays the same as that of the first operand. This operations work
with both integer and floating-point numbers. Integer modulo by zero causes an error.
Example 7 % 3 = 1 4.3 % 2.1 = 0.1
Use when no modulus operator is available: Q = x/y R = x - (Q * y)
Example x = 5 y = 2 Q = 5/2 = 2 //quotient R = 5 - (2 * 2) = 1 //remainder
Unary Minus (-)
When used before a single number it reverses the sign of the number. Negative
become positives and positives become negatives.
Unary Plus (+)
When used before a single number - the number is positive no matter the original
sign.
Increment (++) - Increments a variable by one. This can be either a post
or pre increment operation depending on whether the operator is after or
before the operand. With post-increment the expression operand value is
evaluated and then the operand incremented. With pre-increment the
operand is incremented and then used in the expression.
Example i = 1; j = ++i; //both j and i are set to 2 after this statement
executed. i = 1; j = i++; //j equals 1 and i equals 2 after this statement
executed.
Decrement (--) - Decrements a variable by one in a similar fashion as
the increment operator.
The expressions x++ and x-- are equivalent to x=x+1 and x=x-1
respectively. Increment and decrement in both pre and post forms is
most commonly used as a counter.
Relational Operators - Comparison operators are divided into two
categories. The first is equality operators (== and !=) and the second is
relational operators (<=, >, >=). The equality operators are used with
primitive types. The relational operators can work on numbers and
character data types (not boolean). Both categories return a boolean
value, true or false.
Equals (==) - This operator returns true if both operands are equal,
otherwise it returns false.
Not Equals (!=) - This operator is the opposite of the == operator. It
returns true if the two operands are not equal, otherwise it returns false.
Less Than (<) - Evaluates to true if the first number is less than the
second number, otherwise false.
Less Than or Equal (<=) - Evaluates to true if the first number is less
than or equal to the second number, otherwise false.
Greater Than (>) - Evaluates to true if the first number is greater than
the second number, otherwise false.
Greater Than or Equal (>=) - Evaluates to true if the first number is
greater than or equal to the second number, otherwise false.
Logical Operators - These operators work on boolean operands and returns a boolean
result. These operands are used to form logical comparison operations.
Conditional AND (&&) - This operator returns true if and only if both operands are
true, otherwise it returns false. This is referred to as a conditional AND because it
always evaluates the first operand, however if it is false then the second operand is not
evaluated because it is already known that the AND operation will yield a false value.
This is done for efficiency reasons but since the second operand may be skipped be
aware that if that operand was an expression with side effects, they may not be
produced.
Conditional OR (||) - This operator returns true if either one or both operands are true,
otherwise if both operands are false then it returns a false value. This is a conditional
operator in a similar fashion as the AND operator but it skips the second operand if the
first operand is true because in that case it is known that the comparison will yield a true
value. The same cautions about second operand side effects apply.
Example x < 10 || y > 15 p > 0 && p < 10
Boolean NOT (!) - This operator applies to one boolean operand and reverses the
boolean value. If the operand is true then false is returned. If the operand is false then
true is returned. This unary NOT (!) operator has a high precedence so parentheses are
often needed to code a statement correctly.
Example ( ! ( x > y && y < z ) ) // if there were no parenthesis the ! would match
with the x, instead of the whole expression.
Additional Operator Definitions
Assignment (=)
The assignment operator should not be confused with the equal
operator (==) and visa versa. This is a common source of bugs.
+=, -=, *=, /=, %=
Each of these operators performs the operation on the left side of the
equal sign. The operands are listed on either side of the operator with
the operand on the left receiving the result of the operation.
Example
a += b; //is the same as a = a + b; x /= y; //is the same as x = x / y;
Precedence Expression Evaluation
Precedence Name
Symbols
Associativity
1
increment (postfix)
++ (if i = 1; i++; i = 1;) left
decrement (postfix)
-2
increment (prefix)
+ + (if i = 1; ++i; i = 2;) right
decrement (prefix)
-unary plus
+
unary minus
3
multiplicative
* / %
left
4
additive
+left
5
assignment
= *= /= %= += -=
right
( i += 2; ) same as ( i = i + 2; )
a = b += c++ - d + --e / -f
->
a = b += (c++) - d + --e / -f
a = b += (c++) - d + (--e) / (-f) -> a = b += (c++) - d + ((--e) / (-f))
a = b += (((c++) - d) + ((--e) / (-f))) ->
(a = (b += (((c++) - d) + ((--e) / (-f)))))