Quick question on Boolean operators

This is a discussion on Quick question on Boolean operators within the C Programming forums, part of the General Programming Boards category; I just got through the third lesson and was making some simple programs to practice what I learned. One of ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    5

    Quick question on Boolean operators

    I just got through the third lesson and was making some simple programs to practice what I learned. One of which is as follows (my question is mainly about the bold part):

    Code:
    #include <stdio.h>
    int main()
    {
        int x;
        for (x = 0; x <=5 ; x++){
            if (x == (1 || 3)){
                  continue;}
            printf("x is %d and not 1 or 3.\n", x);
            }
            return 0;
    }
    It yields the following output:
    x is 0 and not 1 or 3.
    x is 2 and not 1 or 3.
    x is 3 and not 1 or 3.
    x is 4 and not 1 or 3.
    x is 5 and not 1 or 3.

    When I wrote this, I was expecting the program to not output the "x is 1..." and "x is 3..." statements. Surely enough, it didn't output the "x is 1..." line, but it produced the "x is 3..." line, which confuses me. By using the OR operator on the bold part of the code, I expected it to be read as "if x is 1 or 3, continue the loop." It did this for x == 1, but it didn't do this for x == 3, which leaves me quite puzzled. I know I can write it as "x == 1 || x == 3," but I was trying to find a more efficient way to do it.

    Could some explain how the statement is being read in the program that makes it not produce the "x is 1..." line but produce the "x is 3..." line? Any other suggestions on how to achieve the result I want?

    Much obliged.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    In C, nonzero is true in a boolean expression, and true and false evaluate as 1 and 0 resp., so (1 || 3) evaluates as 1. You need to rewrite it as "x == 1 || x == 3", there's no more efficient way to do it.

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    326
    First iteration, the condition becomes:

    if (0 == (1 || 3))
    if (false == (true)) <-- (1 || 3) always evaluates to 1.

    which is false, so you print the value of x and that other stuff.

    Second iteration, you get:

    if (1 == 1)

    which is true, so you skip the printing.

    When x = 3, you get:

    if (3 == 1)

    which is false, so you get the printing.

    To get the result you want, use

    if (x == 1 || x == 3)

    EDIT: Beaten taking too long to type this out.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    In C, any nonzero value is true. This means that in the statement x == ( 1 || 3 ) 1 or 3 is evaluated first (as true). So the statement simplifies to x == 1. Since 3 is not 1, the output is correct.

    Maybe you wanted something like this.
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      int v;
    
      for ( v = 0; v < 5; v++ )
      {
        if ( v != 1 && v != 3 )
          printf( "value = %d\n", v );
      }
      return 0;
    }
    Last edited by whiteflags; 08-12-2007 at 09:07 PM.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    5
    Ah, I see. So Boolean operators always yield a numeric value of only 1 or 0? That makes sense now. Thanks a bunch for the explanations!

  6. #6
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Normally you'd hear that the logical operators (==, !, ||, && etc) return true or false. What this actually means is that they just return a normal int, with the value 1 or 0.
    You could try printing it:
    Code:
    printf("&#37;d %d\n", 1 && 0, 1 || 0);
    This should output "0 1".
    Again I don't know if standards guarantee that, but Prelude uses this to an advantage in her tree tutorials so I guess it's as good as standard.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    5
    Quote Originally Posted by OnionKnight View Post
    Normally you'd hear that the logical operators (==, !, ||, && etc) return true or false. What this actually means is that they just return a normal int, with the value 1 or 0.
    You could try printing it:
    Code:
    printf("%d %d\n", 1 && 0, 1 || 0);
    This should output "0 1".
    Again I don't know if standards guarantee that, but Prelude uses this to an advantage in her tree tutorials so I guess it's as good as standard.
    Haha. Well, I always feel that it's better to know more. Thanks for the input.

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    33
    Hai Dude...
    Please understand the basic difference between the bitwise operator and Logical operator

    bitwise operator-->performs operations bit by bit, so you are result is correct only in case of bitwise operations

    Logical operator--->it will give result only 0 and 1 so, in case of this always non-zero number(both positive and negative) is treated as truth value and zero is considered as false

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 04:48 AM
  2. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 05:44 AM
  3. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 12:04 PM
  4. Quick Question on File Names and Directories
    By Kyoto Oshiro in forum C++ Programming
    Replies: 4
    Last Post: 03-29-2002, 02:54 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 06:46 PM

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