-1

Can someone please explain this line of code

int num1;
int num2;
int num3;
int largest = ( (num1 > num2) ? ( (num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3));

in IF-Else statement so i can understand

Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
Kartik
  • 3
  • 1
  • It's trying to find out the largest number among 3 numbers. – Anik Islam Abhi Mar 05 '18 at 09:22
  • please let me know whats wrong with question before down voting – Kartik Mar 05 '18 at 09:23
  • @AnikIslamAbhi but i want to understand that what happing in this line "( (num1 > num2) ? ( (num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3));" – Kartik Mar 05 '18 at 09:24
  • 1
    Not downvoter, but in a very simple question like this, it's good to show some research effort. Try to write it as if-else ladder yourself. Write that to the question for others to verify. – hyde Mar 05 '18 at 09:25
  • 1
    You are expected to do serious research prior posting. You already know the term to google for. But obviously you prefer to dump some incomplete code here to have us explain something that you can read up in any good book or tutorial. And as said: the code you are showing is incomplete. It does nothing - because as written here, it wouldnt even compile. When you have "not working" code, then put up a real [mcve]. And then: what prevented you from **running** that code to figure what it is doing?! – GhostCat Mar 05 '18 at 09:26
  • @GhostCat i m new here so i dont know whats the rules and i m just beginner and i already did research and could't find the answer so i asked here i think i have to delete this question before i get blocked – Kartik Mar 05 '18 at 09:30
  • 1
    The *rules* are outlined at the [help]. And as said: try putting your search terms into a search engine first. "java ternary operator" gives you tons of good resources to start with. Please understand that this community is not a replacement for you trying to solve your problem yourself (first). – GhostCat Mar 05 '18 at 09:31

1 Answers1

1

This

int largest = ( (num1 > num2) ? ( (num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3));

is equivalent to

int largest;
if (num1 > num2) {
    if (num1 > num3) {
       largest = num1;
    } else {
       largest = num3;
    }
} else {
    if (num2 > num3) {
       largest = num2;
    } else {
       largest = num3;
    }
}

Though it could more simply be achieved with:

int largest = Math.max(num1, Math.max(num2, num3));
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • Thanks for your answer idk why people down voting my question if they keep doing that i will get blocked for asking more – Kartik Mar 05 '18 at 09:27
  • True. Except for the thing that we have no idea where num1, num2, num3 are coming from. – GhostCat Mar 05 '18 at 09:27
  • @GhostCat i m just asking what is haping with int largest Read the question heading first – Kartik Mar 05 '18 at 09:31
  • No. You are asking us to look at your code and do a translation for you that you could easily do yourself after 5 minutes of research. – GhostCat Mar 05 '18 at 10:10