![]() |
Simply question about C program
I am a newbie and have little knowlege about programming. I do not understand the role of the logical operators-AND-OR NOT C, and how are they used? Looking forward to your response!
|
Re: Simply question about C program
"-AND-OR NOT C"
I'm a little confused by how you phrased this. Are you asking why and how you would use an "AND"? Let me know if this does or doesn't answer your question. http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B |
Re: Simply question about C program
Quote:
if (a && b) {do something if a AND b are both true} if (a || b) {do something if either (or both) a OR b is true. ie, a and b are not both false.} if (!a) {do something if a is NOT true} |
Re: Simply question about C program
Quote:
|
Re: Simply question about C program
I like to think of AND as a multiplication and OR as an addition. The analogy breaks down with negation though.
So you can think of A && B || (C && D) as A * B + (C*D) which is pretty easily solved by plugging 1 in for true and 0 in for false. 1*0+(1*1) = 1 (assuming A = 1, B = 0, C = 1, and D = 1) I have to rely on tricks like this because I struggle to do boolean stuff for some reason. Figured I'd share the trick. |
Re: Simply question about C program
Quote:
|
Re: Simply question about C program
Sometimes it's also helpful to use "truth tables" when dealing with logical operations (especially as they become more complex). Examples of the three operations being discussed here, where T indicates that the argument is logically TRUE (2+2 == 4), and F indicates it is logically FALSE (2+2 == 5)
Code:
Code:
A | B | C | NOT B | (NOT B) AND C | A OR ((NOT B) AND C)I had a sophomore-level class in college that spent a lot of time on stuff like this. There are rules for simplifying logic statements and such that can be very powerful, but essentially rely on analysis like this to know what conditions lead to a True or False result. Also a note on OR: With very few exceptions, OR means "Inclusive OR" - that it's true in every situation except when both arguments are false. Just about everything I've seen then makes a very distinctive difference for an XOR, or "Exclusive OR" operation - where it's only true if the two arguments are different - one is true and one is false. Essentially A XOR B = (A OR B) AND NOT (A AND B) |
Re: Simply question about C program
Quote:
|
Re: Simply question about C program
Quote:
|
| All times are GMT -5. The time now is 23:39. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi