Looping Construct
How do we translate this looping construct
to C?
We use a
while statement
Essential elements of a loop:
There are four essential elements to every loop:
-
Condition initialization
-
the condition determining if the loop iterates must be initialized
before the first test.
-
Condition test
-
the condition is tested to see if the body of the loop is to be executed.
-
Loop body
-
the steps to be executed, which MUST include
-
Condition update
-
a statement which modifies the condition such that at some time
it will become false.
For example:
i = 0; /* loop initialization */
while( i < 100 ) /* loop test */
i = i + 1; /* loop body and update */
For the stock program, the loop condition is:
...
while there are more stocks to proccess
...
How do we tell if there are more stocks?
We will look at three ways:
-
Count the stocks
-
Use a
special data value
-
Use
End of File
to Overview.