-3

I really don't know what should be the title of this question, spent 10 minutes but this is the best I came up with.

The real question is very basic and I think I know the answer. But still, like the operator condition ? true-statement : false-statement, is there any shortcut of this kind of if statement?

    if(intA == -1 || intB == -1 || intC == -1 || intD == -1 || intE == -1)

Suggestion: Something like this could be added in Java:

    if((intA || intB || intC || intD || intE) == -1)
Bugs Happen
  • 2,169
  • 4
  • 33
  • 59
  • 1
    No, there isn't - and Stack Overflow isn't really an appropriate place for language feature requests. Do you really need 5 separate variables here? If you had a collection instead, there are other alternatives... – Jon Skeet Aug 10 '15 at 08:58
  • @JonSkeet: No its just a question, like that `? :` operator exists, so I thought there might be something for this kind of situation too. – Bugs Happen Aug 10 '15 at 09:08
  • @JonSkeet: I have another question, not related to this topic. How can I ask you that? Should I mention its [link](http://stackoverflow.com/q/31640967/1708390) here? – Bugs Happen Aug 10 '15 at 10:03
  • You don't ask *me* that - you ask it as a separate Stack Overflow question, assuming it *is* a separate question. If it's a request for clarification on an answer, then a comment would be appropriate. – Jon Skeet Aug 10 '15 at 10:14
  • Yes it was a request for clarification – Bugs Happen Aug 10 '15 at 11:15

4 Answers4

2

No.. There isn't. These are different variables with different values.

Suggestion: you can consider the all these variable necessary or not. If all these variables necessary there will be no way to simplify.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

Afaik, there is no real shortcut syntax for this. Probably, you could do some tricks with logical and/or to achieve this, but I would not recommend to do so as it would be harder to read:

if (((intA | intB | intC) & -1) == -1)

You could still add those ints to an collection, and check if -1 is contained in that collection.

Fun fact: In Python, there is syntactic sugar for comparing a variable with 2 values; you can write 2 < a < 3, which would not be possible in Java. But personally, I do not know a language where syntactic sugar for what you are asking for exists.

Alex
  • 1,175
  • 1
  • 8
  • 25
1

You should use lists for this purpose. For ex. You can implement function

findFirstEquals(List, Int) 

which iterate through the list, search for first element that equals to second parameter and returns true if found.

In this case your if would be like following

intList = ArrayList<Int>()
// put 5, 6, 7,8 etc...
if findFirstEquals(intList, -1) ... 
vvg
  • 6,325
  • 19
  • 36
0

You can use Switch() statement to make it more easy like below

 public void myMethod(int intValue)
 {
       switch (intValue) {
                 case -1:  //Your logic here ;
                           break;
                 case 1 : //Another condition
                           break;
                 default: //Default behaviour;
                           break;
       }              
 }  

Call method myMethod(yourValue) and passed your value to it.

Passed your integer value to switch it will handle it as per value you have passed.

May this will help you.

Yagnesh Agola
  • 4,556
  • 6
  • 37
  • 50