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

I don't understand the logic of code that is given below

I have run this code in compiler and getting the output as 1 but I don’t understand how it will be 1. Please explain with answer.

public class Main {
    public static void main(String args[]) {
        int a = 10;
        int b = 2;
        System.out.println((a < b) ? a++ : --b);
    }
}

>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 ternary operator ?: will check the condition (a < b), since it is false, it will execute the expression after :, which is --b.

--b will subtract b by 1, and return the value after subtraction, which is 1.

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