Ternary operator

• December 23rd, 2013, 12:33 PM
JRoberto
Ternary operator
Hi

Please can someone help me to translate this ternary operation:
Code :

```  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.
• December 23rd, 2013, 12:51 PM
Norm
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.
• December 23rd, 2013, 01:09 PM
JRoberto
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...
• December 23rd, 2013, 01:29 PM
Norm
Re: Ternary operator
I didn't try to compile the code to see if it had syntax errors. Does the posted code compile ok?
• December 23rd, 2013, 01:38 PM
JRoberto
Re: Ternary operator
Sorry i can not compile because i'm translating the source to another language (Purebasic).
This is the complete function:
Code :

```  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 ];```
• December 23rd, 2013, 01:41 PM
Norm
Re: Ternary operator
That must be javascript. function is not a keyword in java.
• December 23rd, 2013, 01:43 PM
JRoberto
Re: Ternary operator
Ah...ok.
Thank you.
• December 29th, 2013, 05:19 PM
UndersKore
Re: Ternary operator
Code java:

```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 ];```
• December 29th, 2013, 05:33 PM
JRoberto
Re: Ternary operator
Thank you!