AND, NOT, OR, XOR, are those same operators you learned in school. I suggested you switch back to those days where this was taught to you in math class. A good source is to search google for "math truth tables".
When you have something like myInt > 12 you have a proposition. Basically this proposition states that "myInt is greater than 12".
Now, propositions have a value. If I say "2 potatoes make a pair of potatoes", i've built a proposition and the value of this proposition is TRUE. Because indeed 2 potatoes make a pair of potatoes.
So, assuming the variable myInt is equal to 17. The proposition above is also true. Because if myInt equals 17, then myInt certainly is greater than 12.
You are now half-way there.
Logical operators in C++ produce logical expressions. What they do is they grab one or more propositions and evaluate them producing one of two possible values. TRUE or FALSE
Let's see...
My car has wheels AND my car can fly. (propositions underlined, logical operator in bold)
Without any math what is the value of that expression. you know that the second proposition is false. So, If I just said that to you your answer would be: "That's a lie!"
The value of that expression is thus false. My car has wheels alright, but it certainly can't fly. The operator AND evaluated my expression to false. True AND False = False.
But what if I had said:
My car has wheels OR my car can fly. (propositions underlined, logical operator in bold)
You answer would probably be "Your car has wheels". You couldn't say that my statement was false. It was true. True OR False = True.
So, taking this to C++... Let's go back to the myInt example:
Code:
myInt = 17;
myInt > 12 && myInt < 25; // && means AND in C++
myInt < 12 || myInt < 25; // || means OR in C++
What is the value of each of those expressions?
The first is true. The second is true. In order to be able to evaluate you will have to remember about the truth tables. Again, search google for "math truth tables". They will tell you all combinations of true and false and the possible results for each of the logical operators.
An if statement evaluates a logical expression. It is the equivalent of saying: IF myInt > 12 do this, ELSE do that.
Code:
if( myInt < 12 ) {
// do this
}
else {
// do that
}
And this is how boolean expressions are evaluated in C++ (if you don't mind the crude explanation).
One last thing is left to be said.
In C++ (as in most other languages) TRUE and FALSE have a value. False evaluates to 0, while True evaluates to everything else.
What exactly does this mean? Well, any value that can be treated as a number can also be treated as a boolean (or logical value). And when that happens, when we want to treat that number has a boolean, any value other than 0 is considered true, while any value equal to 0 is considered false.
So, with our example of myInt above, I could write:
Code:
if( myInt ) {
// do something here if the variable myInt is not equal to 0
}
else {
// do something here if the variable myInt is equal to 0
}