Like Tree10Likes

boolean operators question

This is a discussion on boolean operators question within the C Programming forums, part of the General Programming Boards category; hello, BRAND new to C coding... have some experience in HTML but thats it. actually very excited! i've been reading ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    5

    boolean operators question

    hello, BRAND new to C coding... have some experience in HTML but thats it. actually very excited!
    i've been reading the tutorial on the main website for C, and im on the second section, but I'm stuck on boolean operators. i read the section a few times and it gives you three examples at the bottom. they are these:

    Code:
    A. !( 1 || 0 )         ANSWER: 0	
    B. !( 1 || 1 && 0 )    ANSWER: 0 (AND is evaluated before OR)
    C. !( ( 1 || 0 ) && 0 )  ANSWER: 1 (Parenthesis are useful)
    heres the page: If Statements in C - Cprogramming.com Tutorial

    now i think i get how they work: you'd put them after an if statement, like

    Code:
    if !( 1 || 0 )
    if im right so far, then that particular statement (the first one) would read if the variable is not 1 or 0, then... am i right? or am i completely off? haha
    basically i'm asking if im on the right track, or if i'm not how in the hell do these things work... thanks so much

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by sfortunato View Post
    if im right so far, then that particular statement (the first one) would read if the variable is not 1 or 0, then... am i right? or am i completely off? haha
    basically i'm asking if im on the right track, or if i'm not how in the hell do these things work... thanks so much
    That tutorial is just demonstrating how the logical operators AND (&&) and OR (||) work. It has nothing to do with the variable being equal to 1 or 0 literally.

    Think of 0 as "false" and 1 as "true".
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by sfortunato View Post
    hello, BRAND new to C coding... have some experience in HTML but thats it. actually very excited!
    i've been reading the tutorial on the main website for C, and im on the second section, but I'm stuck on boolean operators. i read the section a few times and it gives you three examples at the bottom. they are these:

    Code:
    A. !( 1 || 0 )         ANSWER: 0    
    B. !( 1 || 1 && 0 )    ANSWER: 0 (AND is evaluated before OR)
    C. !( ( 1 || 0 ) && 0 )  ANSWER: 1 (Parenthesis are useful)
    heres the page: If Statements in C - Cprogramming.com Tutorial

    now i think i get how they work: you'd put them after an if statement, like

    Code:
    if !( 1 || 0 )
    if im right so far, then that particular statement (the first one) would read if the variable is not 1 or 0, then... am i right? or am i completely off? haha
    basically i'm asking if im on the right track, or if i'm not how in the hell do these things work... thanks so much
    For starters, the whole boolean expression must be in the parenthesis for if statements, so if anything it would be:
    Code:
    if (!(1 || 0))
    Which will mean prior to the negation, that "if it's 1 or it's 0", which when negated will mean "if it's not 1 or 0" - as you said.

    As for A, B, and C.
    A. Looks correct.
    B. I'll take your word that && is evaluated before ||, you shouldn't leave that in code, though, it's not very readable, if you have an && and an || in an expression, wrap one in parenthesis to make the code more readable. Correct.
    C. Correct, the OR will be evaluated first, evaluating to 1, 1 && 0 == 0, !0 = 1.

    Looks like you're pretty right on target as far as boolean logic goes. Keep up the good work!
    Last edited by Syndacate; 07-28-2011 at 11:46 PM.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by AndrewHunter View Post
    That tutorial is just demonstrating how the logical operators AND (&&) and OR (||) work. It has nothing to do with the variable being equal to 1 or 0 literally.

    Think of 0 as "false" and 1 as "true".
    Very good point, think it's also important to mention that in C, it will only check to see if it's 0, then it evaluates to false, ANY other number evaluates to true.

    ie.
    Code:
    if (5) ... // true
    if (-2) ... // true
    if (0) ... // false
    You'll find this useful later with pointers to verify whether a pointer is NULL or not, by doing:
    if (ptr) ... // if NULL (0), doesn't do it, any other address it evaluates to (ie. 0x20304020 (some random address)) will be true, and it will execute

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    5
    actually Syndacate, those A B and C examples are right from the tutorial and had the answers already next to them... i was just copy and pasting them because i didn't know how they worked... haha

    but ok, i think i'm understanding it a bit more now. so if 0 is false and 1 is true, then would these translate into these?

    Code:
    if  (!( 1 || 0 ))     if it's not true or false, then execute code
    if  (!( 1 || 1 && 0 ))  if its not true or true and false, then execute code (confusing o_o )
    is this right so far? thanks for the help everyone.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    A refresher of the tutorial:
    NOT: The NOT operator accepts one input. If that input is TRUE, it returns FALSE, and if that input is FALSE, it returns TRUE. For example, NOT (1) evaluates to 0, and NOT (0) evaluates to 1. NOT (any number but zero) evaluates to 0. In C NOT is written as !. NOT is evaluated prior to both AND and OR.

    AND: This is another important command. AND returns TRUE if both inputs are TRUE (if 'this' AND 'that' are true). (1) AND (0) would evaluate to zero because one of the inputs is false (both must be TRUE for it to evaluate to TRUE). (1) AND (1) evaluates to 1. (any number but 0) AND (0) evaluates to 0. The AND operator is written && in C. Do not be confused by thinking it checks equality between numbers: it does not. Keep in mind that the AND operator is evaluated before the OR operator.

    OR: Very useful is the OR statement! If either (or both) of the two values it checks are TRUE then it returns TRUE. For example, (1) OR (0) evaluates to 1. (0) OR (0) evaluates to 0. The OR is written as || in C. Those are the pipe characters. On your keyboard, they may look like a stretched colon. On my computer the pipe shares its key with \. Keep in mind that OR will be evaluated after AND.
    Now look at your first question:
    Code:
    !(1 || 0)
    
    "OR" gets evaluated first. So - TRUE || FALSE --> TRUE
    
    Then NOT gets evaluated. So ! (TRUE) ---> FALSE
    Now the second question:
    Code:
    !(1 || 1 && 0)
    
    AND is evaluated before OR. So, TRUE && FALSE --> FALSE
    
    !(1 || FALSE) 
    
    Now OR is evaluated. So TRUE || FALSE --> TRUE
    
    Now the NOT is evaluated. NOT (TRUE) --> FALSE
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    2,690
    Your verbal description of the logic seems to be causing the confusion. Try to understand the concept step by step logically.

    Code:
    0 = false
    1 = true
    
    0 and 0 = 0
    0 and 1 = 0
    1 and 0 = 0
    1 and 1 = 1
    
    0 or 0 = 0
    0 or 1 = 1
    1 or 0 = 1
    1 or 1 = 1
    
    !0 = not 0 = 1
    !1 = not 1 = 0
    Therefore:

    Code:
    "if (!( 1 || 0 ))"
    
    (!( 1 || 0 )) = (!(1)) = 0
    and

    Code:
    "if (!( 1 || 1 && 0 ))"
    
    (!( 1 || 1 && 0 )) = (!( 1 && 0 )) = (!(0)) = 1
    I hope this helps - does this make it more clear?

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    5
    ah yes! awesome, i think i get it! so:

    !( 1 || 0 && 1 )
    goes to
    !( 1 || 0 )
    then
    !(1)
    then finally,
    0

    yes, am i right? thanks again so much for the help everyone.
    also, don't mean to take advantage of the thread help here, but could you replace those numbers with variables? i.e.:
    Code:
    if (!( x && y ))
    and if x was 5 and y was 0 it'd be true. or is this what you're supposed to do with these things? haha.

  9. #9
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by sfortunato View Post
    ah yes! awesome, i think i get it! so:

    !( 1 || 0 && 1 )
    goes to
    !( 1 || 0 )
    then
    !(1)
    then finally,
    0

    yes, am i right? thanks again so much for the help everyone.
    also, don't mean to take advantage of the thread help here, but could you replace those numbers with variables? i.e.:
    Code:
    if (!( x && y ))
    and if x was 5 and y was 0 it'd be true. or is this what you're supposed to do with these things? haha.
    Yes, that's correct.

    As I said before, when you get to pointers (which will be denoted with variables, typically), they'll become a great help. You can plug anything in C into a boolean expression - it does no sort of type checking that I know of, only evaluates what's there.

    Your example would work, yes, 5 && 0 == FALSE since it's evaluated as: TRUE && FALSE (which is FALSE), so negating that would be TRUE.

    The reason it seems so weird is because you're not using them right, consider you have 3 status codes, 0, 5, and 10, and you want to print different things depending on which it is.
    ie.
    Code:
    int status_code = getStatusCode();
    if (status_code == 5 || status_code == 10)    // Is True if status code equals 5 OR if status code equals 10
        ...do something
    else if (!status_code)               // Is True if status code is equal to 0, as !0 = TRUE
        ...do something else
    You will see boolean expressions a lot in loops and if statements, mainly to determine whether to do something.

    EDIT:
    Let me give you a few more examples to help illustrate how boolean expressions are typically used
    Let's say you need to count down..
    Code:
    int counter = 230; // arbitrary number
    while (counter)    // **(can also be:  while (counter != 0      OR while (counter > 0), etc. - there will typically be more than one way to do it)**
    {
       printf("%i\n", counter);
       counter = counter - 1;
    }
    Or if you need to poll input until there's no more (ie end of file)
    Code:
    while (fgets(...)) ... //** fgets() can read from an open file handle, it will return a NULL pointer upon end of file, which will evaluate to 0, and will then be FALSE, and will exit the loop
    And like I said, pointer validation. If the stdlib function: malloc (memory allocator) is unable to allocate the requested memory (not enough memory, for instance), it will return a NULL pointer - you can check for this.
    Code:
    void* my_pointer = malloc(128);
    if (!my_pointer)
       printf("Unable to allocate memory\n");
    The multiple token boolean expressions, come into play when more conditions need to be met, ie.
    Code:
    if (input_value < 0 || input_value > 31) // bounds checking, for instance
    or
    Code:
    if (!getErrorState() && offset < 100) // This can call that function (that I just made up now, would return a 0 if everything is going well, return an error code if it's not, this is the standard C way typically, that's why it needs to be negated.  So it says:  "if the error state is 0 (no error) AND the offset is less than 100, do this"
    It is very common for functions in C to return a 0 if everything is working well, and an error code if it's not, these have to be negated with the exclamation mark or the equality operator.

    This is ironically contrary to pointer validation, in which 0 means it's NULL, and probably NOT working well, and a valid address that it's pointing to MAY mean that it is.

    Programs typically exit with the status code 0 if all is well, that's where you get the 'return 0;' from at the end of main. exit(0); will do the same thing. The numbers - you define them, but 0 is generally accepted as "no error" by just about everything.

    Unlike C++ and Java, C has NO built-in boolean, so boolean expressions must evaluate numeric values - which is everything, even characters as they're just numbers represented differently. Hope that cleared up better when you could use it.

    When working in C world, remember what I said earlier:
    If it's not 0, it's true
    Last edited by Syndacate; 07-29-2011 at 03:21 PM.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,675
    Quote Originally Posted by Syndacate View Post
    C runs on this principle as far as decision making goes. Even stuff like:
    Code:
    struct my_structure;
    if (my_structure) ...
    That will evaluate to true, as whatever it ends up evaluating to, it won't be 0....now that being said, I'm not quite sure how it would evaluate a stack allocated structure, but it doesn't really matter, all roads kind of lead to not 0 :-P.
    You really should verify your statements before making claims like that. That most definitely will NOT evaluate to true. That wont evaluate to anything, because it wont even compile. First, your code only gives a struct a tag (name), it doesn't describe what's in it. That's legal C, but largely useless. Second, the struct tag doesn't have meaning outside of being a type name (for an incomplete struct definition), and grammatically you need a variable, not a type name, in an if condition. Third, even if you define your struct, and make a variable of said type, it still wont compile. C needs a scalar (i.e. integral type like char, short, int, float, char *) to put in there. Note that float types are not recommended because a float value of 0.0 may not be all-zero bits (thus 0.0 could be logically true), and the inaccuracy in floats suggest you should use a tolerance check instead of strict equality. Here's an example showing that your example there is broken:
    Code:
    $ cat foo.c
    #include <stdio.h>
    
    
    struct foo {
        int a;
        int b;
    };
    
    
    int main(void)
    {
        struct foo my_structure;
        if (my_structure) {
            printf("my_structure is true\n");
        }
        return 0;
    }
    
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:11: error: used struct type value where scalar is required

  11. #11
    Registered User
    Join Date
    Jul 2011
    Posts
    5
    alright, i think i get it, so you can use these to check if variables = a number OR another number, if they equal a number AND another number, etc. so like

    Code:
    if ( x == 5 || x == 15 ) /* if x equals 5 or if x equals 15, then execute code */
    thanks so much for the help guys, really appreciate it all. !

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    alright, i think i get it, so you can use these to check if variables = a number OR another number, if they equal a number AND another number, etc. so like
    No type in C has two values at once. Even with unions, you have to interpret the data underneath as a type, which means it has one value.

    I mean, look at this:

    x == 5 && x == 15

    If x is 15 this comes up with false. If x is 5 this comes up with false.

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by whiteflags View Post
    x == 5 && x == 15
    Just wait until we're all using quantum computers. x might then be 5, 15 and a whole bunch of other values simultaneously
    AndrewHunter likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by whiteflags View Post
    No type in C has two values at once. Even with unions, you have to interpret the data underneath as a type, which means it has one value.

    I mean, look at this:

    x == 5 && x == 15

    If x is 15 this comes up with false. If x is 5 this comes up with false.
    Yeah, you can never AND checking a variable against 2 different numbers, and one twice is redundant. I feel he meant OR, there, though. He's still learning boolean logic.

    @ OP:
    I wouldn't get hung up too much on boolean logic. Whilst a big part of programming in any language, it's something you'll learn as you go along, not something you should spend much time on. Move on and as you learn more stuff that you can do with the language and libraries, look at more examples, etc. A lot of stuff like this (especially where it can be used in C) will become very obvious to you without thinking about it as you progress. So IMO, you've learned what you need to out of a basic boolean logic tutorial, you've got a good base, move ahead with your quest of learning C =).

  15. #15
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,675
    Quote Originally Posted by Syndacate View Post
    @anduril: Go screw yourself. If you got something against me because I felt that guys' answer had nothing to do with my question, that's fine, tell me in PM, block me, whatever, but trying to pull apart code snippets I write to illustrate a point and help somebody because you have something against me is just juvenile to say the least.

    Also, if you want to talk about the bolded part of your quote - that applies to you as well:
    ***You accused me of having incorrect syntax in my last thread, where when I re-read your post, it was clear that my syntax was 100% correct and you had simply never seen that before. So I would say that saying applies to you just as well. Verify that it's incorrect syntax before you accuse me of it being wrong, and it being "the source of my confusion."

    @ OP:
    I apologize that this manner got involved in your thread. There's some issues that anduril462 has against me because he believes people should be able to answer a question they have by drawing a correlation between any answer and any question.

    If he had it his way he would give you an answer of:
    Code:
    if (1 && 0)
    And expect you to extrapolate all your answers from that, feeling it's a valid answer.
    Mods, is there any way you can you split all this personal stuff out, it doesn't belong here. At the very least, you could move it to the thread in question: Pointer Asterisk Ownership Questions....


    @Syndacate: I don't have anything against you. I'm sorry we got off on the wrong foot, and I don't know why you're so hung up on this. If you still have lingering personal issues from the past thread, why don't you PM me? I was over it, but if you still need to iron some stuff out, then that's fine, I'm happy to discuss. I just It's just that sfortunato's thread is not the place to do it.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help on Boolean Operators?
    By yosimba2000 in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2009, 09:58 PM
  2. Boolean Operators...
    By Nean in forum C++ Programming
    Replies: 8
    Last Post: 10-06-2009, 03:31 AM
  3. boolean operators
    By forkpie hat in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2008, 03:35 AM
  4. Quick question on Boolean operators
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 11:43 PM
  5. Boolean operators
    By Trogdor27 in forum C++ Programming
    Replies: 10
    Last Post: 09-12-2005, 07:46 AM

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