While the arithmetic operators perform computation mainly with values of int or double, the logical operators deal with values of bool.
There are three logical operators in C++: Logical Not, Logical And, and Logical Inclusive Or. These operators perform a logical computation in contradistinction to a mathematical computation; the operands to these logical operators are implicitly converted to bool, if necessary, and the result of a logical operation is a value of bool.
Operator name | Usage | Result |
---|---|---|
Logical Not | !a | true if a is false, false otherwise. |
Logical And | a && b | true if a and b are both true, false otherwise |
Logical Or (or more precisely, Logical Inclusive Or) | a || b | false if a and b are both false, true otherwise |
An expression whose result is bool is generally known as a condition, especially within the context of a statement or parameter that expects a boolean value.
The operands to the logical operators are typically subexpressions that are also conditions while using one of the comparision operators:
Operator name | Usage |
---|---|
Equal to | a == b |
Not equal to | a != b |
Less than | a < b |
Greater than | a > b |
Less than or equal to | a <= b |
Greater than or equal to | a >= b |
These operators can be used between arithmetic types, the result is true or false depending on what kind of comparison is used. What each particular comparison operator does is common sense, for example a < b returns true if a is less than b and false otherwise.
The comparison operators can also be used with two string values, in which case the operator performs a lexicographical comparison rather than an arithmetical comparision; one can think of string comparisons as operating under the same rules of the ordering in which words or phrases show up in a dictionary: the comparison is mostly dependent on alphabetical ordering.
A selection statement is a statement that selects the next statement(s) to execute depending on a condition. The first and most common one you may have already seen:
int x;
cin >> x;
if (x % 2 == 0) {
cout << "Even\n";
}
Here, "Even" will be printed if a user-entered integer is disivisble by 2. An optional else clause may also be provided to an if statement:
if (x % 2 == 0) {
cout << "Even\n";
} else {
cout << "Odd\n";
}
Now "Odd" will be printed instead if the condition evaluated to false. What gets supplied to an else clause is another statement, in this case a compound statement or block was provided: that is, one or more statements enclosed in braces.
Since an If Statement is a kind of statement in C++, another If Statement can be specified to an Else Clause. Effectively this mean one can chain several If Statements together:
double cs;
cout << "Enter credit score:";
cin >> cs;
if (cs >= 720) {
cout << "Excellent\n";
} else if (cs >= 690) {
cout << "Good\n";
} else if (cs >= 630) {
cout << "Fair\n";
} else {
cout << "Bad\n";
}
The other kind of selection statement in C++ is the switch statement, this will be demonstrated in lecture.