true or false.
For example, the following code defines a boolean variable
(type bool) and sets it to true:
bool x = true;A Boolean value also be interpreted as a number, with
true being 1 and false is 0.
On the other hand, any numeric value can be interpreted as a boolean:
the number 0 is false and
all other numbers are true.
==, !=,
<, <=,
> and >=.
For example, the expression a+b == c is true,
if a+b and c are equal.
Ordinary logical operators are
&& (and) and || (or).
For example, the expression x >= 3 && x <= 8 is true,
if x is between 3...8.
Also, the operator ! in front of the boolean expression
reverses its truth value.
For example, x > 5
and !(x <= 5) have the same meaning.
We can clarify the interpretation of a boolean expression using parentheses.
For example, an expression
x%4 == 0 && (x%100 != 0 || x%400 == 0)
is true when x is a leap year.
The expression requires that x be divisible by 4
and if x is divisible by 100,
it is also divisible by 400.
Parts of the boolean expression go from left to right,
and if the result is already clear, the remaining parts will not be checked.
For example, the expression b != 0 && a/b == 2
a/b == 2 is not checked if b == 0,
because the result is definitely false.
So it doesn't hurt, even though in the section
a/b == 2 we would be dividing by zero.
if (condition) {
code
}
Here, the condition is some boolean expression.
If the condition holds, the code in the block is executed,
and otherwise nothing will happen.
We can also add another branch:
if (condition) {
code1
} else {
code2
}
Now if the condition holds,
execute code1, and otherwise execute code2.
More generally, conditions can be concatenated as follows:
if (condition1) {
code1
} else if (condition2) {
code2
} else if (condition3) {
code3
...
}
This code goes through the conditions and executes the first code for
which the preceding condition is true.
int age;
cout << "How old are you? ";
cin >> age;
if (age < 0 || age > 120) {
cout << "Unlikely";
} else if (ika < 18) {
cout << "You're a minor";
} else {
cout << "You're an adult";
}
If age is below 0 or above 120, the message is "Unlikely".
Otherwise, the program will print "You're a minor",
if you are under 18, or "You're an adult",
if you are at least 18 years old.
Program execution may look like this:
How old are you? 10 You're a minor
= and ==:
= sets the value of a variable, whereas == compares values.
A common programming error is to write
= in a condition
instead of ==:
if (x = 5) {
cout << "monkey";
}
The condition here is the expression x = 5
that is, the setting of a variable.
The value of the expression is 5, which is true,
because all numbers except 0 are true.
So the condition is always true, that is, the code always prints "monkey".
{ and } characters.
For example, the following codes work the same way:
if (x == 5) {
cout << "monkey";
}
if (x == 5) cout << "monkey";The following code does not work as you might imagine:
if (x == 5)
cout << "monkey";
cout << "banana";
Because there is no block,
the condition only affects the following statement
that is, the code matches this:
if (x == 5) {
cout << "monkey";
}
cout << "banana";
a ? b : c
gets b,
if a applies,
and otherwise c.
Here's how to use a conditional expression:
int s = x%2 == 0 ? 5 : 8;This corresponds to the following code:
int s;
if (x%2 == 0) {
s = 5;
} else {
s = 8;
}