Hour 10
- Getting Controls
It is harder to command than to
obey.
—F. Nietzsche
In Hour 7, "Doing the Same
Thing Over and Over," you learned to use the for, while, and do-while
statements to do the same things over and over. These three statements can be
grouped into the looping category that is a part of the control flow statements
in C.
In this lesson you'll learn about
the statements that belong to another group of control flow
statements—conditional branching (or jumping), such as
- The if statement
- The if-else statement
- The switch statement
- The break statement
- The continue statement
- The goto statement
If life were a straight line, it
would be very boring. The same thing is true for programming. It would be too
dull if the statements in your program could only be executed in the order in
which they appear.
In fact, an important task of a
program is to instruct the computer to branch (that is, jump) to different
portions of the code and work on different jobs whenever the specified
conditions are met.
However, in most cases, you don't
know in advance what will come next. What you do know is that something is
bound to happen if certain conditions are met. Therefore, you can just write
down tasks and conditions in the program. The decisions of when to perform the
tasks are made by the conditional branching statements.
In C, the if statement is the most
popular conditional branching statement; it can be used to evaluate the
conditions as well as to make the decision whether the block of code controlled
by the statement is going to be executed.
The general form of the if statement
is
if
(expression) {
statement1;
statement2;
.
.
.
}
Here expression is the conditional
criterion. If expression is logical TRUE (that is, nonzero), the statements
inside the braces ({ and }), such as statement1 and statement2, are executed.
If expression is logical FALSE (zero), then the statements are skipped.
Note that the braces ({ and }) form
a block of statements that is under the control of the if statement. If there
is only one statement inside the block, the braces can be ignored. The
parentheses (( and )), however, must always be used to enclose the conditional
expression.
For instance, the following
expression
if
(x > 0)
printf("The square root of x is:
%f\n", sqrt(x));
tells the computer that if the value
of x is greater than zero (that is, positive), it should calculate the square
root of x by calling the sqrt() function, and then print out the result. Here
the conditional criterion is the relational expression x > 0, which returns
1 for true and 0 for false.
Listing 10.1 gives you another
example of using the if statement.
TYPE
Listing 10.1. Using the if statement in decision making.
Listing 10.1. Using the if statement in decision making.
1: /* 10L01.c
Using the if statement */
2: #include <stdio.h>
3:
4: main()
5: {
6: int i;
7:
8: printf("Integers that can be divided
by both 2 and 3\n");
9: printf("(within the range of 0 to
100):\n");
10: for (i=0; i<=100; i++){
11: if ((i%2 == 0) && (i%3 == 0))
12: printf(" %d\n", i);
13: }
14: return 0;
15:
}
OUTPUT
After 10L01.exe, the executable of the program in Listing 10.1, is created and run from a DOS prompt, the following output is printed on the screen:
After 10L01.exe, the executable of the program in Listing 10.1, is created and run from a DOS prompt, the following output is printed on the screen:
C:\app>
10L01
Integers
that can be divided by both 2 and 3
(within
the range of 0 to 100):
0
6
12
18
24
30
36
42
48
54
60
66
72
78
84
90
96
C:\app>
ANALYSIS
As you see in Listing 10.1, line 6 declares an integer variable, i. Lines 8 and 9 print out two headlines. Starting in line 10, the for statement keeps looping 101 times.
As you see in Listing 10.1, line 6 declares an integer variable, i. Lines 8 and 9 print out two headlines. Starting in line 10, the for statement keeps looping 101 times.
Within the for loop, the if
statement in lines 11 and 12 evaluates the logical expression (i%2 == 0)
&& (i%3 == 0). If the expression returns 1 (that is, the value of i can
be divided by both 2 and 3 completely), the value of i is displayed on the
screen by calling the printf() function in line 12. Otherwise, the statement in
line 12 is skipped.
Note that the braces ({ and }) are
discarded in the if statement because there is only one statement under the
control of the statement.
The result shown on the screen gives
all integers within the range of 0 to 100 that can be divided by both 2 and 3.
In the if statement, when the
conditional expression is logical TRUE, the computer will jump to the
statements controlled by the if statement and execute them right away. If the
expression is false, the computer will ignore those statements controlled by
the if statement.
From time to time, you will want the
computer to execute some other specified statements when the conditional
expression of the if statement is logical FALSE. By doing so, you can use
another conditional branching statement in C—the if-else statement.
As an expansion of the if statement,
the if-else statement has the following form:
if
(expression) {
statement1;
statement2;
.
.
.
}
else
{
statement_A;
statement_B;
.
.
.
}
Here if expression is logical TRUE,
the statements controlled by if, including statement1 and statement2, are
executed. The statements, such as statement_A and statement_B, inside the
statement block and following the else keyword are executed if expression is not
logical TRUE.
The program in Listing 10.2 shows
how to use the if-else statement.
TYPE
Listing 10.2. Using the if-else statement.
Listing 10.2. Using the if-else statement.
1: /* 10L02.c
Using the if-else statement */
2: #include <stdio.h>
3:
4: main()
5: {
6: int i;
7:
8: printf("Even Number Odd Number\n");
9: for (i=0; i<10; i++)
10: if (i%2 == 0)
11: printf("%d", i);
12: else
13: printf("%14d\n", i);
14:
15: return 0;
16:
}
OUTPUT
The following result is obtained by running the executable file 10L02.exe:
The following result is obtained by running the executable file 10L02.exe:
C:\app>10L02
Even
Number Odd Number
0 1
2 3
4 5
6 7
8 9
C:\app>
ANALYSIS
Line 6 of Listing 10.2 declares an integer variable, i. The printf() function in line 8 displays a headline on the screen.
Line 6 of Listing 10.2 declares an integer variable, i. The printf() function in line 8 displays a headline on the screen.
The integer variable i is
initialized in the first expression field of the for statement in line 9.
Controlled by the for statement, the if-else statement in lines 10_13 is
executed 10 times. According to the if-else statement, the printf() function in
line 11 prints out even numbers if the relational expression i%2 == 0 in line
10 returns 1 (that is, TRUE). If the relational expression returns 0 (that is,
FALSE), the printf() function controlled by the else keyword in line 12 outputs
odd numbers to the standard output.
Because the if-else statement is
treated as a single statement, the braces { and } are not needed to form a
block of statement in the for statement. Likewise, there are no braces used in
the if-else statement because the if and else keywords each control a single
statement, respectively, in lines 11 and 13.
Note that the minimum width of 14 is
specified in the printf() function in line 13, so the output of the odd numbers
is listed to the right side of the even numbers, as you can see in the output
section. Because the program in Listing 10.2 checks numbers in a range of 0 to
9, the output shows that 0, 2, 4, 6, and 8 are even numbers, and 1, 3, 5, 7,
and 9 are odd ones.
As you saw in the previous sections,
one if statement enables a program to make one decision. In many cases, a
program has to make a series of decisions. To enable it to do so, you can use
nested if statements.
Listing 10.3 demonstrates the usage
of nested if statements.
TYPE
Listing 10.3. Using nested if statements.
Listing 10.3. Using nested if statements.
1: /* 10L03.c
Using nested if statements */
2: #include <stdio.h>
3:
4: main()
5: {
6: int i;
7:
8: for (i=-5; i<=5; i++){
9: if (i > 0)
10: if (i%2 == 0)
11: printf("%d is an even
number.\n", i);
12:
else
13: printf("%d is an odd
number.\n", i);
14: else if (i == 0)
15: printf("The number is
zero.\n");
16: else
17: printf("Negative number:
%d\n", i);
18: }
19: return 0;
20:
}
OUTPUT
After running the executable file 10L03.exe, I obtain the following output:
After running the executable file 10L03.exe, I obtain the following output:
C:\app>10L03
Negative
number: -5
Negative
number: -4
Negative
number: -3
Negative
number: -2
Negative
number: -1
The
number is zero.
1
is an odd number.
2
is an even number.
3
is an odd number.
4
is an even number.
5
is an odd number.
C:\app>
ANALYSIS
Listing 10.3 contains a for loop, starting in line 8 and ending in line 18. According to the expressions of the for statement in line 8, any tasks controlled by the for statement are executed up to 11 times.
Listing 10.3 contains a for loop, starting in line 8 and ending in line 18. According to the expressions of the for statement in line 8, any tasks controlled by the for statement are executed up to 11 times.
First, a decision has to be made
based on the return value of the relational expression i > 0 in the if
statement of line 9. The i > 0 expression is used to test whether the value
of i is positive or negative (including zero.) If the return value is 1, the
computer jumps to the second (that is, nested) if statement in line 10.
Note that line 10 contains another
relational expression, i%2 == 0, which tests whether the integer variable i is
even or odd. Therefore, the second decision of displaying even numbers or odd
numbers has to be made according to the return value of the second relational
expression, i%2 == 0. The printf() function in line 11 prints out an even
number if the return value is 1. Otherwise, the statement in line 13 is
executed, and an odd number is shown on the screen.
No comments:
Post a Comment