An order can be in the "status" of Completed, Corrected or some other status.

I saw some code that is checking it like this, the purpose is to disable some stuff when the status is in Completed or Corrected status.

 if (model.CurrentStatus != DSRHelper.OrderStatusEnum.Complete && model.CurrentStatus != DSRHelper.OrderStatusEnum.Corrected)

I can't get it why the engineer has used "AND" for this, shouldn't it be an "OR"?

  • Using && means it is neither Completed or Corrected. If you change it to || it could be one or the other. – Austin Henley Oct 29 '12 at 16:42
  • Given the statuses listed, this if statement is saying "it is in some other state". This is a perfectly correct and standard format - if(var != state1 && var != state2) can be much easier to write than (if var == state3 || var == state4 || ...). Also note the switch of != to ==. – user40980 Oct 29 '12 at 16:49
  • confusing and hard to grok logical expressions are what one often gets when code is not well thought out to start with. Apparently, despite its pretending name, DSRHelper is not particularly helpful in this case – gnat Oct 29 '12 at 18:53

No, AND appears to be correct here. If you switched it to OR, the condition would always be true. If it was Completed, then the second condition would be true. If it was Corrected, then the first would be true.

Keep in mind that the condition currently used is equivalent to

!(model.CurrentStatus == DSRHelper.OrderStatusEnum.Complete || model.CurrentStatus == DSRHelper.OrderStatusEnum.Corrected)

No, the AND is correct: if you put OR, you'd get a condition that is always true, because the status cannot be Complete and Corrected at the same time.

To understand why, use De Morgan's Laws to convert your proposed condition to an equivalent. You start with

NOT(status is complete) OR NOT(status is corrected)

and convert to

NOT((status is complete) AND (status is corrected))

this is always false.


If the condition used OR, the condition would always be true. In English, the current condition says "If the Current Status is not Complete and is not Corrected then..." implying that any state but those two will cause the condition to be true.

If you used OR, then absolutely any state would cause the condition to be true. If the current status is Complete, then the first half of the condition will be false, but the second half will be true, so the condition is true. If the current status is Corrected, the first half of the condition will be true so without even checking the second half, the entire condition is true (some languages or will still check the second half, but the condition will still be true if they do). If you use OR and the current status is neither Complete or Corrected then both halves of the condition are true. So OR will always be true.

Not the answer you're looking for? Browse other questions tagged or ask your own question.