Problems with logic (&& or ||)

3

I found the code that is in my system strange:

if (cmbCdTipoProcesso.SelectedValue != "3" && cmbCdTipoProcesso.SelectedValue != "4")    
{....}

Apparently the code is strange because I will never have cmbCdTipoProcesso.SelectedValue = 3 or cmbCdTipoProcesso.SelectedValue = 4 and so on, so I thought: So why the AND logic and not the OR?

Returning to electronics, dealing with Boolean algebra, in an AND logic I have an output 1 when all inputs are 1, otherwise the output will always be 0, but applying to programming.

Does it seem like the situation will never happen or am I wrong? Situation of if .

    
asked by anonymous 07.01.2015 / 13:00

3 answers

5

Read again looking character by character. Your code reading, according to the explanation you wrote below it, is taking into account that you are seeing if both are equal at 3 and 4 at the same time. In fact this is impossible to happen.

The problem is that the code is checking if both are different of 3 and 4. And this is perfectly possible.

That is, this code is fetching any value except those that are 3 and 4.

For some reason the code needs to exclude these values. I do not know if it's obvious why you need to disregard these values for action. That's why it's important to comment code, or better yet, write code that shows why you're doing this.

In this case it would be wrong if you used || since it would always be true. Just as one number can never be the same as another, a number will always be different from another.

See in dotnetfiddle a table of results trying to make various comparisons with the same variable and different values;

    
07.01.2015 / 13:05
3

It is much easier to solve this problem mathematically by simply replacing your long variable names with letters type A or B , replacing the symbol && with a simple and or AND or e , (and ignoring, for practical reasons, that "3" and "4" are strings ). In your case we have:

cmbCdTipoProcesso.SelectedValue != "3" && cmbCdTipoProcesso.SelectedValue != "4"

That translated more simply means:

A != 3 e A != 4

That is:

A é diferente de 3 e A é diferente de 4
  • Assume A is 3.

    Let's check the expression:

    A é diferente de 3 e A é diferente de 4

    The expression above will be evaluated false , because A = 3 , even A != 4 .

  • If A was 4, we would be in the same situation.

  • >

    In a more general case, where times A and if to be valued:

    if A != 3 e B != 4
    

    That is, according to one of the laws of De Morgan :

    if !(A == 3 ou B == 4) 
    

    that is, in Portuguese:

    se não (A igual a 3 ou B igual a 4)
    

    In other words, the A block will be executed when the situation in parentheses is false ( because the false denial is true ), ie all cases where A is different from 3 and B is different from 4 at the same time , for example:

    A = 2 // A é diferente de 3
    B = 7 // B é diferente de 4
    
    If A is equal to 3 or B is equal to 4 , or both, the expression of B will not be considered true. I repeat, if has to be different from 3 and if has to be different from 4, otherwise the A block will not be executed.

        
    07.01.2015 / 15:39
    2

    What this passage tells us is that the value selected must be diferente de 3 co_de E .

    Remembering that the logical operator diferente de 4 means difference and not equality.

    Another way to write the same logic would be: != , are two different ways to build the same wheel ....

    As for the other doubt Boolean logic, I did not understand where you wanted to go, could you explain it better?

    - EDIT , what @Maniero said is correct, the use of valor < 3 && valor > 4 in this case would be wrong, because it would remove the uniqueness of the condition.     

    07.01.2015 / 13:04