Home > DeveloperSection > Forums > what is short circuit operator in java
jayprakash sharma
jayprakash sharma

Total Post:117

Points:821
Posted on    October-01-2014 6:56 AM

 Java Java  OOPS 
Ratings:


 1 Reply(s)
 854  View(s)
Rate this:
example of short circuit operator


Kamlakar Singh
Kamlakar Singh

Total Post:194

Points:1396
Posted on    October-03-2014 11:47 PM

The OR operator results in true when one operand is true, no matter what the second operand is.
The AND operator results in false when one operand is false, no matter what the second operand is.
If you use the || and &&, Java will not evaluate the right-hand operand when the outcome can be determined by the left operand alone.
The following code shows how you can use short-circuit logical operator to ensure that a division operation will be valid before evaluating it:

class Program

{

    static void Main(string[] args)

    {

            int x = 0;

            int y = 0;

            for(int i=0;i<10;i++)

            {

                if (x++ < 7 || y++ < 5) ;

            }

            System.out.println(x);

            System.out.println(y);

    }

}


Modified On Oct-03-2014 11:49:23 PM

Don't want to miss updates? Please click the below button!

Follow MindStick