Hardware Reference
In-Depth Information
S
Figure 2.4 An infinite loop
If there is concern that the relationship n 1
n 2 may not hold, then it must be checked at the
beginning of the loop. Four steps are required to implement a FOR loop.
Step 1
Initialize the loop counter and other variables.
Step 2
Compare the loop counter with the limit to see if it is within bounds. If it is, then perform
the specified operations. Otherwise, exit the loop.
Step 3
Increment (or decrement) the loop counter.
Step 4
Go to step 2.
A For loop is illustrated in Figure 2.5.
W HILE C D O S
Whenever a While construct is executed, the logical expression C is evaluated first. If it
yields a false value, statement S will not be executed. The action of a While construct is illus-
trated in Figure 2.6. Four steps are required to implement a While loop.
i
n 1
i
n 2
No
No
i
n 2 ?
n 1 ?
Yes
Yes
S
S
i
i + 1
i - 1
(a) For i = n 1 to n 2 DO S
(b) For i = n 2 down to n 1 DO S
Figure 2.5 For looping construct
 
 
Search WWH ::




Custom Search