OCS1902
OOPS USING
 JAVA
 SESSION 2
 Operators and
 Branching
 Statements
01
CONTENTS
 Operators
 Branching
 Statements
 2
Operators
 3
 operator environment.
 Most of its operators can
 be divided into the
 following four groups
● Arithmetic
● Bitwise
● Relational
● Logical
● Java also defines some
 additional operators that
 handle certain special
 situations. 4
Arithmetic
Operators
 5
 Arithmetic
 Operators
The unary minus operator negates its
single operand.
The unary plus operator simply
returns the value of its operand.
Remember that when the division
operator is applied to an integer
type, there will be no fractional
component attached to the result.
 6
 Arithmetic Operators-
 Modulus class Modulus {
 public static void main(String
 args[]) {
The modulus operator, int x = 42;
%, returns the double y = 42.25;
remainder of a division System.out.println("x mod 10 = "
operation. + x % 10);
It can be applied to System.out.println("y mod 10 = "
 + y % 10);
floating-point types as
 }
well as integer types. }
 When you run this program, you will
 get the following output:
 x mod 10 = 2 7
 Arithmetic Compound Assignment
 Operators
Java provides special operators that can be
used to combine an arithmetic operation
with an assignment.
 a = a + 4;
In Java, you can rewrite this statement as
shown here,
 a += 4;
This version uses the += compound
assignment operator.
Both statements perform the same action
 they increase the value of a by 8
 Arithmetic Compound Assignment
 Operators
class OpEquals {
 public static void
main(String args[]){
 int a = 1; int b = 2; int
c = 3; a=6
 a += 5;
 b *= 4; b=8
 c += a * b;
 c %= 6;
 c=5
 System.out.println("a =
" + a);
 System.out.println("b =
" + b);
 System.out.println("c = "
+ c);
 } 9
Arithmetic Operators-Unary
operators
 10
Unary Operators-Program
 class Sample 11
 {
 19
 public static void main(String args[])
 {
 int a = 10;
 int b = 20;
 System.out.println("++a = " +
 (++a) );
 System.out.println("--b = " + (--
 b) );
 }
 }
 11
Unary Operators-Program
 What will be the result, if we try to
 compile and execute the following code?
 class Test
 {
 public static void main(String [ ] 16
 args)
 {
 17
 int x=10;
 int y=5;
 System.out.println(x+
 ++(++y));
 System.out.println(x+y);
 } 12
 RelationalOperat
• Relationalors
 operators are used to compare
 two values.
• These operators are primarily used in
 conditions that control the flow of the
 program, such as in if statements or loops.
 13
RelationalOperat
ors
 14
 RelationalOperat
Relational orsare most commonly used with
 operators primitive
data types like int, float, double, and char.
However, when comparing objects, especially strings, the
equals() method or compareTo() method is often more
appropriate, as the == operator checks for reference equality
rather than content equality in the case of objects.
 Example:
 String a = "hello";
 String b = "hello";
 a == b checks if a and b refer to the same
 object.
 a.equals(b) checks if the content of a and b
 is the same. 15
 Logical
Logical
 Operators
 operators in Java are used to combine
multiple boolean expressions or values and form
compound boolean expressions.
They play a crucial role in decision-making
structures like if-else statements and loops.
 16
Logical
Operators
 17
 Logical
 Operators
Java also employs short-circuit evaluation with
logical operators:
In the case of && (AND), if the first operand is
false, Java does not evaluate the second operand,
because the overall expression cannot be true.
Similarly, for || (OR), if the first operand is true,
Java does not evaluate the second operand, as the
overall expression is already true.
 18
 Bitwise
 Operators
Bitwise operators in Java perform
operations on the bits of integer
types (int, long, byte, short).
They are primarily used in low-
level programming, such as
graphics, device drivers, and
algorithms that require
manipulation of data at the bit
level. 19
Bitwise
Operators
 20
Bitwise(&, |, ^)
 21
 Shift Operators(>>)
When we represent 16 in binary form, we
will get the following binary valu :
When we apply >> which is the right
shift operator, the bit represented by 1
moves by 3 positions to the
right(represented by the number after
the right shift operator).
After shifting the binary digit 1, we will
get : 22
 Shift Operators(>>)
When we represent 8 in binary form, we will
get the following binary value :
When we apply << which is the left shift
operator, the bit represented by 1 moves by
4 positions to the left (represented by the
number after the left shift operator).
After shifting the binary digit 1, we will get :
x = 128
 23
Branching
Statements
 24
Branching Statements
 0 0
 1 if 3 if-else-if
 0 0 ladder
 2 if else 1 Switch case
 25
if statement
 26
if statement
 27
if else statement
 28
if else statement
 29
if else if ladder
 30
if else if ladder
 31
 Switch statement
• In Java, the switch statement allows you to
 select one of many code blocks to be executed.
• It's a more elegant and readable alternative to
 multiple if-else if-else statements when dealing
 with multiple conditions based on a single
 expression.
• The expression within the switch must be of a
 compatible type, such as int, char, String, or an
 enumeration.
 32
Switch statement
 33
Switch statement-points
 expression: This is the value that is compared with
 the values of the case labels.
 case: Each case label specifies a value to compare
 with the expression. If the expression matches a case
 value, the code following that case is executed.
 break: A break statement is used to exit the switch
 block. If it's omitted, execution will continue into the
 next case, which is often undesirable (this is known as
 "fall-through").
 default: The default case is optional and can appear
 anywhere in the switch block. The code in the default
 case is executed if none of the case values match the 34
 Switch statement
int day = 4; case 5:
String dayString; dayString = "Friday";
 break;
switch (day) { case 6:
 case 1: dayString = "Saturday";
 dayString = "Monday"; break;
 break; case 7:
 case 2: dayString = "Sunday";
 dayString = "Tuesday"; break;
 break; default:
 case 3: dayString = "Invalid day";
 dayString = "Wednesday"; break;
 break; }
 case 4:
 dayString = "Thursday"; System.out.println("Day of the
 break; week: " + dayString);
 35
 Ternary operator ?:
The ternary operator in Java is a concise way to
perform conditional assignments. It's a one-liner
replacement for an if-else statement and is often
used for simple conditions.
 Here's how it works:
 condition: This is a boolean expression that evaluates
 either true or false.
 valueIfTrue: This value is assigned to result if the condit
 36
 is true.
Ternary operator ?:
 37
Thank
 you!
 38