The java ternary operator, without the else statement

Discussion in 'Computer Science & Culture' started by Voodoo Child, Mar 18, 2004.

Thread Status:
Not open for further replies.
  1. Voodoo Child Registered Senior Member

    Messages:
    1,296
    Not exactly a giant problem but how do you right a shorthand if with no else segment, ie

    a = (b == c) ? d: e;

    But without the e bit.

    This is really irritating me.
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. testify Look, a puppy! Registered Senior Member

    Messages:
    508
    Have you tried just removing e like so?:
    Code:
    a = (b == c)? d:;
    It's not a very common thing to use this syntax for an if statement without the else because an if statement can just be as long if it was:
    Code:
    if( b == c) { a = d };
    OR if it was:
    Code:
    a = (b == c)? d:;
    Normally the latter syntax is used for else statements that have recurring lines such as this:
    Code:
    if (Character.isUpperCase(aChar)) {
        System.out.println("The character " + aChar + " is upper case.");
    } else {
        System.out.println("The character " + aChar + " is lower case.");
    }
    
    Which could be made smaller into this:
    Code:
    System.out.println("The character " + aChar + " is " +
                       (Character.isUpperCase(aChar) ? "upper" : "lower") +
                       "case."); 
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. AntonK Technomage Registered Senior Member

    Messages:
    1,083
    you could always also simply use the current value. Since you want a to take the value of d or stay the same do this:

    Code:
    a = (b == c) ? d : a;
    -AntonK
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. Voodoo Child Registered Senior Member

    Messages:
    1,296
    Yes, I tried
    a = (b == c)? d:;
    ...didn't work.

    The particular code was in a for loop so your classical if wouldn't work. I should probably stop being so anal.
     
  8. testify Look, a puppy! Registered Senior Member

    Messages:
    508
    mind showing the code you're using?
     
  9. malkiri Registered Senior Member

    Messages:
    198
    Removing the third expression shouldn't be expected to work - if (b == c) evaluated to false, you'd be left with an assignment something like: a = ;, which is obviously not valid.

    I assume you want your code to look something like this:

    Code:
    for (int i=0; i<n; ++i, a = (b == c) ? d:a)
    {
     ... 
    }
    AntonK's suggestion will do what you want, but before you use it...is there a good reason not to do the following?

    Code:
    for (int i=0; i<n; ++i)
    {
      if (b == c)
        a = d;
    }
    It's clearer and more understandable, partly because the ternary operator takes a couple glances, and partly because it doesn't mix the loop control variables and your program's data. Plus, you'll avoid an unnecessary assignment of a = a whenever b != c, assuming it's not optimized out by the compiler.
     
  10. Voodoo Child Registered Senior Member

    Messages:
    1,296
    good point, Malk
     
  11. testify Look, a puppy! Registered Senior Member

    Messages:
    508
    This is a bit off topic but I didn't realize you could put more than one statement in a for loop section. Does it really work like that...seperating statements in the starting of the loop with commas to trigger them every time the loop occurs?

    Edit: tried to put the part of the code that mark showed, but it was messed up, even in the code tags...so meh.
     
  12. okinrus Registered Senior Member

    Messages:
    2,669
    The use of the ternary operater can be justified when it represents something not in the main path of control. For example, the use of it in the System.out.print statement mentioned above can be justified because the two paths of control don't effect the major path of control through the method. That said, it's almost never used except for short inline method functions in C++.

    The "," operator is a sequencing operator in C++, but I'm not sure about java. The Java for-loop grammar accepts a statement expression list, which is a list of statement expressions separated by commas, in the for update portion. But the statement expression does not include all statements but only the ones list <a href="http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#5984">here</a>
     
  13. malkiri Registered Senior Member

    Messages:
    198
    I don't know Java well enough to know for certain, but I'd be fairly surprised if it had a different syntax (regarding multiple statements). Officially, my example was in C++

    Please Register or Log in to view the hidden image!

     
  14. malkiri Registered Senior Member

    Messages:
    198
    Of course. My reply was regarding the question was about the comma separator.

    Edit: Hmm...did you delete your reply, okinrus?
     
  15. okinrus Registered Senior Member

    Messages:
    2,669
    Yes, my bad. It's likely that Java uses the same syntax as C on this.
     
Thread Status:
Not open for further replies.

Share This Page