c# - If statements working differently then expected -


This may or may be a stupid question, but if the outcome of the statements is different, the following will be 2

Say k = "z";

  1. if (! K == "x" ||! K == "y")

    {

    // do something

    }

    2.

      If (! (K == "x" || k == "y")) {// do something}  

    in my mind These two statements should be killed // Dew something .... only the bottom will have the same effect. Which is the result of the BTW hope.

    Again sorry, but I can not wrap my ignorant mind around it.

Your question is "why (not x) or (not y) Separately no (x or y) ? "

(not x) or (not y) no (x or y) is wrong if x or y is correct, and otherwise true. is false if both X and Y are true, and otherwise are true.

"Either X or Y is true" and "Both X and Y are true" are different.

When you are confused then there is a good technique: Create the truth table:

  XY No X No or Y (No X) or (No Y) No (X Or y) TTFFTFFTFFTTTFFFTTFTT  

Now it is clear that they are D?

If you draw the true table for NOT (X and Y) then you will see that it is (not x) or (not w) , Then those two values ​​are equivalent.

And if you draw the truth table for (not x) and (not y) you will see that it is not no (x or y) are equal. Again, these two values ​​are equivalent.

Make sure it's clear.

Now, you have a big problem here:

  k! = "X" || K! = "Y"  

This predicate nothing can be true. Suppose Kashmir is "X" then it is not clearly "Y", so the reality is true. Suppose that Kashmir is "y" then it is not clearly "x", so predicate is true. Suppose that k has any other value. So this is clearly not "X" and "N", so there is a doubtful truth. So no matter what, it is really true. (no X) or (not Y) does not mean "neither X and Y".


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -