Java Programming Assignment Help - MIT USA | Java Programming Homework Help - MIT USA | Java Programming Project Help - MIT USA | Online Java Programming Help - MIT USA

In programming help students of Java Assignment Help MIT USA asked about conditional operators in java. Conditional operators determine if one operand is greater than, less than, equal to, or not equal to another operand.

Operator Description are as follows :
== equal to
!= not equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to

Conditional operators takes two operands and return true, if the condition evaluates to true, otherwise return false.

Example
class ConditionalEx{
public static void main(String args[]){
int a = 10, b=11;

System.out.println("is a equals to a " + (a==a));
System.out.println("is a equals to b " + (a==b));
System.out.println("is a not equals to a " + (a!=a));
System.out.println("is a not equals to b " + (a!=b));
System.out.println("is a less than a " + (a<a));
System.out.println("is a less than b " + (a<b));
System.out.println("is a less than or equal to a " + (a<=a));
System.out.println("is a less than or equal b " + (a<=b));
System.out.println("is a greater than a " + (a>a));
System.out.println("is a greater than b " + (a>b));
System.out.println("is a greater than or equal to a " + (a>=a));
System.out.println("is a greater than or equal to b " + (a>=b));
}
}

Output
is a equals to a true
is a equals to b false
is a not equals to a false
is a not equals to b true
is a less than a false
is a less than b true
is a less than or equal to a true
is a less than or equal b true
is a greater than a false
is a greater than b false
is a greater than or equal to a true
is a greater than or equal to b false

Some points to remember

1. You can compare one type of data with another if types are compatible.
class ConditionalEx{
public static void main(String args[]){
int a = 10;
float b = 1.09f;
char c = 'a';

System.out.println("a less than b " + (a<b));
System.out.println("a less than c " + (a<c));
}
}
Output

a less than b false
a less than c true

2. If you try to compare two incompatible type values, compiler will throw error.
class ConditionalEx{
public static void main(String args[]){
int a = 10;
boolean b = true;

System.out.println("a less than b " + (a<b));
}
}

If you try to compile the above program, you will get the below error
ConditionalEx.java:8: error: bad operand types for binary operator '<'
System.out.println("a less than b " + (a<b));
^
first type: int
second type: boolean
1 error

In programming help student also faces problem in understanding the use of logical operators. Following are explanation with example.

Java provides three logical operators, These are &&(AND), || (OR), and !(NOT)

Logical AND is also called Conditional AND

Logical OR is also called Conditional OR

Logical AND, OR are called Short circuit operators, will see why these are called short circuit soon.

Logical AND Operator
Operand1 Operand2 Evaluates To
TRUE TRUE TRUE
TRUE FALSE FALSE
FALSE TRUE FALSE
FALSE FALSE FALSE

As shown in the above table, && operator returns true if both the operands evaluates to true, otherwise returns false.

Example
class AndEx{
public static void main(String args[]){
boolean operand1 = true;
boolean operand2 = true;
System.out.println((operand1 && operand2));

operand1 = true;
operand2 = false;
System.out.println((operand1 && operand2));

operand1 = false;
operand2 = true;
System.out.println((operand1 && operand2));

operand1 = false;
operand2 = false;
System.out.println((operand1 && operand2));
}
}

Output
true
false
false
false

Why Logical AND is called short circuit operator
Since if the first statement in the expression evaluates to false, then java won't evaluates the entire expression. So Logical AND is called short circuit AND

Example
class ShortCircuitAnd{
public static void main(String args[]){
int a = 10, b=21;

if( (a>b) && (a++ > b) ){
System.out.println("This statement not evaluated");
}
System.out.println("a is not incremented " + a);

if( (a<b) && (a++ < b) ){
System.out.println("This statement is evaluated");
}
System.out.println("a is not incremented " + a);

}
}

Output
a is not incremented 10
This statement is evaluated
a is not incremented 11
Observation
In the expression (a>b) && (a++ > b), a>b is false, so && operator won't evaluates second statement in the expression, so a is not incremented.

Logical OR Operator
Operand1 Operand2 Evaluates To
TRUE TRUE TRUE
TRUE FALSE TRUE
FALSE TRUE TRUE
FALSE FALSE FALSE

As shown in the above table, || operator returns true if any of the operand evaluates to true, otherwise returns false.

Example
class OrEx{
public static void main(String args[]){
boolean operand1 = true;
boolean operand2 = true;
System.out.println((operand1 || operand2));

operand1 = true;
operand2 = false;
System.out.println((operand1 || operand2));

operand1 = false;
operand2 = true;
System.out.println((operand1 || operand2));

operand1 = false;
operand2 = false;
System.out.println((operand1 || operand2));
}
}
Output
true
true
true
false

Why Logical OR is called short circuit operator
Since if the first statement evaluates to true, then java won't evaluates the entire expression. So Logial OR is called short circuit OR

Example
class ShortCircuitOr{
public static void main(String args[]){
int a = 10, b=21;

if( (a<b) || (a++ < b) ){
System.out.println("This statement evaluated");
}
System.out.println("a is not incremented " + a);

if( (a>b) || (a++ > b) ){
System.out.println("This statement is evaluated");
}
System.out.println("a is incremented " + a);
}
}

Output
This statement evaluated
a is not incremented 10
a is incremented 11

Logical (!)NOT operator
Operand Evalutes To
FALSE TRUE
TRUE FALSE

If the operand is FALSE, ! Operator evaluates it to TRUE
If the operand is TRUE, ! Operator evaluates it to FALSE

Example
class LogicalNot{
public static void main(String args[]){
boolean a = true;
System.out.println((!a));

a = false;
System.out.println((!a));
}
}
Output
false
true

Our Team

We have 2500+ expert pool from all over the world. They are highly qualified and well experienced. All experts have experinced in teaching and good command over programming topics. Some of our experts are as follows:


John Doe

JAVA EXPERT(10+ Yrs Exp.)

Larry Doe

C EXPERT(12+ Yrs Exp.)

Ranith Kays

C++ EXPERT(8+ Yrs Exp.)

Joan Ray

OPEN SOURCE LANGUAGE EXPERT(8+ Yrs Exp.)