COMP6598
Introduction to Programming
Elementary Programming (Operators)
Week 3
Session 4
Acknowledgement
These slides have been adapted from
Introduction to Java Programming. 9ed. D.
Liang. 2013. Chapter 2
Learning Outcomes
• LO 1: Design and apply the right algorithms
to solve problem
Lecture Outline :
• Assignment Operators
• Increment and Decrement Operators
• Logical Operators
• Bitwise Operators
• Relational Operators
• Math Operations
• Operator precedence
• Exercise
ASSIGNMENT OPERATOR
Assignment Operator
• The assignment operator, = , enables the program to give a value to a
variable. Operator = is called a binary operator because it has two operands.
• An assignment statement uses an assignment operator to assign a value to a
variable.
• In an assignment statement, the data type of the variable on the left must be
compatible with the data type of the value on the right.
• Syntax :
variable = expression;
• Example
int y = 1; // Assign 1 to variable y
double radius = 1.0; // Assign 1.0 to variable radius
int x = 5 * (3 / 2); // Assign the value of the expression to x
area = radius * radius * 3.14159; // Compute area
Augmented assignment
operator
• The operators +, -, *, /, and % can be combined with the
assignment operator to form augmented operators.
Operator Name Example Equivalent
+= Addition assignment i += 2 i=I+2
-= Subtraction assignment i -=2 i = i -2
*= Multiplication assignment i *= 2 i=i*2
/= Division assignment i /= 2 i=i/2
%= Remainder assignment i %= 2 i=i%2
Example
INCREMENT AND DECREMENT
Increment and
Decrement
• The increment (++) and decrement (– –)
operators are for incrementing and decrementing
a variable by 1.
• if the operator ++ and -- are placed after the
variable , these operators are known as
postincrement and postdecrement (i++, i --)
• if the operator ++ and -- are placed before the
variable , these operators are known as
preincrement and predecrement (++i, --i)
Increment and
Decrement
Operator Name Description Example (assume
i=1)
++var Preincrement Increment var by 1, and int j = ++i;
use the new var value in //j is 2, i is 2
the statement
var++ Postincrement Increment var by 1, but int j = i++;
use the original //j is 1, i is 2
--var Predecrement Decrement var by 1, and int j = --i;
use the new new value in //j is 0, i is 0
the statement
var--; Postdecrement Decrement var by 1, and int j = i--;
use the original var value //j is 1, i is 0
in the statement
Example
LOGICAL OPERATORS
Logical Operators
• The logical operators !, &&, ||, and ^ can be used to create a
compound Boolean expression.
• The Logical operators are used to combine more than one
conditions
Operator Name Description
! not Logical negation
&& and Logical conjunction
|| or Logical disjunction
^ exclusive or Logical exclusion
Truth Table
P1 P2 !P1 P1 && P2 P1 || P2 P1 ^ P2
False False True False False False
False True True False True True
True False False False True True
True True False True True False
Example
BITWISE OPERATOR
BitWise Operator
• Bitwise Operators and shift operators are
used to deal with binary numbers at the
machine level programs.
• The bit operators apply only to integer types
(byte, short, int, and long). A character
involved in a bit operation is converted to an
integer.
Bitwise Operator
Operator Name Example Description
(using bytes in the example)
& Bitwise AND 10101110 & 10010010 yields The AND of two corresponding bits yields a 1 if
10000010 both bits are 1
| Bitwise inclusive OR 10101110 | 10010010 yields The OR of two corresponding bits yields a 1 if
10111110 either bit is 1
^ Bitwise exlusive OR 10101110 10010010 yields The XOR of two corresponding yields as 1 only if
00111100 two bits are different
~ One’s complement ~10101110 yields 01010001 The operator toggles each bit from 0 to 1 and
from 1 to 0
<< Left shift 10101110 << 2 yields 10111000 The operator shifts bits in the first operand left
by the number of bits specified in the second
operand, filing with Os on the right
>> Right shift with sign 10101110 >> 2 yields 11101011 The operator shifts bit in the first operand right
extension 00101110 >> 2 yields 00001011 by the number of bits specified in the second
operand, filling with the highest (sign) bit on the
left
>>> Unsigned right shift 10101110 >>> 2 yields 00101011 The operator shifts bit in the first operand right
with zero extension 00101110 >>> 2 yields 00001011 by the number of bits specified in the second
operand, filling with Os on the left
Example
RELATIONAL / COMPARISON
OPERATIONS
Relational Operator
• The Relational operators determine if one operand is greater than, less
than, equal to, or not equal to another operand.
• Relational Operator can also be combined with Logical Operator
Java Mathmatics Name Example (radius is 5) Result
Operator Symbol
< < Less than radius < 0 false
<= ≤ Less than or equal to radius <= 0 false
> > Greater than radius > 0 true
>= ≥ Greater than or equal radius >=0 true
to
== = Equal to radius == 0 false
!= ≠ Not equal to radius != 0 true
Example
MATH OPERATIONS
Math Class
• The Math class contains the methods needed
to perform basic mathematical functions.
• The Math class can be categorized as
– trigonometric methods : sin, cos, tan, asin, acon,
atan, toDegrees, toRadians
– exponent methods : exp, log, log10, pow, sqrt
– service methods : rounding, min, max, absolute,
and random methods
Example - Trigonometri
Example Exponent
Example service method
OPERATOR PRECEDENCE
Operator Precedence
Precedence Operator
var++ and var– (Postfix)
+, - (Unary plus and minus). ++var and –var (Prefix)
(type) (Casting)
! (Not)
*, / . % (Multiplication, division and remainder)
+ , - (Binary addition and substraction)
<. <= , >, >= (Comparison)
==, != (equality)
^(exclusive OR)
&& (AND)
|| (OR)
=, +=, -=, *=, /=, %= (assignment Operator)
Exercise
• Which data types that requires the largest
amount of memory ?
• If today is Monday, what will be the day in
the next 50 days ?
• Write an expression that obtains a random
integer between 20 and 100
REFERENCES
References
• Daniel Liang, Y., 2013, Introduction to java programming,
vol.09, Pearson Education, New Jersey. Chapter 3Wrapper
Class
http://en.wikipedia.org/wiki/Primitive_wrapper_class
http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.
ii.doc/ad/rwrjwrap.htm
http://java.sun.com/j2se/1.3/docs/api/java/lang/Math.html
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html
Thank You