0% found this document useful (0 votes)
14 views

Lecture - 3.2 - Operators and Control Statements in Java

The document discusses the basic operators in Java including arithmetic, relational, bitwise, boolean logical, and conditional operators. It provides examples and explanations of each operator type.

Uploaded by

d.vargas
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Lecture - 3.2 - Operators and Control Statements in Java

The document discusses the basic operators in Java including arithmetic, relational, bitwise, boolean logical, and conditional operators. It provides examples and explanations of each operator type.

Uploaded by

d.vargas
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Lecture – 3.

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

• There are many assignment operators, including the following:

Operator Example Equivalent To

+= 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

• The outcome of these operations is a boolean value.


• = = , != can be applied to any type in java.
• Only numeric types are compared using ordering operator.
3. Bitwise Operators
~ Bitwise unary NOT
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
>> Shift Right
>>> Shift Right zero fill
<< Shift left
&= Bitwise AND Assignment
|= Bitwise OR Assignment
^= Bitwise XOR Assignment
>>= Shift Right Assignment
>>>= Shift Right zero fill Assignment
<<= Shift Left Assignment
Bitwise Operator

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 a|b a^b !a

true true true True false false

true false false true true false

false true false true ture true

false false false false false true


5. Conditional Operator ( ? : )

(expression) ? value if true : value if false

(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

• Check whether a number is even or odd.


• Check whether a number is positive or negative.
• Check whether a year is LEAP YEAR or Not.
• Find out the maximum value of three numbers.
• Make a simple calculator.
Thank you!

19

You might also like