&&
The logical AND operator, written &&
, also combines two expressions into one. The resulting expression has the value true
only if both of the original expressions are true
. Here are some examples:
5 == 5 && 4 == 4 // true because both expressions are true
5 == 3 && 4 == 4 // false because first expression is false
5 > 3 && 5 > 10 // false because second expression is false
5 > 8 && 5 < 10 // false because first expression is false
5 < 8 && 5 > 2 // true because both expressions are true
5 > 8 && 5 < 2 // false because both expressions are false
Because the &&
has a lower precedence than the relational operators, you don’t need to use parentheses in these expressions. Like the ||
operator, the &&
operator acts as a sequence point, so the left side is evaluated, and any side effects are carried out before the right side is evaluated. If the left side is false, the whole logical expression must be false, so C++ doesn’t bother evaluating the right side in that case. Table 6.2 summarizes how the &&
operator works.
Listing 6.5 shows how to use &&
to cope with a common situation, terminating a while
loop, for two different reasons. In the listing, a while
loop reads values into an array. One test (i < ArSize
) terminates the loop when the array is full. The second test (temp >= 0
) gives the user the option of quitting early by entering a negative number. The program uses the &&
operator to combine the two tests into a single condition. The program also uses two if
statements, an if else
statement, and a for
loop, so it demonstrates several topics from this chapter and Chapter 5.
// and.cpp -- using the logical AND operator
#include <iostream>
const int ArSize = 6;
int main()
{
using namespace std;
float naaq[ArSize];
cout << "Enter the NAAQs (New Age Awareness Quotients) "
<< "of
your neighbors. Program terminates "
<< "when you make
" << ArSize << " entries "
<< "or enter a negative value.
";
int i = 0;
float temp;
cout << "First value: ";
cin >> temp;
while (i < ArSize && temp >= 0) // 2 quitting criteria
{
naaq[i] = temp;
++i;
if (i < ArSize) // room left in the array,
{
cout << "Next value: ";
cin >> temp; // so get next value
}
}
if (i == 0)
cout << "No data--bye
";
else
{
cout << "Enter your NAAQ: ";
float you;
cin >> you;
int count = 0;
for (int j = 0; j < i; j++)
if (naaq[j] > you)
++count;
cout << count;
cout << " of your neighbors have greater awareness of
"
<< "the New Age than you do.
";
}
return 0;
}
Note that the program in Listing 6.5 places input into the temporary variable temp
. Only after it verifies that the input is valid does the program assign the value to the array.
Here are a couple of sample runs of the program. One terminates after six entries:
Enter the NAAQs (New Age Awareness Quotients) of
your neighbors. Program terminates when you make
6 entries or enter a negative value.
First value: 28
Next value: 72
Next value: 15
Next value: 6
Next value: 130
Next value: 145
Enter your NAAQ: 50
3 of your neighbors have greater awareness of
the New Age than you do.
The second run terminates after a negative value is entered:
Enter the NAAQs (New Age Awareness Quotients) of
your neighbors. Program terminates when you make
6 entries or enter a negative value.
First value: 123
Next value: 119
Next value: 4
Next value: 89
Next value: -1
Enter your NAAQ: 123.031
0 of your neighbors have greater awareness of
the New Age than you do.
The following is the input part of the program in Listing 6.5:
cin >> temp;
while (i < ArSize && temp >= 0) // 2 quitting criteria
{
naaq[i] = temp;
++i;
if (i < ArSize) // room left in the array,
{
cout << "Next value: ";
cin >> temp; // so get next value
}
}
The program begins by reading the first input value into a temporary variable called temp
. Then the while
test condition checks whether there is still room left in the array (i < ArSize
) and whether the input value is non-negative (temp >= 0
). If it is, the program copies the temp
value to the array and increases the array index by one. At that point, because array numbering starts at zero, i
equals the total number of entries to date. That is, if i
starts out at 0
, the first cycle through the loop assigns a value to naaq[0]
and then sets i
to 1
.
The loop terminates when the array is filled or when the user enters a negative number. Note that the loop reads another value into temp
only if i
is less than ArSize
—that is, only if there is still room left in the array.
After it gets data, the program uses an if else
statement to comment if no data was entered (that is, if the first entry was a negative number) and to process the data if any is present.
3.147.67.16