Lecture - 3.2 - Operators and Control Statements in Java
Lecture - 3.2 - Operators and Control Statements in Java
JAVA: Operators
1
Contents
• Basic Operators in JAVA
2
Basic Operators in Java
1. Arithmetic Operators
2. Relational Operators
3. Bitwise Operators
4. Boolean Logical Operators
5. Conditional Operator ( ? : )
1. Arithmetic Operators
• Addition +
• Subtraction -
• Multiplication *
• Division /
• Remainder %
• Increment ++
• Addition Assignment +=
• Subtraction Assignment -=
• Multiplication Assignment *=
• Division Assignment /=
• Modulus Assignment %=
• Decrement --
**Arithmetic Assignment Operators
+= x += y x=x+y
-= x -= y x=x-y
*= x *= y x=x*y
/= x /= y x=x/y
%= x %= y x=x%y
**Increment/decrement Operators
• Preincrement:
• i = ++n;
• Predecrement:
• i = --n;
• Postincrement
• i = n++;
• Postdecrement
• i = n--;
2. Relational Operators
• > greater than
• >= greater than or equal to
• < less than
• <= less than or equal to
• == equal to
• != not equal to
A B A|B A&B A^ B ~A
0 0 0 0 0 1
0 1 1 0 1 1
1 0 1 0 1 0
1 1 1 1 0 0
4. Boolean Logical Operators
& Logical AND
| Logical OR
^ Logical XOR
|| Short-circuit OR
&& Short-circuit AND
! Logical unary NOT
&= AND Assignment
|= OR Assignment
^= XOR Assignment
== Equal to
!= Not equal to
?: Ternary if-then-else
Boolean Logical Operator
✔ The logical boolean operators &, | and ^ operates in the same
way that they operate on the bits of integer.
(a > b) ? a : b;
it is an expression which returns one of two values, a or b. The condition, (a > b), is tested. If it is
true the first value, a, is returned. If it is false, the second value, b, is returned.
Conditional Operator is equivalent to if ..else
if (a > b) {
max = a;
}
else { max = (a > b) ? a : b;
max = b;
}
Operator Precedence
Java Control Statements
Java Control Statements
• Selection Statements
⮚ if
⮚ switch
• Iteration Statements
⮚ For loop
⮚ While loop
⮚ do-while loop
• Jump Statements
⮚ Break
⮚ Continue
Exercise
19