Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In Java,operator precedence, increment and decrement, why isn't the first println statement executed?

class Example{
    public static void main(String args[]){
        int x=99;
        if(x++==x){
            System.out.println("x++==x : "+x); //Why this code line is not run?
        }
        if(++x==x ){
            System.out.println("++x==x : "+x); 
        }
    }
}

Why isn’t the first println statement executed?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The operands of an expression are evaluated left to right.

In the expression x++ == x, first x++ is evaluated. It increments x by 1, but returns the original value of x. So x++ returns 99.

Then x is evaluated, which returns 100 (since it was incremented by x++).

Since 99 is not equal to 100, this condition evaluates to false.

If you change the expression to x==x++, you’ll get true.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading