| Previous | Table of Contents | Next |
If the number entered is less than or equal to 10, the if on line 10 evaluates to false. Program control goes to the next line following the if, in this case line 16. If you enter a number less than 10, the output is as follows:
Enter a number less than 10 or greater than 100: 9
The else clause on line 14 was clearly intended to be attached to the if statement on line 11, and thus is indented accordingly. Unfortunately, the else statement is really attached to the if on line 12, and thus this program has a subtle bug.
It is a subtle bug because the compiler will not complain. This is a legal C++ program, but it just doesnt do what was intended. Further, most of the time the programmer tests this program, it will appear to work. As long as a number that is greater than 100 is entered, the program will seem to work just fine.
Listing 4.6 fixes the problem by putting in the necessary braces.
Listing 4.6 Demonstrates the Proper Use of Braces with an if Statement
1: // Listing 4.6 - demonstrates proper use of braces
2: // in nested if statements3: #include <iostream.h>
4: int main()
5: {
6: int x;
7: cout << Enter a number less than 10 or greater than 100: ;
8: cin >> x;
9: cout << \n;
10:
11: if (x > 10)
12: {
13: if (x > 100)
14: cout << More than 100, Thanks!\n;
15: }
16: else
17: cout << Less than 10, Thanks!\n;
18: return 0;
19: }
![]() | Enter a number less than 10 or greater than 100: 20 |
![]() | The braces on lines 12 and 15 make everything between them into one statement, and now the else on line 16 applies to the if on line 10 as intended. |
The user typed 20, so the if statement on line 11 is true; however, the if statement on line 13 is false, so nothing is printed. It would be better if the programmer put another else clause after line 14 so that errors would be caught and a message printed.
![]() |
|
Often you want to ask more than one relational question at a time. Is it true that x is greater than y, and also true that y is greater than z? A program might need to determine that both of these conditions are true, or that some other condition is true, in order to take an action.
Imagine a sophisticated alarm system that has this logic: If the door alarm sounds AND it is after six p.m. AND it is NOT a holiday, OR if it is a weekend, then call the police. The three logical operators of C++ are used to make this kind of evaluation. These operators are listed in Table 4.2.
| Table 4.2 The Logical Operators | ||
| Operator | Symbol | Example |
|---|---|---|
| AND | && | expression1 && expression2 |
| OR | || | expression1 || expression2 |
| NOT | !! | expression |
A logical AND statement evaluates two expressions, and if both expressions are true, the logical AND statement is true as well. If it is true that you are hungry, AND it is true that you have money, THEN it is true that you can buy lunch. Thus,
if ( (x == 5) && (y == 5) )
would evaluate true if both x and y are equal to 5, and it would evaluate false if either one is not equal to 5. Note that both sides must be true for the entire expression to be true.
Note that the logical AND is two && symbols.
A logical OR statement evaluates two expressions. If either one is true, the expression is true. If you have money OR you have a credit card, you can pay the bill. You dont need both money and a credit card; you need only one, although having both would be fine as well. Thus,
if ( (x == 5) || (y == 5) )
evaluates true if either x or y is equal to 5, or if both are equal to five. In fact, if x is equal to five, the compiler will never check on y at all!
Note that the logical OR is two || symbols.
A logical NOT statement evaluates true if the expression being tested is false. Again, if the expression being tested is false, the value of the test is true! Thus
if ( !(x == 5) )
is true only if x is not equal to 5. This is exactly the same as writing
if (x != 5)
| Previous | Table of Contents | Next |