0% found this document useful (0 votes)
27 views7 pages

Operator & Control Statements

C programming

Uploaded by

pubcse24th
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views7 pages

Operator & Control Statements

C programming

Uploaded by

pubcse24th
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Operators in Java

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

Operator Type Category Precedence


postfix expr++ expr--
Unary
prefix ++expr --expr +expr -expr ~ !
multiplicative */%
Arithmetic
additive +-
Shift shift << >> >>>
comparison < > <= >= instanceof
Relational
equality == !=
bitwise AND &
Bitwise bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
Logical
logical OR ||
Ternary ternary ?:
Assignment assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=

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.

Output: Java Ternary Operator Example


public class OperatorExample{
-11 public static void main(String args[]){
9 int a=2;
false int b=5;
true int min=(a<b)?a:b;
Java Arithmetic Operators System.out.println(min);
Java arithmetic operators are used to perform addition, subtraction, multiplication, and }}
division. They act as basic mathematical operations.
Output:
Java Arithmetic Operator Example
public class OperatorExample{ 2
public static void main(String args[]){ Another Example:
int a=10; public class OperatorExample{
int b=5; public static void main(String args[]){
System.out.println(a+b);//15 int a=10;
System.out.println(a-b);//5 int b=5;
System.out.println(a*b);//50 int min=(a<b)?a:b;
System.out.println(a/b);//2 System.out.println(min);
System.out.println(a%b);//0 }}
}}
Output:
Output:
5
15 Java Assignment Operator
5 Java assignment operator is one of the most common operators. It is used to assign the
50 value on its right to the operand on its left.
2 Java Assignment Operator Example
0 public class OperatorExample{
Java Arithmetic Operator Example: Expression public static void main(String args[]){
public class OperatorExample{ int a=10;

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

Java AND Operator Example: Logical && and Bitwise &


The logical && operator doesn't check the second condition if the first condition is false.
It checks the second condition only if the first one is true.
The bitwise & operator always checks both conditions whether first condition is true or
false.
public class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
int c=20;
System.out.println(a<b&&a<c);//false && true = false
System.out.println(a<b&a<c);//false & true = false
}}

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.

Java provides three types of control flow statements.


1. Decision Making statements
o if statements
o switch statement
2. Loop statements
o do while loop
o while loop
o for loop
o for-each loop
3. Jump statements
o break statement
o continue statement

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

Student.java Java while loop


public class Student { The while loop is also used to iterate over the number of statements multiple times.
public static void main(String[] args) { However, if we don't know the number of iterations in advance, it is recommended to
int x = 10; use a while loop. Unlike for loop, the initialization and increment/decrement doesn't
int y = 12; take place inside the loop statement in while loop.
if(x+y > 20) { It is also known as the entry-controlled loop since the condition is checked at the start
System.out.println("x + y is greater than 20"); of the loop. If the condition is true, then the loop body will be executed; otherwise, the
} statements after the loop will be executed.
} The syntax of the while loop is given below.
} while(condition){
//looping statements
Output:
}
x + y is greater than 20
The flow chart for the while loop is given in the following image.
2) if-else statement
The if-else statement is an extension to the if-statement, which uses another block of code,
i.e., else block. The else block is executed if the condition of the if-block is evaluated as
false.

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.

Syntax of Nested if-statement is given below.


if(condition 1) {
statement 1; //executes when condition 1 is true
if(condition 2) {
statement 2; //executes when condition 2 is true
}
else{
statement 2; //executes when condition 2 is false
} Consider the following example to understand the functioning of the do-while loop in
} Java.
Consider the following example.
Student.java Calculation.java
public class Student {
public static void main(String[] args) { public class Calculation {
String address = "Delhi, India"; public static void main(String[] args) {
// TODO Auto-generated method stub
if(address.endsWith("India")) { int i = 0;
if(address.contains("Meerut")) { System.out.println("Printing the list of first 10 even numbers \n");
System.out.println("Your city is Meerut"); do {
}else if(address.contains("Noida")) { System.out.println(i);
System.out.println("Your city is Noida"); i = i + 2;
}else { }while(i<=10);
System.out.println(address.split(",")[0]); }
} }
}else { Output:
System.out.println("You are not living in India"); Printing the list of first 10 even numbers
} 0
} 2
} 4
6
Output: 8
Delhi 10

Switch Statement: Jump Statements


In Java, Switch statements are similar to if-else-if statements. The switch statement Jump statements are used to transfer the control of the program to the specific
contains multiple blocks of code called cases and a single case is executed based on statements. In other words, jump statements transfer the execution control to the other
the variable which is being switched. The switch statement is easier to use instead of part of the program. There are two types of jump statements in Java, i.e., break and
if-else-if statements. It also enhances the readability of the program. continue.

Points to be noted about switch statement: Java break statement


o The case variables can be int, short, byte, char, or enumeration. String type is As the name suggests, the break statement is used to break the current flow of the
also supported since version 7 of Java program and transfer the control to the next statement outside a loop or switch
statement. However, it breaks only the inner loop in the case of the nested loop.
o Cases cannot be duplicate The break statement cannot be used independently in the Java program, i.e., it can only
o Default statement is executed when any of the case doesn't match the value of be written inside the loop or switch statement.
expression. It is optional.
o Break statement terminates the switch block when the condition is satisfied. The break statement example with for loop
It is optional, if not used, next case is executed. Consider the following example in which we have used the break statement with the
for loop.
o While using switch statements, we must notice that the case expression will be BreakExample.java
of the same type as the variable. However, it will also be a constant value.
public class BreakExample {
The syntax to use the switch statement is given below.
switch (expression){
public static void main(String[] args) {
case value1:
// TODO Auto-generated method stub
statement1;
for(int i = 0; i<= 10; i++) {
break;
Page 6
. System.out.println(i);
. if(i==6) {
. break;
case valueN: }
statementN; }
break; }
default: }
default statement; Output:
} 0
Consider the following example to understand the flow of the switch statement. 1
2
3
Student.java 4
public class Student implements Cloneable { 5
public static void main(String[] args) { 6
int num = 2; break statement example with labeled for loop
switch (num){ Calculation.java
case 0: public class Calculation {
System.out.println("number is 0");
break; public static void main(String[] args) {
case 1: // TODO Auto-generated method stub
System.out.println("number is 1"); a:
break; for(int i = 0; i<= 10; i++) {
default: b:
System.out.println(num); for(int j = 0; j<=15;j++) {
} c:
} for (int k = 0; k<=20; k++) {
} System.out.println(k);
Output: if(k==5) {
2 break a;
While using switch statements, we must notice that the case expression will be of the same }
type as the variable. However, it will also be a constant value. The switch permits only int, }
string, and Enum type variables to be used. }
Loop Statements
}
In programming, sometimes we need to execute the block of code repeatedly while some
}
condition evaluates to true. However, loop statements are used to execute the set of
instructions in a repeated order. The execution of the set of instructions depends upon a
particular condition.
}
In Java, we have three types of loops that execute similarly. However, there are differences
Output:
in their syntax and condition checking time. 0
1. for loop 1
2
2. while loop 3
4
3. do-while loop 5
Let's understand the loop statements one by one. Java continue statement
Unlike break statement, the continue statement doesn't break the loop, whereas, it
Java for loop skips the specific part of the loop and jumps to the next iteration of the loop
In Java, for loop is similar to C and C++. It enables us to initialize the loop variable, check immediately.
the condition, and increment/decrement in a single line of code. We use the for loop only
when we exactly know the number of times, we want to execute the block of code. Consider the following example to understand the functioning of the continue
for(initialization, condition, increment/decrement) { statement in Java.
//block of statements public class ContinueExample {
}
The flow chart for the for-loop is given below. public static void main(String[] args) {
// TODO Auto-generated method stub

for(int i = 0; i<= 2; i++) {

for (int j = i; j<=5; j++) {

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

You might also like