2

I am not really sure what this method does, or better I am not sure what " : " means. Can someone please help me understand?

private int guess( )
 {
      return isTrue( ) ? A : isFalse(  ) ? B : neither( ) ? C : D;
 }
2
  • 1
    Strange example. Can isTrue() and isFalse() really both be false? Commented Apr 3, 2012 at 9:55
  • Maybe it's a fuzzy logic simulator? Commented Apr 3, 2012 at 11:52

5 Answers 5

8

This is a case of nested ternary operators which have the form a ? b : c which evaluates to:

if (a) then b, else c

So your question breaks down to this:

if (isTrue()) {
    return A;
} else if(isFalse()) {
    return B;
} else if(neither()) {
    return C;
} else {
    return D;
}
Sign up to request clarification or add additional context in comments.

Comments

3

this is a ternary

a ? b : c

means (roughly)

if (a)
   return b;
else
   return c;

Comments

0

The "?:" is the ternary operator. It means "if the condition before the question mark is true", then use the thing before the colon, otherwise the thing after the colon.

The code you posted will return A if isTrue(), B if !isTrue() && isFalse(), C if !isTrue() && !isFalse() && neither(), and D otherwise (!isTrue() && !isFalse() && !neither()).

Comments

0

This is called ternary operator.

isTrue()?a:b;

in above code

if isTrue is true a will be returned,otherwise b will be returned.

you have a nested ternary operator.

isTrue( ) ? A :
           isFalse(  )    ? B :
           neither( )     ? C         : D;

which means isTrue is true a returned,else if it is false b returned and if it is neither c returned else d will be returned.

@birryree given ultimate example code.

Comments

0

Your doubt is quite obvious. This type of syntax we call terinary operator. The actual syntax I am writing below:

Syntax:

Condition ? True part : False part ;

In the above statement , if condition is executed true then True part will execute if executed false then False Part will executed.

Example:

int x=10;

if(x==10) ? Print 10(true its Manoj) : Print Not 10(false its Anyone else) ;

Output:

Print 10(true its Manoj)*

I think these few lines will help to clear your doubts.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.