| Previous | Table of Contents | Next |
![]() | Please enter a big number: 10 Please enter a smaller number: 12 Oops. The second is bigger! |
![]() | The if statement on line 11 is evaluated. If the condition is true, the statement on line 12 is run; if it is false, the statement on line 14 is run. If the else clause on line 13 were removed, the statement on line 14 would run whether or not the if statement were true. Remember, the if statement ends after line 12. If the else was not there, line 14 would just be the next line in the program. |
Remember that either or both of these statements could be replaced with a block of code in braces.
if Statement
Form 1
if (expression)
statement;
next statement;
If the expression is evaluated as true, the statement is executed and the program continues with the next statement. If the expression is not true, the statement is ignored and the program jumps to the next statement.
Remember that the statement can be a single statement ending with a semicolon or a compound statement enclosed in braces.
It is worth noting that any statement can be used in an if or else clause, even another if or else statement. Thus, you might see complex if statements in the following form:
if (expression1)
{
if (expression2)
statement1;
else
{
if (expression3)
statement2;
else
statement3;
}
}
else
statement4;
This cumbersome if statement says, If expression1 is true and expression2 is true, execute statement1. If expression1 is true but expression2 is not true, then if expression3 is true, execute statement2. If expression1 is true but expression2 and expression3 are false, execute statement3. Finally, if expression1 is not true, execute statement4. As you can see, complex if statements can be confusing!
Listing 4.4 gives an example of such a complex if statement.
Listing 4.4 A Complex, Nested if 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! |
![]() | 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 second number yields no remainder. If so, the numbers are either evenly divisible or equal. The if statement on line 23 checks for equality and displays the appropriate message either way.
If the if statement on line 21 fails, the else statement on line 28 is executed.
It is legal to leave out the braces on if statements that are only a single statement, and it is legal to nest if statements, such as those shown below.
if (x > y) // if x is bigger than y
if (x < z) // and if x is smaller than z
x = y; // then set x to the value in y
However, when you are writing large nested statements, this practice can cause enormous confusion. Remember, whitespace and indentation are a convenience for the programmer; they make no difference to the compiler. It is easy to confuse the logic and inadvertently assign an else statement to the wrong if statement. Listing 4.5 illustrates this problem.
Listing 4.5 Demonstrates Why Braces Help Clarify Which else Statement Goes with Which if Statement
1: // Listing 4.5 - demonstrates why braces
2: // are important in nested if statements
3: #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: if (x > 100)
13: cout << More than 100, Thanks!\n;
14: else // not the else intended!
15: cout << Less than 10, Thanks!\n;
16:
17: return 0;
18: }
![]() | Enter a number less than 10 or greater than 100: 20 Less than 10, Thanks! |
![]() | The programmer intended to ask for a number between 10 and 100, check for the correct value, and then print a thank you note. |
If the if statement on line 11 evaluates true, the following statement (line 12) is executed. In this case, line 12 executes when the number entered is greater than 10. Line 12 contains an if statement also. This if statement evaluates true if the number entered is greater than 100. If the number is not greater than 100, the statement on line 13 is executed.
| Previous | Table of Contents | Next |