C# AND Operator, how can this be true? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

C# AND Operator, how can this be true?

I get the AND operator most of the time. But not in this case. In the c# course, Logical Operators. At the first page they talk about AND Operator. Here they also show that only when BOTH are true, the result is true. However in their last question they ask: " If a is true and b is false, what is the result of !(a&&b)? " And that result is true. But how? First you say that BOTH needs to be true in order for the result to be true. And now when one of the statements is false. We still get true?? Again in the picture it says that it should have been false. for example: int childone = 18; int childtwo = 14; if ( !(childone >17 && childtwo >17)) { Console.WriteLine("Both are older then 17"); } So in my example. First child one is older then 17 so thats true. And child two is not older then 17 so its false. Then we switch that around with the NOT operator. Should we not get childone <17 and childtwo <17?? Which is false and true. And that should give us again a result of false cause they are not both younger then 17. ( childone is still 18 ) Maybe someone can help me out with this one. Would be nice if i could understand the logic behind this.

1st Jan 2018, 11:29 PM
Doki
9 Answers
+ 3
Hi, a = true; b = false; ! (a&&b) = true This is because the && operator needs both operands to be true to return true. a is true and b is false, so when ANDed together, false is returned, however because !false = true , true is returned. In your example false is returned from the statements and then !false = true, so true is returned The actual statements are not reversed just the opposite boolean value is returned
1st Jan 2018, 11:39 PM
Tarantino
Tarantino - avatar
+ 3
Just knowing that : - && needs both operands to be true to evaluate to true overall otherwise it is false - | | the or operator needs at least one operand to be true so it can be true overall otherwise it is false - the ! operator flips the overall boolean value so if it was overall true it is now false and if it was overall false then it is now true
2nd Jan 2018, 10:01 PM
Tarantino
Tarantino - avatar
+ 3
Glad I could help a bit, hopefully it becomes more clearer with practice! Good Luck
2nd Jan 2018, 10:38 PM
Tarantino
Tarantino - avatar
+ 2
Haha not at all, I aim to help until it is understood. Let's say we have: boolean t = true; boolean f = false; if (t) // same as saying if t == true { print 'trueee' } else { print 'falseee' } Here 'trueee' will be printed as t is true. The else will be ignored because t is not false. More complex with some repetition: An && operator will return true if both operands on either side of it are true else it will return false: 1 if ( t && t) // this will return true print 'both true so I am printed' 2 if ( t && f ) // this will return false print 'one is false so I will not be printed' Only statement 1 is printed as they are both true so the overall value is true Statement 2 is not printed because one value is false so overall it is false Adding the ! operator now: Do you know how I said statement 2 would not be printed? if we change it to this instead by just adding a ! : if (!( t && f )) This now returns true and the statement will be printed because the false is swapped for the opposite which is true. If we do the same for statement 1 : if (!( t && t )) This will now return false and the statement won't be printed anymore. You have: int childone = 18; int childtwo = 14; if (! (childone > 17 && childtwo > 17)) childone > 17 == true because 18 > 17 childtwo > 17 == false because 14 < 17 Thus you have: true && false which is overall false and (! ( true && false )) which is overall true thanks to the ! So the actual statements are not changed just the boolean values which are evaluated and returned change. Hopefully this has helped and if you have anymore questions please ask away.
2nd Jan 2018, 8:50 PM
Tarantino
Tarantino - avatar
+ 2
When using the ! operator, true will always be swapped for false and false will always be swapped for true in general. And yes you are correct about how the computer will see it as overall being true
2nd Jan 2018, 9:48 PM
Tarantino
Tarantino - avatar
+ 1
i think ill just have to keep this in mind. - the ! operator flips the overall boolean value so if it was overall true it is now false and if it was overall false then it is now true even though it goes up against their own rules i guess. Thanks for the help!
2nd Jan 2018, 10:15 PM
Doki
0
Oke so what i understand from you Minato, is that the > bigger then, just stays the same. that wont change because of the !NOT operator. correct? And the rest i still don't realy get. Doest this mean we only look at the Result? So it doesnt matter anymore or A and B are true or false. Result of the statement just gets the opposite boolean? Or does it still matter or A and B are false / true? Or does A stay true, and only B got changed here? Like you and Sololearn said, Both need to be true to return true. If we only change what ever value that is false to true, but true never change to false, that would make it easy. maybe you could give me a better example? or am i making you crazy already hahaha
2nd Jan 2018, 7:42 PM
Doki
0
i understand what you wrote here. so in you're example you say that. if (!( t && f)) will return true, because the false is swapped for the opposite. So can i take out of this that. When using the ! operator. True will lets say be ignored and not swapped for false. While false will always be swapped for true? So then the computer will read. if (!( t && f)) as if it was. if ( t && t ) Am i correct or wrong ?
2nd Jan 2018, 9:37 PM
Doki
0
oke, i think i get it. But still a bit weird that he sees it overall as being true. While true was swapped to false. Is there some general rule or something that you can follow. To know whatever its going to be true or false overall?
2nd Jan 2018, 9:51 PM
Doki