Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Ternary operator

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Ternary operator

    Hi

    Please can someone help me to translate this ternary operation:
     
      return  ( ( ayr[7] >> ch   | tons >> ch )
              & ( ayr[7] >> ch+3 | noiv       )
              & 1 )  * amp[ ayr[8+ch] & 0x10
                       ? envv
                       : ayr[8+ch] & 0x0f ];

    in to IF and ELSE statement?

    Thank's.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Ternary operator

    Put what is before the ? into the if condition
    put what is between the ? and the : in the true part
    put what is after the : into the else part

    each section will return its value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ternary operator

    Normally i do this.
    But in this case, the blacket opened after amp is closed after : , at the end of expression
    That confused me...

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Ternary operator

    I didn't try to compile the code to see if it had syntax errors. Does the posted code compile ok?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ternary operator

    Sorry i can not compile because i'm translating the source to another language (Purebasic).
    This is the complete function:
     
    function cstep(ch) {
      if( ++ayr[ch+16] >= (ayr[ch<<1] | ayr[1|ch<<1]<<8) )
        ayr[ch+16]= 0,
        tons^= 1 << ch;
      return  ( ( ayr[7] >> ch   | tons >> ch )
              & ( ayr[7] >> ch+3 | noiv       )
              & 1 )  * amp[ ayr[8+ch] & 0x10
                       ? envv
                       : ayr[8+ch] & 0x0f ];

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Ternary operator

    That must be javascript. function is not a keyword in java.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ternary operator

    Ah...ok.
    Thank you.

  8. #8
    Junior Member
    Join Date
    Dec 2013
    Posts
    11
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Ternary operator

    if (ayr[8+ch] & 0x10) 
        return ((ayr[7] >> ch | tons >> ch) & (ayr[7] >> ch+3 | noiv) & 1) * amp[envv];
    else
        return ((ayr[7] >> ch | tons >> ch) & (ayr[7] >> ch+3 | noiv) & 1) * amp[ayr[8+ch] & 0x0f ];

  9. #9
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ternary operator

    Thank you!

Similar Threads

  1. The += assignment operator?
    By jean28 in forum Java Theory & Questions
    Replies: 2
    Last Post: January 19th, 2013, 01:03 AM
  2. meaning of operator ( |= )
    By ashish12169 in forum Java Theory & Questions
    Replies: 1
    Last Post: July 18th, 2012, 02:05 AM
  3. How to use final operator.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 9th, 2011, 06:57 PM
  4. Error with OR operator
    By tarkal in forum Loops & Control Statements
    Replies: 3
    Last Post: September 4th, 2011, 02:49 PM
  5. about Operator Precedence
    By degar in forum Java Theory & Questions
    Replies: 6
    Last Post: August 12th, 2011, 01:57 AM