0

i can't find anywhere how to do methods with ? operator. only what i know:

boolean something = (!again) ? true : false;

this returns me true or false, but if i have if method with break ? like this:

if(!again){
break;
}

can i do that too with ? operator ? or do you know better way ? i have this method in while loop.

I dont know what to say more about that .. just that i canť find that. i was looking almost everywhere. Thank you

Thanx for reply <3

  • 1
    you can't (and shouldn't) do it using **ternary** operator – Kartik Feb 20 '19 at 00:20
  • 1
    What's wrong with the if-statement? – Erwin Bolwidt Feb 20 '19 at 00:21
  • `break` doesn't represent any value so it can't be used as such. It is like you would like to write `2+break` it makes no sense to allow it. Possibly related: [Why break cannot be used with ternary operator?](https://stackoverflow.com/q/37855337) (although it is not about Java) – Pshemo Feb 20 '19 at 00:23
  • you can make single-line if statements concise like this: `if (!again) break;` – Kartik Feb 20 '19 at 00:23
  • What precisely you want? – Vishwa Ratna Feb 20 '19 at 00:24
  • @CommonMan I think they want to replace every `if` statement with ternary operator so that the code looks *cool* – Kartik Feb 20 '19 at 00:29
  • This feels like a [XY Problem](http://xyproblem.info/). Why are you using a `break` in the first place? Why do you think that mixing in the ternary operator (`?`) will be helpful? In short, what is the *actual* problem that you're trying to solve? – Jordan Feb 20 '19 at 00:29
  • `break` is not a type_variable it's just an instruction used in conjunction with `for` or `switch`.(as @Pshemo mentioned). `?` is related to ternary operator which require a `type_var` to store the appropriate result. (in your case `boolean` is the type_var returned by `?`) – Traian GEICU Feb 20 '19 at 00:29
  • ALso `boolean something = (!again) ? true : false;` is overly wordy. Just do `boolean something = !again;`, it gives the same result. – Ole V.V. Feb 20 '19 at 00:29
  • if you have the method in a while loop, using if statement is the best way. ? ternary operator should be used when you want to assign a value to something like foo. String foo = (!again) ? "yes" : "no"; – Pallavi Roy Feb 20 '19 at 00:34
  • @ruslan - Where does this oft-quoted falsehood come from about the conditional operator only being allowed on the right-hand side of an assignment? You can use it anywhere an expression is permitted. This is legal Java: ```p("%d", 1+(x>0 ? 3 : 0)*5);``` though in questionable taste. –  Feb 20 '19 at 02:47

2 Answers2

1

break; is a statement. In java, statements and expressions cannot ordinarily be mixed, although a few things (assignments, unary increment/decrement expressions, constructor calls, and method invocations) are both.

the ternary operator works only with expressions.

It's as simple as that. break is not an expression, and the 'a' and 'b' in cond ? a : b must be expressions.

if (!again) break; is the only way.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
1

The conditional operator is used as part of an expression. So

  x ? y : z

is an expression. You can use it anywhere an expression is called for. However,

  if (some_condition) some_action;

is a statement, and generally speaking, expressions and statements are not interchangeable. So if you're trying to get rid of the if statement, there is no way to do that with ?:

On the other hand, some_condition is a (boolean) expression, so you could write:

 if (x ? y : z) some_action;

as long as x, y, z are themselves boolean subexpressions. So in your original terms, this is legal:

if (!again ? true : false)
     break;

But it's redundant and reduces to

 if (!again)
     break;

so there is negative value in writing it using the ?: operator.