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.
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.
Huh?
-Prelude
My best code is written with the delete key.
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
At it's very basic form it's sorta like this.
&& 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.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.");
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
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:
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.Code:if ( a == 10 || b == 10 ) printf ( "Something" );
-Prelude
My best code is written with the delete key.
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:'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 = FALSE; // Make MyBoolean falseEDIT: 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.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: 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
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.
*sulks back to C++ Programming board*
"Logic is the art of going wrong with confidence."
Morris Kline
Could we not do something like
To fool C?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; }
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
Yea, that's kinda what I do.Could we not do something like
To fool C?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; }
Code:#define false 0 #define true !false typedef int bool;
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.