while( <condition> ) <statement>
while is a keyword.followed by a paranthesized condition
The condition is an expression which evaluates to either true or false.
and the body of the loop is a single statement.
in other words:
Note: we can use compound statements to execute multiple statements when the while condition is true. See compound statements below. For example,
while( <condition> )
{
<statement>
<statement>
<statement>
<statement>
}
Compound Statements
The compound statement groups an arbitrary number of other statments into a single statement. It is sometimes also called a block. It takes the form:
We use a while statement
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
...
We will look at three ways: Counting, special data values, and End of File