logical operators

This is a discussion on logical operators within the C Programming forums, part of the General Programming Boards category; ok now here's my confusion logical operators Code: int i, j,k; i=10; j=1; k=-2; printf("%d", !i < j); //what does ...

  1. #1
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    logical operators

    ok

    now here's my confusion logical operators


    Code:
    int i, j,k;
    i=10; j=1; k=-2;
    printf("%d",  !i < j);    //what does   !i   mean???  //
    printf("%d",  !!i < !j);     //  what does !!i   mean ???  //
    printf("%d", i && j || k);    //  what is the result asking for ?? //
    printf("%d", i < j || k);     //    again, there's no if clause asking for comparison, so what does the result ask for??  //

    I compiled code and ran and all the results were 1.
    Is that a 1 meaning TRUE for the relation between 2 values??
    I am just so confused on the use of these logical operators.

    Gimme loops or if-else statements anytime!!


    Sue B.

    dazed and confused


  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    explained recently on the c++ board. have a look for a thread called AND,OR and NOT or something like that.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    CLARIFICATION PLEASE

    Sorry, Stone, read the post on AND , OR , & NOT and don't get it.
    Please help.
    I guess what is confusing me is using < or > with AND OR NOT
    and getting an evaluation.

    So.........

    int i, j,k;
    i=10; j=1; k=-2;
    printf("%d", !i < j);

    Is this saying, NOT i < j
    thus NOT 10 < 1
    thus TRUE
    yields TRUE and TRUE is a result value of 1 ???


    printf("%d", !!i < !j);

    Is this saying, NOT(NOT i) < NOT j
    thus NOT (NOT 10) < NOT 1
    thus 10 < NOT 1
    thus TRUE
    yields TRUE, thus value of 1 ????

    printf("%d", i && j || k);

    Is this saying, (i AND j) OR k
    thus (10 AND 1) OR -2
    thus TRUE OR TRUE
    yields TRUE, thus value of 1 ????

    printf("%d", i < j || k);

    Is this saying, (i < j) OR k
    thus (10 < 1) OR k
    thus FALSE OR TRUE
    yields TRUE, thus value of 1 ????s
    Sue B.

    dazed and confused


  4. #4
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    Priority of operators, Inequality comes before logical AND (&&) logical OR (||) and although my text Im referencing from I pretty sure NOT comes before logical AND. Now referring back to your first post,


    i=10; j=1; k=-2;

    printf("%d", !i < j);

    It tests ( i < j ) or (10 < 1)
    this is no so it returns zero
    then NOT Zero is one
    so it prints one.

    *************************************

    printf("%d", !!i < !j);
    !!i is the same as i
    Not a 100% sure on what !j does but i think because j == 1
    "j" now becomes zero. So the whole thing becomes

    i<!j == 10<0 == 0


    *****************************

    try other values and play around with the logical operators. If you need more help post a reply

  5. #5
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212

    I made a slight error

    In fact, NOT (!) comes before inequalities (<, >, <= etc) it is a unary operator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Operators in C++
    By Flecto in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2009, 07:17 AM
  2. Codeblocks and logical operators
    By fhbwghads in forum C++ Programming
    Replies: 2
    Last Post: 12-07-2008, 04:22 AM
  3. logical operators
    By Marlon in forum C++ Programming
    Replies: 5
    Last Post: 07-13-2005, 02:55 AM
  4. Relational and Logical Operators
    By GSLR in forum C Programming
    Replies: 2
    Last Post: 03-03-2002, 03:33 PM
  5. Logical Operators
    By E i F x 65 in forum C++ Programming
    Replies: 6
    Last Post: 01-24-2002, 07:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21