Confusing

This is a discussion on Confusing within the C Programming forums, part of the General Programming Boards category; oh, and I basically just don't get what he said. It's just this part, I got all the other stuff ...

  1. #1
    John
    Guest

    Question Confusing

    oh, and I basically just don't get what he said. It's just this part, I got all the other stuff before it down pat.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Huh?

    -Prelude
    My best code is written with the delete key.

  3. #3
    John
    Guest

    Talking

    woops, for some reason it didn't post my first part.
    here is what I want to know. I am on lesson2 for this tutorial here and I got confused with the first paragraph. I guess I am thinking this is like checking to see if 1==2 but its false so get a return of 0. But if its 1==1 you get some value determined by the operations of the expression. I think you will see what I mean, I understood everything till this part.

    "The boolean operators function in a similar way to the comparison operators: each returns 0 if evaluates to FALSE or a nonzero number if it evaluates to TRUE.

    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) evalutes to 0, and NOT (0) evalutes to some nonzero. NOT (any number but zero) evaluates to 0. In C and 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 'this' 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 some nonzero number. (ANY NUMBER BUT ZERO) AND (0) evaluates to 0. (ANY REAL NUMBER BUT ZERO) AND (ANY REAL NUMBER BUT ZERO) evaluates to true (any nonzero number). 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."

    I posted the part on the "AND" operator to give you some context, but right now I'm trying to get past the "NOT" operator. Thanks for any help, John

  4. #4
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    At it's very basic form it's sorta like this.

    Code:
    int num1, num2;
    
    num1 = 5;
    
    printf("Enter a number: ");
    scanf("%d", &num2);
    
    if((num2 > 1) && (num2 == num1))
        printf("You hit lotto");
    
    else if((num2 > 1) && (num2 < num1))
        printf("you should have bought more tickets.");
    
    else if((num2 > 10) || (num2 < 10) && (num2 != num1))
        printf("lotto made a fortune off of the losers."); 
    
    else if(num2 != num1)
       printf("better luck next time.");
    && is a logical comparison of two or more events. If both or all are true then the expression is true or 1. If just one comparison in the expression is false, the entire expression is false. However, if are using or (the double pipe) and one expression is true, then the expression is true or 1. The not operator is just a way of testing for a condition that you don't want. Since you're purposely looking for a condition that is opposite of what you're after, it's quite useful.

    Not a technical answer, but hope it helps.
    Last edited by ronin; 08-17-2002 at 11:33 AM.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    The ! operator will simply change the boolean value to the opposite. So if a is true then !a will be false. The && operator is used for multiple tests in one expression, like so:
    Code:
    if ( a == 10 && b == 10 )
      printf ( "Something" );
    The printf is only reached if both a and b are 10. If either is not 10 then this test will fail. If a is not 10 then b will not be tested because the expression is already false. The || operator is like the && operator except that if any of the parts is true the test returns true:
    Code:
    if ( a == 10 || b == 10 )
      printf ( "Something" );
    If a is 10 then the printf will be reached, if b is 10 then the printf will be reached, if both are 10 then the printf will be reached. If neither are 10 then the test will fail.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User fletch's Avatar
    Join Date
    Jul 2002
    Posts
    176
    Not exactly sure what you're asking, but I'll give it a go anyway...

    A boolean value can be either true or false. As mentioned in the tutorial, a value of 0 is false, a positive non-zero number is considered true. The use of zero and a non-zero number to represent true and false is just implementation - a boolean value is really an 8-bit number. In C/C++, all you have to worry about is true or false - not the actual value. For example:
    Code:
    BOOL	MyBoolean;	// Create a boolean
    
    MyBoolean = TRUE;	// Make MyBoolean true
    MyBoolean = FALSE;	// Make MyBoolean false
    'true' and 'false' are keywords. They let the compiler take care of whether or not MyBoolean is zero or non-zero. The not operator ! changes back and forth between true and false.
    Code:
    BOOL	MyBoolean;	// Create a boolean
    
    MyBoolean = TRUE;	// Make MyBoolean true
    MyBoolean = !MyBoolean;	// Make MyBoolean false (the opposite of true)
    MyBoolean = !MyBoolean;	// Make MyBoolean true (the opposite of false)
    EDIT: I start typing and the next thing I know there's been two more posts... But you can see from Ronin and Prelude's posts the typical use of boolean expressions in if...then statements.
    EDIT: Fixed BOOL, TRUE, and FALSE - see Prelude's post below
    Last edited by fletch; 08-17-2002 at 12:16 PM.
    "Logic is the art of going wrong with confidence."
    Morris Kline

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    bool, true, and false are not keywords in C, only in C++. C89 offers no such support but C99 declares all three as macros.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User fletch's Avatar
    Join Date
    Jul 2002
    Posts
    176
    *sulks back to C++ Programming board*
    "Logic is the art of going wrong with confidence."
    Morris Kline

  9. #9
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Could we not do something like

    Code:
    typedef short BOOL;
    
    #define TRUE 1
    #define FALSE 0
    
    int main(void)
    {
         BOOL test;
         
         if(TRUE != FALSE)
         	test = TRUE;
         else
         	test = FALSE;
         
         printf("%d", test);
         
    
          return 0;
    }
    To fool C?
    Last edited by ronin; 08-17-2002 at 12:08 PM.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    Could we not do something like
    Code:
    typedef short BOOL;
    
    #define TRUE 1
    #define FALSE 0
    
    int main(void)
    {
         BOOL test;
         
         if(TRUE != FALSE)
         	test = TRUE;
         else
         	test = FALSE;
         
         printf("%d", test);
         
    
          return 0;
    }
    To fool C?
    Yea, that's kinda what I do.
    Code:
    #define false 0
    #define true !false
    
    typedef int bool;

  11. #11
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    No matter what a value is, to the CPU it will _always_ be either POSITIVE, ZERO, or NEGATIVE. That gives you three categories.

    Well, actually two, because the computer sees POSITIVE AND NEGATIVE as the same: both are NON-ZERO. The computer only knows of one number it can test against: ZERO. It has no other _guaranteed_ numbers to work with, so it performs _all_ of its tests against ZERO.

    As such, whenever you use boolean logic on a computer, you are not dealing with positive, negative, and zero, you are only dealing with ZERO or NON-ZERO. You don't care about sign.

    AND = is a filter for non-zero values
    NOT = is a filter for zero values

    1 AND 1 = 1
    1 AND 0 = 1
    0 AND 1 = 1
    0 AND 0 = 0 <= both must be zero before test fails.

    NOT or '!', is always used in the context of a test:

    IF NOT <value> is a test-- is the value zero or not? If the value is zero, then the test passes. If the value is anything else (itself), the test fails, because this is a test for zero.
    It is not the spoon that bends, it is you who bends around the spoon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Most confusing language
    By Suchy in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 04-22-2007, 09:08 PM
  2. Arrays are confusing...
    By GavinHawk in forum C Programming
    Replies: 10
    Last Post: 11-29-2005, 12:09 AM
  3. Confusing Pointer
    By loko in forum C Programming
    Replies: 4
    Last Post: 08-29-2005, 08:52 PM
  4. pointers are confusing!
    By ali1 in forum C Programming
    Replies: 10
    Last Post: 09-07-2004, 10:41 PM
  5. functions declaration, pointers, cast, .... CONFUSING
    By Rhodium in forum C Programming
    Replies: 7
    Last Post: 01-09-2003, 05:21 AM

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