I am having real trouble understanding these NOT AND and OR Statement in lesson 2 of the C++ tutorial. Cprogramming.com Tutorial: If Statements
I wondering if the input is true why does it return false. does this mean if the input !1 is true then it returns 0? and vise versa? please someone help me with this because I'm about to start school to take my prerequisites in order for me to get into my major which is software engineering. I really want to get a big head start on the courses I'll be taking in about 1 1/2 years to 2 years.
Also should I be starting with C++ having never done programming before or should I start with C?
Boolean algebra works on two values: true or false. For values that aren't true or false (like 3) C++ "converts" it to true or false using the basic idea that 0 is false and everything else is true.
NOT says: take the boolean value and return the other boolean value. So true becomes false and false becomes true. Think about it in english: You are allowed to go see the movie. Vs: You are not allowed to go see the movie.
AND asks if two values are both true. So TRUE/TRUE is TRUE but TRUE/FALSE, FALSE/TRUE, and FALSE/FALSE are all FALSE. Jack and Jill both start with y is false.
OR asks if at least one of the two values are true. TRUE/TRUE, TRUE/FALSE, FALSE/TRUE are true but FALSE/FALSE is FALSE. This operator does lead to some confusion as English tends to imply that one of the values are true but the other is false (you can have the red pill or the blue pill). Logically this is actually an XOR (Exclusive OR) where only one of the values may be true. So TRUE/TRUE would be false.
In C++, logical operations yield a result of type bool, which has a value true or false. If that is then converted back to an integer, true gives a value 1 and false gives a value zero.
In C++ (and C) a zero integer value corresponds to false and everything else is deemed true. That's the convention. So "if (23)" is functionally equivalent to the test "if (true)" or, more accurately, to the test "if (23 != 0)".
The ! operator is the logical NOT (i.e. !x gives a true result if x is false, and a false value if x is true [non-zero]). So !1 will yield false (zero), as will !23, and !0 will yield true (1).
The logical AND ("&&" or "and" in C++) takes two operands and returns a true result only if both operands are true. So (a && b) will yield false (zero) if either a or b are false, and true (1) otherwise.
The logical OR (|| or "or" in C++) takes two operands and returns a true result if either of the operands are true. So (a || b) will only yield false if both a and b are false.
I think I understand
let me got back and restudy that part of lesson 2 to see if I can figure out how they got the answer they got for the question in the quiz of that lesson.
wait so the logical Not or "!" is looking for the input of zero from the if statement if the logical Not doesn't get zero from the if statement it returns a zero but if it gets zero it returns a 1. is this how that works? I understand OR and AND from what grumpy and Thantos posts but I'm still have trouble with NOT. but this is what is confusing me.
A. !( 1 || 0 ) ANSWER: 0
why is NOT 1 OR 0 = False when OR says its true if either of the operands are true or is the NOT 1 mean 0.
so is the problem 0 || 0 = 0?
also if anyone has this stuff master can you give me you instant messenger name or something? being able to ask someone who knows directly is much better. I wont be IMing you whenever I'm stuck because I like to figure things out but this one took me for a loop.
You've heard of order of operations, right? Parentheses first, then look at what's outside.
man you just told me the same thing my sister said. so the reason
C. !( ( 1 || 0 ) && 0 ) ANSWER: 1 (Parenthesis are useful)
is because its 1 OR 0 evalutes to 1 then the outer parenthesis becomes !1 (which is really 0) !0 || 0 evaluates to 1 because both operands are NOT false so it gotta be true
but would it be done like this?
!( (1 OR 0 = 0) && 0 )
!( 0 && 0 = 0) so the statement become !0 so its 1. Right?
1 or 0 is 1. But 1 && 0 is still 0, so from then on it's right.
ok gotcha I typed that in wrong I was watching the Laker Magic Game lol Thank you to everyone who bothered to help me out.
hey I'm new here, I just started learning C++ and just want to say how helpful this thread has been. I was completely lost on lesson two as well but reading these replies has really helped me, so thanks everyone!
friends, I am new to C. here is problem, whose output I could not understand. could you please help me out?
the output is w=1 x=0 y=1 z=1
printf("\nw=%d x=%d y=%d z=%d\n",w,x,y,z);
I am confused why w=1 in the answer, when expression says w=4 or -1 or 0
thanks in advance
The logical operators OR and AND are short-circuiting. With OR that means stop when you find truth, and with AND it means stop when you find falsehood, because these results will end up being the results for the whole expression. If you evaluate any Boolean expression you will either find truth or falsehood.
The result of a Boolean expression also belongs in a bool. That way you can do things like
and it makes a little more sense to the layperson.
std::cout << std::boolalpha << whatdoesitmean << std::endl;