-2

I have three if statements:

if (!(a == 0 && b == 0)) {...}

if (!(a == 0 && b != 0)) {...}

if (!(a != 0 && b != 0)) {...}

I would like to combine them in one code block such as a method.

I don't want the other statements to run if one has run. There are workarounds if I want to avoid coming up with some good logic but I'd like to know if there is a beautiful way to write that.

Jonas
  • 121,568
  • 97
  • 310
  • 388
Sandah Aung
  • 6,156
  • 15
  • 56
  • 98
  • it's a type. jsut `b` would be fine – Sandah Aung Dec 31 '14 at 04:15
  • 1
    Is the same logic executed if any of those `if` is met? – Luiggi Mendoza Dec 31 '14 at 04:15
  • I suppose that is the case. – Sandah Aung Dec 31 '14 at 04:16
  • 2
    Well then, use `||` to join the evaluation of all your `if`s. Apart of that, I won't say this is complicated `if-else` logic. – Luiggi Mendoza Dec 31 '14 at 04:17
  • Well somebody doing electronics said there is a way to make this more compact. I thought such a way existed. – Sandah Aung Dec 31 '14 at 04:21
  • There should be and you could do it after some boolean analysis. But more important: is this really necessary? Does this improves the code readability and performance or any other relevant factor for your app? If yes, then this is a great opportunity to improve this, probably with a code refactor. But if it is not, then just leave it as is. – Luiggi Mendoza Dec 31 '14 at 04:22
  • You're missing a case - what if b and not a? – Jon Kiparsky Dec 31 '14 at 04:25
  • "I don't want the other statements to run if one has run"--then you'll have to rethink your logic, because there's some overlap. For example, if `a` and `b` are both 0, the second and third statements will run in your example--but you say you only want one of them, so which one? – ajb Dec 31 '14 at 04:26
  • Hang on - you want to omit the other cases if the first one runs? And to omit the third one if the second one runs? So, basically put an `else` before the second and third ones? In that case, the third one will never run! – Dawood ibn Kareem Dec 31 '14 at 04:26
  • 2
    The first think I'd do is use DeMorgan's Law. Instead of `if (!(a == 0 && b == 0))`, write `if (a != 0 || b != 0)`. This will make it clearer what's going on, especially if you've written it wrong... this will be true unless **both** `a` and `b` are 0. – ajb Dec 31 '14 at 04:29
  • 1
    If the logic executed in `{...}` is the same then the tests are completely redundant as the code will be executed if a == 0 or b == 0 or a != 0 or b != 0 – Steve C Dec 31 '14 at 04:35
  • @ajb The problem is I came up with each of the if statements on their own and at one point I had to combine them in a way that I described in the question. – Sandah Aung Dec 31 '14 at 04:37

3 Answers3

5
if (!(a == 0 && b b == 0)) {...}

truth table

a b r

z n T 
n n T 
z z F
n z T

for

if (!(a == 0 && b b != 0)) {...}

truth table

z n F
n n F 
z z T
n z T

for

if (!(a != 0 && b b != 0)) {...}

truth table

z n T
n n F
z z T
n z T

common case

n z T

so result that works on all 3 condition is

a != 0 && b == 0

note that: all 3 conditions are totally different, this will only work if you care to execute if

(first && second && third) is true

validate all case by yourself

(z = zero, n = non zero, a, b variables, r = result, T = true, F = false)

jmj
  • 237,923
  • 42
  • 401
  • 438
1

Simplest way to express this:

public int foo(boolean a, boolean b) {
  int result;
  if (a) {
    if (b) { result = 1; }
    else { result = 2; }
  else {
    if (b) { result = 3; }
    else { result = 4; }
  return result;   
}

(notice that this evaluates a and b once each, which is the minimum you can expect here)

To keep @DavidWallace happy, here's a translation to the particular form of the original question:

public int foo(int a, int b) {
  int result;
  if (a==0) {
    if (b==0) { result = 1; }  // or whatever should happen, not specified in the question
    else { result = 2; }
  else {
    if (b==0) { result = 3; }
    else { result = 4; }
  return result;   
}
Jon Kiparsky
  • 7,499
  • 2
  • 23
  • 38
1

Just to re-word your question. You say that if one block runs, the others shouldn't. So when you wrote

if (!(a == 0 && b == 0)) {...}

if (!(a == 0 && b != 0)) {...}

if (!(a != 0 && b != 0)) {...}

You actually meant

if (!(a == 0 && b == 0)) { 
     // first ...
}
else if (!(a == 0 && b != 0)) {
     // second ...
}
else if (!(a != 0 && b != 0)) { 
     // third ...
}

But this is equivalent to

if (a != 0 || b != 0) { 
    // first ... 
}
else if (b == 0) { 
    // second ... 
}

Notice that the third ... can never run, as I stated in my comment.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110