Forum Stats

  • 3,760,438 Users
  • 2,251,706 Discussions
  • 7,871,122 Comments

Discussions

How does the following NOT condition work?

gg
gg Member Posts: 120 Blue Ribbon
edited Sep 23, 2019 11:29AM in SQL & PL/SQL

Hi all,

   Kindly consider the following block of code.

set SERVEROUTPUT ON size unlimited

declare

v1 number:=1;

v2 number:=2;

begin

if not (v1=2 and v2=2) then

dbms_output.put_line('condition met');

else

dbms_output.put_line('condition not met');

end if;

end;

/

After executing it i get the output as "condition met".

Can somebody pls explain why do i get the above output and how does NOT work as both the condition in the first IF are not satisfied

V1=1

V2=2

and the condition i have given is "if not (v1=2 and v2=2)" as V2 being 2 makes the first if condition not being met and should print the output in the else condition.

Thanks

Gautam

Tagged:
ggmathguyPaulzipFrank Kulash

Best Answer

Answers

  • Paulzip
    Paulzip Member Posts: 8,460 Blue Diamond
    edited Sep 23, 2019 11:03AM Accepted Answer

    De Morgan's Law. states that

    not (v1=2 and v2=2)

    is equivalent to

    v1 <> 2 or V2 <> 2

    So, v1 <> 2 is true and the logic passes the test.

    I think perhaps you may want this...

    not (v1=2 OR v2=2) => (v1 <> 2 and V2 <> 2)

    gggg
  • Cookiemonster76
    Cookiemonster76 Member Posts: 3,410
    edited Sep 23, 2019 11:00AM

    v1=2 is FALSE

    v2=2 is TRUE

    so you've got

    IF NOT (FALSE AND TRUE)

    An AND is only TRUE if both conditions are TRUE.

    ie. TRUE AND FALSE is FALSE.

    IF NOT (FALSE)

    is TRUE.

    The only way that goes to the ELSE is if both conditions are TRUE.

    mathguyFrank Kulash
  • gg
    gg Member Posts: 120 Blue Ribbon
    edited Sep 23, 2019 11:00AM

    Thanks a lot for your quick response....

    Gautam

    Paulzip
  • Solomon Yakobson
    Solomon Yakobson Member Posts: 18,755 Red Diamond
    edited Sep 23, 2019 11:01AM

    Look at operation precedence. - conditions in parenthesis is calculated first:

    not (v1=2 and v2=2)

    So PL/SQL compares v1 with 2 and get FALSE since v1 = 1. Then it sees AND condition. AND result is FALSE if one of operands is FALSE, so it doesn't even bother comparing v2 to 2 since it knows final result is FALSE. Then PL/SQL applies NOT and since NOT FALSE = TRUE operator IF executes

    dbms_output.put_line('condition met');

    SY.

  • mathguy
    mathguy Member Posts: 10,094 Blue Diamond
    edited Sep 23, 2019 11:08AM
    580988 wrote:Thanks a lot for your quick response....Gautam

    While Paulzip's answer is certainly correct, and it probably addresses the reason you found this to be confusing, please note that De Morgan's laws have nothing to do with the answer to your question.

    Rather, Cookiemonster in Reply 2 gave you the direct answer. The condition v1 = 2 is FALSE. Therefore, no matter what else you connect to it with the AND operator, the result will also be FALSE. In particular, (v1 = 2 AND v2 = 2) is FALSE.  (Obviously: it would only be true if BOTH v1 and v2 were equal to 2; they are not.)  And then, the negation of the whole thing is TRUE. That is how the NOT operator works, by definition.

    As you can see, there is no need to reference De Morgan's laws, or any other Boolean identity, to understand the behavior you observed. Don't conclude from Paulzip's answer that "you didn't understand this because you didn't think about De Morgan's laws".

  • mathguy
    mathguy Member Posts: 10,094 Blue Diamond
    edited Sep 23, 2019 11:22AM
    Solomon Yakobson wrote:Careful!!! Not applicable to Oracle. Never forget about NULLs in Oracle:

    EDIT:  Oops!  It seems you completely changed the Reply I was responding to. I am keeping this though, since my observations below may be helpful regardless.

    To clarify:  (Note that this is not about Oracle, but about three-valued logic; this applies at least to the SQL Standard and to all SQL dialects)

    De Morgan's laws apply equally well to three-valued logic. Many people are not aware of this. Worse, some people believe the laws don't hold in three-valued logic. They do.

    The part of Paulzip's answer that does NOT apply in three-valued logic is writing the negation of v1 = 2 as v1 <> 2. This part ("which part one must be careful with, as it is not applicable to Oracle") has nothing to do with De Morgan's laws.

  • Paulzip
    Paulzip Member Posts: 8,460 Blue Diamond
    edited Sep 23, 2019 11:29AM

    Yes, De Morgan's Law certainly isn't necessary to understand the behaviour at all, and hopefully it didn't appear that I wasn't inferring it was.  I only referenced it, as it is often a useful tool for people to understand where logic might not be behaving as they expected, in that switching between conjunctive and disjunctive forms can often give you a "Oh, I see now!" moment.  Given his reply, I believe OP got that from my post.

  • Cookiemonster76
    Cookiemonster76 Member Posts: 3,410
    edited Sep 23, 2019 11:23AM

    I wouldn't say De Morgan's laws have nothing to do with it, though you are right that you don't need to know it - since, as I demonstrated, you can work out what De Morgan's law states easily enough from first principals.

    Paulzip
  • mathguy
    mathguy Member Posts: 10,094 Blue Diamond
    edited Sep 23, 2019 11:28AM

    Right - I even mentioned in my Reply that you probably addressed the reason the OP was confused. To say it another way - the OP asked a question, but he likely meant a different one (I thought the answer should be "this", because ".....")   I am sure your answer was helpful to the OP, explaining why their reasoning may have been flawed.

    Paulzip