Java-Short Circuit Operators (& &, | | ) / Chapter -8/Operator & Assignments


Short Circuit Operators are quite similar to bitwise operators except for the following differences.
Case 1.
x&&y    =>>  y will be evaluated if and only if x is true.If x is false then y won't be evaluated.

Case 2.
x  | | y   =>> y will be evaluated if and only if x is false .If x is true 
then y won't be evaluated.


Example 1.


int x=10 ,y =15;
if( ++x <10 & ++y > 15 )
{
         x++;
}
else
{

        y++;
}
System.out.println( x+".........."+y);

output is :   11 ......17



Example 2.

int x=10 ,y =15;
if( ++x <10 && ++y > 15 )
{
         x++;
}
else
{

        y++;
}
System.out.println( x+".........."+y);

output is :   11 ......16



Example 3.

int x=10 ,y =15;
if( ++x <10  | ++y > 15 )
{
         x++;
}
else
{

        y++;
}
System.out.println( x+".........."+y);

output is :   12 ......16



Example 4.

int x=10 ,y =15;
if( ++x <10 && ++y > 15 )
{
         x++;
}
else
{

        y++;
}
System.out.println( x+".........."+y);

output is :   12 ......16



Example 5. Choose the correct Option?

int x=10;
if(++x<10 && x/0 >10)
{
            System.out.println( "Hello");

}
else
{
            System.out.println( "Hi");

}
(a) Compile Time Error
(b)Runtime Exception / By Zero
(c)Hello
(d)Hi

Ans: Hi
Hint: If block's condition is false that is why the else block  will be  executed.

Next:Chapter -9/Operator & Assignments / Assignment Operators

Back:Chapter -7/Operator & Assignments / Bitwise Operators 

Home:

No comments:

Post a Comment

Be the first to comment!

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.

Regards,
JavaByChetan
Back To Home

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com