Problems with logic (&& or ||)

Asked

Viewed 205 times

3

I found strange the code that is in my system:

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

Apparently the code is weird, 'cause I’ll never have cmbCdTipoProcesso.SelectedValue = 3 or cmbCdTipoProcesso.SelectedValue = 4 and so on, then I thought, So why the logic AND and not the OR?

Going back 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.

It seems that the situation will never happen or am I wrong? Situation of if.

3 answers

5


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

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 taking any value except those that are 3 and 4.

For some reason the code needs to delete these values. I don’t know if it’s obvious because it needs to disregard these values for the action. That’s why it’s important to comment code, or better yet, write code that shows why you’re doing it.

In this case, I’d be wrong to use the || since it would always give true. Just as one number can never be equal to another different, one number will always be different from another.

I made a table of results trying to make several comparisons with the same variable and different values.

using static System.Console;

public class Program {
    public static void Main() {
        var x = 1;
        WriteLine(x == 1 || x == 2);
        WriteLine(x == 1 || x != 2);
        WriteLine(x != 1 || x != 2);
        WriteLine(x == 1 && x == 2);
        WriteLine(x == 1 && x != 2);
        WriteLine(x != 1 && x != 2);
        WriteLine(x == 3 || x == 2);
        WriteLine(x == 3 || x != 2);
        WriteLine(x != 3 || x != 1);
        WriteLine(x == 3 && x == 2);
        WriteLine(x == 3 && x != 2);
        WriteLine(x != 3 && x != 2);
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • Perfect Bigown, perfect. This will prove the guy wrong and I can work as is. The programmer before me is correct. There’s a lot of criticism of the guy and I didn’t come here for it, so I’m gonna prove the previous colleague was right.

  • 5

    @pnet If they have time to criticize (erroneously) Boolean algebra and micro-manage code like that, I would say that the problem is management, not implementation.

3

It’s much easier to solve this problem mathematically, simply by replacing your long variable names with type letters 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"

How simply translated does that mean:

A != 3 e A != 4

That is to say:

A é diferente de 3 e A é diferente de 4
  1. Suppose A is 3.

    Let’s take a look at the expression:

    A é diferente de 3 e A é diferente de 4

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

  2. If A were 4, we would be in the same situation.

  3. Only in the event that A is different at the same time of 3 and 4 is that block of your if will be executed.


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

if A != 3 e B != 4

according to one of the laws of Morgan’s:

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

i.e., in Portuguese:

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

In other words, the if will be executed when the situation in brackets is false (because the denial of false is true), or all cases in which 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 equals to 3 or B equals to 4, or both, the expression of if will not be considered true. I repeat, A has to be different from 3 and B it has to be different from 4, if not the block of the if will not be executed.

2

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

Remembering that the logical operator != means difference and not equality.

Another way to write the same logic would be equal to: valor < 3 && valor > 4,are two different ways to build the same wheel....

As for another question of boolean logic, I did not understand where you wanted to get, could explain better?

--EDIT, what @Maniero said is correct, using the || in which case it would be mistaken, because it would remove the uniqueness of the condition.

  • It’s hard to make the boss understand this here. His reasoning is reversed.

  • 1

    kkk @pnet, what’s done, is done, logic is just one, it’s no use your boss wanting to understand 'a' by 'b', this is no problem in programming, but in common sense

  • VB and C# are completely different languages, but the conditional logic is equal in both kk

  • In condition the only difference it would have is that while in different C# is equivalent to !=, in different VB is equivalent to <>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.