home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 3
/
PDCD_3.iso
/
languages
/
c
/
c_tutor
/
CHAP3_TXT
< prev
next >
Wrap
Text File
|
1987-11-21
|
19KB
|
455 lines
Chapter 3 - Program Control
THE WHILE LOOP
The C programming language has several structures for
looping and conditional branching. We will cover them all
in this chapter and we will begin with the while loop. The
while loop continues to loop while some condition is true.
When the condition becomes false, the looping is
discontinued. It therefore does just what it says it does,
the name of the loop being very descriptive.
Load the program WHILE.C and display it for an example
of a while loop. We begin with a comment and the program
name, then go on to define an integer variable "count"
within the body of the program. The variable is set to zero
and we come to the while loop itself. The syntax of a while
loop is just as shown here. The keyword "while" is followed
by an expression of something in parentheses, followed by a
compound statement bracketed by braces. As long as the
expression in parenthesis is true, all statements within the
braces will be executed. In this case, since the variable
count is incremented by one every time the statements are
executed, it will eventually reach 6, the statement will not
be executed, and the loop will be terminated. The program
control will resume at the statement following the
statements in braces.
We will cover the compare expression, the one in
parentheses, in the next chapter. Until then, simply accept
the expressions for what you think they should do and you
will probably be correct.
Several things must be pointed out regarding the while
loop. First, if the variable count were initially set to
any number greater than 5, the statements within the loop
would not be executed at all, so it is possible to have a
while loop that never is executed. Secondly, if the
variable were not incremented in the loop, then in this
case, the loop would never terminate, and the program would
never complete. Finally, if there is only one statement to
be executed within the loop, it does not need braces but can
stand alone.
Compile and run this program.
THE DO-WHILE LOOP
A variation of the while loop is illustrated in the
program DOWHILE.C, which you should load and display. This
program is nearly identical to the last one except that the
loop begins with the reserved word "do", followed by a
compound statement in braces, then the reserved word
Page 11
Chapter 3 - Program Control
"while", and finally an expression in parentheses. The
statements in the braces are executed repeatedly as long as
the expression in parentheses is true. When the expression
in parentheses becomes false, execution is terminated, and
control passes to the statements following this statement.
Several things must be pointed out regarding this
statement. Since the test is done at the end of the loop,
the statements in the braces will always be executed at
least once. Secondly, if "i" were not changed within the
loop, the loop would never terminate, and hence the program
would never terminate. Finally, just like for the while
loop, if only one statement will be executed within the
loop, no braces are required. Compile and run this program
to see if it does what you think it should do.
It should come as no surprise to you that these loops
can be nested. That is, one loop can be included within the
compound statement of another loop, and the nesting level
has no limit.
THE FOR LOOP
The "for" loop is really nothing new, it is simply a
new way to describe the "while" loop. Load and edit the
file named FORLOOP.C for an example of a program with a
"for" loop. The "for" loop consists of the reserved word
"for" followed by a rather large expression in parentheses.
This expression is really composed of three fields separated
by semi-colons. The first field contains the expression
"index = 0" and is an initializing field. Any expressions
in this field are executed prior to the first pass through
the loop. There is essentially no limit as to what can go
here, but good programming practice would require it to be
kept simple. Several initializing statements can be placed
in this field, separated by commas.
The second field, in this case containing "index < 6",
is the test which is done at the beginning of each loop
through the program. It can be any expression which will
evaluate to a true or false. (More will be said about the
actual value of true and false in the next chapter.)
The expression contained in the third field is executed
each time the loop is executed but it is not executed until
after those statements in the main body of the loop are
executed. This field, like the first, can also be composed
of several operations separated by commas.
Following the for() expression is any single or
compound statement which will be executed as the body of the
Page 12
Chapter 3 - Program Control
loop. A compound statement is any group of valid C
statements enclosed in braces. In nearly any context in C,
a simple statement can be replaced by a compound statement
that will be treated as if it were a single statement as far
as program control goes. Compile and run this program.
You may be wondering why there are two statements
available that do exactly the same thing because the "while"
and the "for" loop do exactly the same thing. The "while"
is convenient to use for a loop that you don't have any idea
how many times the loop will be executed, and the "for" loop
is usually used in those cases when you are doing a fixed
number of iterations. The "for" loop is also convenient
because it moves all of the control information for a loop
into one place, between the parentheses, rather than at both
ends of the code. It is your choice as to which you would
rather use.
THE IF STATEMENT
Load and display the file IFELSE.C for an example of
our first conditional branching statement, the "if". Notice
first, that there is a "for" loop with a compound statement
as its executable part containing two "if" statements. This
is an example of how statements can be nested. It should be
clear to you that each of the "if" statements will be
executed 10 times.
Consider the first "if" statement. It starts with the
keyword "if" followed by an expression in parentheses. If
the expression is evaluated and found to be true, the single
statement following the "if" is executed, and if false, the
following statement is skipped. Here too, the single
statement can be replaced by a compound statement composed
of several statements bounded by braces. The ex