Skip to main content
Participant
April 1, 2016
Question

Using a conditional statement with two 'not equals' not returning anticipated result.

  • April 1, 2016
  • 1 reply
  • 605 views

I am trying to write the logic that executes only if neither of two variables do NOT equal 1.

I thought it would be as simple as combining two "not equal" statements with an OR.

For example, I think the following code is correct, but it does not work as anticipated - ie I think it should not execute:

== code ==

var failMode1: int = 1;

var failMode2: int = 0;

if (failMode1 != 1 || failMode2 != 1) {

    trace("this should not execute")

}

== end code ==

I know I could swap the statements around to make it work (ie use ==) but I'd like to understand what is happening. Any help/explanation would be appriciated.

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
April 1, 2016

I am guessing your logic is not correct though I am having a problem wrapping my head around your description of what you want because you use a double negative in that description.  I find it hard to translate it due to that.

      "logic that executes only if neither of two variables do NOT equal 1              '

Had you used the word "either" instead of "neither" then I would say you want to use an AND, not an OR.

But in using neither, that would essentially negate the NOT that you emphasize in your description.  So can you take another shot at describing what you want if using an AND does not get you there?

TheVille2Author
Participant
April 1, 2016

Sorry for the confusion, I'll try an elaborate.

I have a situation where I have two variables that need three states each (so I cannot use a boolean). The states are 0, 1 & 2.

What I need to determine is when any of the two variables is either 0 or 2. In other words not 1.

So, the example in my first post might be written in English like this:

if (variable 1 does not = 1) OR (variable 2 does not = 1) do not execute the code.

I know it's a bit backward (and I have got a working solution using other logic) but I'm interested in why the logic here doesn't seem to work.

Ned Murphy
Legend
April 1, 2016

You continue to use a double negative so it remains confused.  If you try thinking in terms of executing the code instead of not executing the code you might see the light at the end of the tunnel.

For the code you had, you should not see a trace only if both of the values = 1, meaning the code will not execute only when both ar 1.  Otherwise, if either of them is 0 or 2 then the trace will display.