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!


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.

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}


Or if both a and b are true - one or the other or both.

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 + (CD) which is pretty easily solved by plugging 1 in for true and 0 in for false. 10+(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.



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)

 A  |  B  | A OR B
 T  |  T  |    T
 T  |  F  |    T
 F  |  T  |    T
 F  |  F  |    F

 A  |  B  | A AND B
 T  |  T  |    T
 T  |  F  |    F
 F  |  T  |    F
 F  |  F  |    F

 A  | NOT A
 T  |    F
 F  |    T

This way, you show all possible combinations of A and B (where both are true, A is true and B is false, A is false and B is true, and both are false), and can join various combinations of operations together quite easily by simply adding an operation on columns already existing. For example, if we wanted the combined logical operation A OR ((NOT B) AND C), We first evaluate NOT B, then once we have that column, we can use it to evaluate (NOT B) AND C, and finally with that column we can evaluate the whole thing:

 A  |  B  |  C  |  NOT B  |  (NOT B) AND C  | A OR ((NOT B) AND C)
 T  |  T  |  T  |    F    |        F        |         T
 T  |  T  |  F  |    F    |        F        |         T
 T  |  F  |  T  |    T    |        T        |         T
 T  |  F  |  F  |    T    |        F        |         T
 F  |  T  |  T  |    F    |        F        |         F
 F  |  T  |  F  |    F    |        F        |         F
 F  |  F  |  T  |    T    |        T        |         T
 F  |  F  |  F  |    T    |        F        |         F

So, we know that there are exactly 5 combinations of A, B and C that result in a “true” result, and 3 that do not.

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)

I guess what he meant was the AND, OR, and NOT boolean operators. Your question was really confusing at first. I am also a newbie in C but this stuff is quite basic. AND returns true only if both operands are true. OR returns true if one of the operand is true. NOT converts true to false and false to true.

okay, it does. Thanks very much for your help :slight_smile: