Operator & Control Statements
Operator & Control Statements
Operator in Java is a symbol that is used to perform operations. For example: +, -, *, / etc.
There are many types of operators in Java which are given below:
o Unary Operator,
o Arithmetic Operator,
o Shift Operator,
o Relational Operator,
o Bitwise Operator,
o Logical Operator,
o Ternary Operator and
o Assignment Operator.
Java Operator Precedence
Page 1
Java Unary Operator Java AND Operator Example: Logical && vs Bitwise &
The Java unary operators require only one operand. Unary operators are used to perform public class OperatorExample{
various operations i.e.: public static void main(String args[]){
o incrementing/decrementing a value by one int a=10;
int b=5;
o negating an expression int c=20;
o inverting the value of a boolean System.out.println(a<b&&a++<c);//false && true = false
System.out.println(a);//10 because second condition is not checked
Java Unary Operator Example: ++ and -- System.out.println(a<b&a++<c);//false && true = false
public class OperatorExample{ System.out.println(a);//11 because second condition is checked
public static void main(String args[]){ }}
int x=10;
System.out.println(x++);//10 (11) Output:
System.out.println(++x);//12
System.out.println(x--);//12 (11) false
System.out.println(--x);//10 10
}} false
11
Output: Java OR Operator Example: Logical || and Bitwise |
The logical || operator doesn't check the second condition if the first condition is true.
10 It checks the second condition only if the first one is false.
12 The bitwise | operator always checks both conditions whether first condition is true or
12 false.
10
public class OperatorExample{
Java Unary Operator Example 2: ++ and -- public static void main(String args[]){
public class OperatorExample{ int a=10;
public static void main(String args[]){ int b=5;
int a=10; int c=20;
int b=10; System.out.println(a>b||a<c);//true || true = true
System.out.println(a++ + ++a);//10+12=22 System.out.println(a>b|a<c);//true | true = true
System.out.println(b++ + b++);//10+11=21 //|| vs |
System.out.println(a>b||a++<c);//true || true = true
}} System.out.println(a);//10 because second condition is not checked
System.out.println(a>b|a++<c);//true | true = true
Output: System.out.println(a);//11 because second condition is checked
22
21 }}
Output:
Java Unary Operator Example: ~ and !
public class OperatorExample{ true
public static void main(String args[]){ true
int a=10; true
int b=-10; 10
boolean c=true; true
boolean d=false; 11
System.out.println(~a);//-11 (minus of total positive value which starts from 0)
System.out.println(~b);//9 (positive of total minus, positive starts from 0) Java Ternary Operator
System.out.println(!c);//false (opposite of boolean value) Java Ternary operator is used as one line replacement for if-then-else statement and
System.out.println(!d);//true used a lot in Java programming. It is the only conditional operator which takes three
}} operands.
Page 2
public static void main(String args[]){ int b=20;
System.out.println(10*10/5+3-1*4/2); a+=4;//a=a+4 (a=10+4)
}} b-=4;//b=b-4 (b=20-4)
System.out.println(a);
Output: System.out.println(b);
}}
21
Output:
Java Left Shift Operator
The Java left shift operator << is used to shift all of the bits in a value to the left side of a 14
specified number of times. 16
Java Assignment Operator Example
Java Left Shift Operator Example
public class OperatorExample{
public class OperatorExample{
public static void main(String[] args){
public static void main(String args[]){
int a=10;
System.out.println(10<<2);//10*2^2=10*4=40
a+=3;//10+3
System.out.println(10<<3);//10*2^3=10*8=80
System.out.println(a);
System.out.println(20<<2);//20*2^2=20*4=80
a-=4;//13-4
System.out.println(15<<4);//15*2^4=15*16=240
System.out.println(a);
}}
a*=2;//9*2
Output:
System.out.println(a);
40
a/=2;//18/2
80
System.out.println(a);
80
}}
240
Output:
Java Right Shift Operator
The Java right shift operator >> is used to move the value of the left operand to right by 13
the number of bits specified by the right operand. 9
18
Java Right Shift Operator Example
9
public OperatorExample{
Java Assignment Operator Example: Adding short
public static void main(String args[]){
public class OperatorExample{
System.out.println(10>>2);//10/2^2=10/4=2
public static void main(String args[]){
System.out.println(20>>2);//20/2^2=20/4=5
short a=10;
System.out.println(20>>3);//20/2^3=20/8=2
short b=10;
}}
//a+=b;//a=a+b internally so fine
Output: a=a+b;//Compile time error because 10+10=20 now int
System.out.println(a);
2 }}
5
2 Output:
Java Shift Operator Example: >> vs >>>
Compile time error
public class OperatorExample{
After type cast:
public static void main(String args[]){
public class OperatorExample{
//For positive number, >> and >>> works same
public static void main(String args[]){
System.out.println(20>>2);
short a=10;
System.out.println(20>>>2);
short b=10;
//For negative number, >>> changes parity bit (MSB) to 0
a=(short)(a+b);//20 which is int now converted to short
System.out.println(-20>>2);
System.out.println(a);
System.out.println(-20>>>2);
}}
}}
Output:
Output:
20
5
5
-5
1073741819
Output:
false
false
Page 3
Java Control Statements | Control Flow in Java
Java compiler executes the code from top to bottom. The statements in the code are executed according to
the order in which they appear. However, Java provides statements that can be used to control the flow of
Java code. Such statements are called control flow statements. It is one of the fundamental features of
Java, which provides a smooth flow of program.
Page 4
Decision-Making statements: Java for-each loop
As the name suggests, decision-making statements decide which statement to execute and Java provides an enhanced for loop to traverse the data structures like array or
when. Decision-making statements evaluate the Boolean expression and control the collection. In the for-each loop, we don't need to update the loop variable. The syntax
program flow depending upon the result of the condition provided. There are two types of to use the for-each loop in java is given below.
decision-making statements in Java, i.e., If statement and switch statement. for(data_type var : array_name/collection_name){
//statements
1) If Statement: }
In Java, the "if" statement is used to evaluate a condition. The control of the program is Consider the following example to understand the functioning of the for-each loop in
diverted depending upon the specific condition. The condition of the If statement gives a Java.
Boolean value, either true or false. In Java, there are four types of if-statements given Calculation.java
below. public class Calculation {
public static void main(String[] args) {
1. Simple if statement
// TODO Auto-generated method stub
2. if-else statement
String[] names = {"Java","C","C++","Python","JavaScript"};
3. if-else-if ladder
System.out.println("Printing the content of the array names:\n");
4. Nested if-statement
for(String name:names) {
Let's understand the if-statements one by one.
System.out.println(name);
1) Simple if statement: }
It is the most basic statement among all control flow statements in Java. It evaluates a }
Boolean expression and enables the program to enter a block of code if the expression }
evaluates to true. Output:
Printing the content of the array names:
Syntax of if statement is given below.
if(condition) { Java
statement 1; //executes when condition is true C
} C++
Consider the following example in which we have used the if statement in the java code. Python
Student.java JavaScript
Syntax:
if(condition) {
statement 1; //executes when condition is true
}
else{
statement 2; //executes when condition is false
}
Consider the following example.
Student.java
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y < 10) { Consider the following example.
System.out.println("x + y is less than 10"); Calculation .java
} else { public class Calculation {
System.out.println("x + y is greater than 20"); public static void main(String[] args) {
} // TODO Auto-generated method stub
} int i = 0;
} System.out.println("Printing the list of first 10 even numbers \n");
Output: while(i<=10) {
System.out.println(i);
x + y is greater than 20 i = i + 2;
3) if-else-if ladder: }
The if-else-if statement contains the if-statement followed by multiple else-if statements. In }
other words, we can say that it is the chain of if-else statements that create a decision tree }
where the program may enter in the block of code where the condition is true. We can also Output:
define an else statement at the end of the chain. Printing the list of first 10 even numbers
Syntax of if-else-if statement is given below.
0
if(condition 1) { 2
statement 1; //executes when condition 1 is true 4
} 6
8
Page 5
else if(condition 2) { 10
statement 2; //executes when condition 2 is true
} Java do-while loop
else { The do-while loop checks the condition at the end of the loop after executing the loop
statement 2; //executes when all the conditions are false statements. When the number of iteration is not known and we have to execute the
} loop at least once, we can use do-while loop.
Consider the following example. It is also known as the exit-controlled loop since the condition is not checked in
Student.java advance. The syntax of the do-while loop is given below.
public class Student {
public static void main(String[] args) { do
String city = "Delhi"; {
if(city == "Meerut") { //statements
System.out.println("city is meerut"); } while (condition);
}else if (city == "Noida") { The flow chart of the do-while loop is given in the following image.
System.out.println("city is noida");
}else if(city == "Agra") {
System.out.println("city is agra");
}else {
System.out.println(city);
}
}
}
Output:
Delhi
4. Nested if-statement
In nested if-statements, the if statement can contain a if or if-else statement inside another if
or else-if statement.
if(j == 4) {
continue;
}
System.out.println(j);
Consider the following example to understand the proper functioning of the for loop in java.
}
Calculation.java }
public class Calculattion { }
public static void main(String[] args) {
// TODO Auto-generated method stub }
int sum = 0; Output:
0
for(int j = 1; j<=10; j++) { 1
sum = sum + j; 2
} 3
5
System.out.println("The sum of first 10 natural numbers is " + sum); 1
} 2
3
} 5
Output: 2
The sum of first 10 natural numbers is 55 3
5
Page 7