• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble with ternary tester  RSS feed

 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Could someone please help me make sense of this ternary operator. I checked the web and couldn't find any examples to clear it up for me. All the examples I saw included only one boolean expression followed by expression1 : expression 2.

What the book hit me with at the end of the chapter is below. Surprisingly, they didn't cover this extended version.



It looks like it is ( booleanExpression ? booleanExpression ? not sure here : expression1 : expression2 )

Of course the answer I picked was wrong.

Help?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the ternary syntax is:



which is shorthand for an if statement



In this example it looks like their "value if true" expression is itself a ternary expression.
I'll add some brackets to make it obvious how it is evaluating:


i.e. the equivalent of a nested if/else statement


Of course if I ever saw anybody actually WRITING code like this, I would probably (not so) politely suggest that they rewrite their code to make it more readable.
 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I wouldn't expect to see that, either. This is from Oracle, the OCA book. The authors promised to make the review questions harder than the questions found on the test. So I suppose they are trying to make sure their readers know every way something could be written, but not that they necessarily should.

Thanks for clarifying that for me.

The book says that the correct answer is 8.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just checking - you now understand why/how it comes up with the answer of 8 ?

And yeah, the stuff you have to learn to pass the certification exams is normally quite different to what you actually need to know in the working world.
 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, thank you.

(booleanExpression1 ? booleanExpression2 ? expression1 : expression2 : expression3);

The way I thought about it after reading your explanation is if booleanExpression1 is false, then jump to expression3 (print value there to command prompt). If the booleanExpression1 is true, then examine second boolean expression. If that is true, then take expression1, else use expression2.

I probably could have figured that out on my own if it wasn't for that second '?' sitting there making me question what the 10 was.
 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And also that the System.out.println() can accept different data types as parameters. The Java Gods hath decreed that println() shall graciously accept all data. Which is nice because I bombed ALL the questions about casting. Clearly, I didn't read close enough. But I am working on it.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote: . . . I would probably (not so) politely suggest that they rewrite their code to make it more readable.
It is possible to make nested ?: perfectly readable with the right indentation. Remember that ?: associates to the right, so let's start with the original expression, plus a space after the second colon:-Now, remembering it associates to the right find the rightmost ?: expression which is delimited by one operator (? or :‍) to the left (blue) and two operators to the right (green) of the rightmost ? (red), so I can underline that. In case anybody is colour blind, I shall try with different colours later.
x > 2 ? x < 4 ? 10 : 8 : 7
Now add ()Now indent the outer expression remembering that the part in () remains as a whole.You can now see it means if X > 2 then (x < 4 ? 10 : 8) otherwise 7.
Remembering the the () when added did not change the semantics of that expression, we can take the () off again:-Now indent the inner ?: expression similarly.Different colours:-
Now, remembering it associates to the right find the rightmost ?: expression which is delimited by one operator (? or :‍) to the left (orange) and two operators to the right (red) of the rightmost ? (blue), so I can underline that.
x > 2 ? x < 4 ? 10 : 8 : 7
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minuste ago, I wrote: . . . Now, remembering it associates to the right find the rightmost ?: expression which is delimited by one operator (? or :‍) to the left (blue) and two operators to the right (green) of the rightmost ? (red), so I can underline that. . . .
That doesn't read well. It means something like
  • 1: ?: associates to the right.
  • 2: Find the farthest right ?
  • 3: The expression extends not quite as far as the second : after that ?
  • 4: Go as far right as the second : and then come back one character. That is where the nested expression ends.
  • 5: The expression starts just after the last ? or : before that rightmost ?
  • 6: Go to the last : or ? before the rightmost ? and then come forwards one character. That is where the nested expression starts.
  •  
    Darryl Burke
    Bartender
    Posts: 5167
    11
    Java Netbeans IDE Opera
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I find it more readable if a nested ternary is in the 'or' position rather than the 'if' position, but maybe that's just me. On those lines, I would write the expressions as
     
    Campbell Ritchie
    Marshal
    Posts: 56529
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There are all sorts of ways you can write that sort of expression, and it takes quite a bit of practice to get used to.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!