2

I understand that:

value = (a > b) ? a : b;

is the same as:

if (a > b)
  value = a;
else
  value = b;

But I'm having trouble deciphering what this means:

EDIT (the previous example I used was not good, this is real code from another example):

Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID)
      : (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(R.xml.global_tracker)
          : analytics.newTracker(R.xml.ecommerce_tracker);

How do I rewrite the last equation in terms of if, else if, and else?

martynas
  • 12,120
  • 3
  • 55
  • 60
user2323030
  • 1,193
  • 4
  • 16
  • 37
  • 5
    It's uncompilable statement. – Ιναη ßαbαηιη May 01 '14 at 06:14
  • 1
    Well, it's straight out of this [example](https://developers.google.com/analytics/devguides/collection/android/v4/). Maybe Google wrote something uncompilable, but it seems like those chances are slim. **Edit** The letters `a` through `d` and the comparator `>` are ones I made up, but I'm really just asking about the `? :`. – user2323030 May 01 '14 at 06:16
  • 1
    To expand on what @IvanBabanin said, the first condition implies that `a` and `b` are non-`boolean` variables, but that leaves the condition of the second conditional incomplete. – awksp May 01 '14 at 06:17
  • I think everyone is missing the point of the actual question. I just want to know about the `? :`. I've edited the example. – user2323030 May 01 '14 at 06:20
  • A good place to start would be to think of things like this as a nested conditional: ` ? : ( ? : ( ? : ))` – awksp May 01 '14 at 06:20
  • The conditional could be roughly translated from ` ? : ` to `if (condition) then else ` – awksp May 01 '14 at 06:21
  • OMG, refactor it! Only use `? :` in a short, single line statement – Rangi Lin May 01 '14 at 06:24

3 Answers3

11

You need a second conditional for this ternary expression to work.

value = (a > b) ? a : (b > c) ? c : d;

Then it would become:

if (a > b) {
    value = a;
} else if (b > c) {
    value = c;
} else {
    value = d;
}

In your case:

if (trackerId == TrackerName.APP_TRACKER) {
    t = analytics.newTracker(PROPERTY_ID);
} else if (trackerId == TrackerName.GLOBAL_TRACKER) {
    t = analytics.newTracker(R.xml.global_tracker);
} else {
    t = analytics.newTracker(R.xml.ecommerce_tracker);
}

Resources:

Community
  • 1
  • 1
martynas
  • 12,120
  • 3
  • 55
  • 60
6

So based on the example you linked:

Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID)
      : (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(R.xml.global_tracker)
          : analytics.newTracker(R.xml.ecommerce_tracker);

Which you seemed to have used to produce value = (a > b) ? a : b ? c : d;, which should probably be more like, value = (a == b) ? c : (a == d) ? e : f, it would read something like...

Tracker t = null;
if (trackerId == TrackerName.APP_TRACKER) {
    t = analytics.newTracker(PROPERTY_ID);
} else if ((trackerId == TrackerName.GLOBAL_TRACKER)) {
    t = analytics.newTracker(R.xml.global_tracker);
} else {
    t = analytics.newTracker(R.xml.ecommerce_tracker);
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    Yes, thank you. This is exactly what I was asking about. That previous example I made up was bad and it was purely because I didn't understand `? :`. – user2323030 May 01 '14 at 06:23
3

Use an IDE like Eclipse that offers the possibility to automatically rewrite ternary operators as ifs. Ctrl + 1 opens the quick fix shown below.

enter image description here

Kirill Rakhman
  • 42,195
  • 18
  • 124
  • 148