|
|
|
|
|
|
|
LISTING 4.4 A COMPLEX, NESTEDif STATEMENT |
|
|
|
 |
|
|
|
|
1: // Listing 4.4 - a complex nested
2: // if statement
3: #include <iostream.h>
4: int main()
5: {
6: // Ask for two numbers
7: // Assign the numbers to bigNumber and littleNumber
8: // If bigNumber is bigger than littleNumber,
9: // see if they are evenly divisible
10: // If they are, see if they are the same number
11:
12: int firstNumber, secondNumber;
13: cout << Enter two numbers. \nFirst: ;
14: cin >> firstNumber;
15: cout << \nSecond: ;
16: cin >> secondNumber;
17: cout << \n\n;
18:
19: if (firstNumber >= secondNumber)
20: {
21: if ( (firstNumber % secondNumber) == 0) // evenly divisible?
22: {
23: if (firstNumber == secondNumber)
24: cout << They are the same!\n;
25: else
26: cout << They are evenly divisible!\n;
27: }
28: else
29: cout << They are not evenly divisible!\n;
30: }
31: else
32: cout << Hey! The second one is larger!\n;
33: return 0;
34: } |
|
|
|
|
|
|
|
|
Enter two numbers.
First: 10
Second: 2
They are evenly divisible! |
|
|
|
|
|
|
|
|
Analysis: Two numbers are prompted for and then compared. The first if statement, on line 19, checks to ensure that the first number is greater than or equal to the second. If not, the else clause on line 31 is executed. |
|
|
|
|
|
|
|
|
If the first if is true, the block of code beginning on line 20 is |
|
|
|
|
|
|
|
|
executed, and the second if statement is tested on line 21. This |
|
|
|
|
|
|
|
|
checks to see whether the first number modulo the |
|
|
|
|
|