Short Circuit Operators

Short Circuit Operators ( &&, || )

These operators are exactly same as normal operator |, ‘&’ except the following difference.

 General Operators ( &, | ) Short Circuit Operator ( &&, || )
 Both arguments should be evaluated always. Second argument evaluation is optional.
 Relatively performance is low.Performance is high.
 Applicable for both Boolean and integral types. Applicable only for Boolean type.

e1 && e2 ==> e2 will be evaluated if e1 is true.

e1 || e2 ==> e2 will be evaluated if e1 is false.

int x = 10 , y = 10 ;
if ( + + > 10 || + + y < 10 ) {      
x + + ;
} else {
y + + ;
}
s.o.p ( x + "-----" + y ) ;

Cases:-

&& &|| |
x = 11x = 11x = 12x = 12
y = 12y = 12y = 11y = 10
int x = 10 ;
if ( ( ++x < 10) && (x > 10) ) {
System.out.println ( “Hello” ) ;
} else {
System.out.println ( “Hi” ) ;
}

Output
Hi.

int x = 10;
if ( ( ++x < 10) || (x > 10) ) {
System.out.println ( “Hello” ) ;
} else {
System.out.println ( “Hi” ) ;
}

Output
Hello.

Leave a Reply