Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Explorer ,
Apr 01, 2016 Apr 01, 2016

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.

TOPICS
ActionScript
535
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 01, 2016 Apr 01, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 01, 2016 Apr 01, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 01, 2016 Apr 01, 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. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 01, 2016 Apr 01, 2016

This might be the crux of my question. I want the code to not to execute if either or any variable is equal to 1.

This might be a better example:

var failMode1: int = 1;
var failMode2: int = 0;

if (failMode1 != 1 || failMode2 != 1) {
trace("one of the varaibles equals 1")
} else {
trace("non of the variables equals 1")
}

Changing the value of either variable gives unexpected results.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 01, 2016 Apr 01, 2016

For what you just described, my first suggestion should be the answer... use AND instead of OR

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

     execute this code

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 01, 2016 Apr 01, 2016

I've just had a 'eureka' moment.

I need to create two separate statements. One checks if either variable 1 OR 2 are not equal to 1. A second statement checks if both are not equal to 1.

So something like:

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

// none of the variables equal 1;

}else{

// one or both variables equal 1

}

I think this will work!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 02, 2016 Apr 02, 2016

No, it will not, that code will fail wonderously.  I have the feeling you are not willing to consider anyone's help.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 03, 2016 Apr 03, 2016
LATEST

Sorry about that (it was a late night thought and I didn't test before posting), just trying to understand why it is behaving the way it does.

Anyway, for the record this works...

var failMode1: int = 1;
var failMode2: int = 0;

if ( !(failMode1 == 1 || failMode2 == 1)) {
trace("neither equal 1")
} else {
trace("one or both equal 1")
}

You can test by swapping the variable values - this gives me results I want and expected. Thanks for you help.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines