mirror of
https://github.com/EiffelSoftware/eiffel-org.git
synced 2025-12-08 15:52:26 +01:00
Added example of commonly used form.
Author:halw Date:2010-02-01T21:53:19.000000Z git-svn-id: https://svn.eiffel.com/eiffel-org/trunk@428 abb3cda0-5349-4a8f-a601-0c33ac3a8c38
This commit is contained in:
@@ -46,7 +46,7 @@ The effect of such a multi-branch instruction, if the value of <code>exp</code>
|
||||
|
||||
The loop construct provides a flexible framework for doing iterative computation. Its flexibility lies in how the complete form can be tailored and simplified for certain purposes by omitting optional parts. We will explore the entire mechanism, but let's approach things a little at a time.
|
||||
|
||||
First here is the loop in what is probably its most commonly used form:
|
||||
First let's look at the loop in what is probably its most commonly usage. This is a case in which some parts of the loop construct have been omitted because they are not necessary. So, just remember that you are not seeing everything that loops can be in this example.
|
||||
|
||||
<code>
|
||||
from
|
||||
@@ -62,6 +62,24 @@ First here is the loop in what is probably its most commonly used form:
|
||||
|
||||
The effect is to execute <code>Initialization</code>, then, zero or more times until <code>Exit_condition</code> is satisfied, to execute <code>Loop_body</code>. (If after <code>Initialization</code> the value of <code>Exit_condition</code> is already true, <code>Loop_body</code> will not be executed at all.)
|
||||
|
||||
This form of the loop is used commonly to traverse data structures. For example, suppose that we wished to print every element in a linked list of strings. We can do so with this usage of the loop construct, as shown below.
|
||||
|
||||
<code>
|
||||
my_list: LINKED_LIST [STRING]
|
||||
|
||||
...
|
||||
|
||||
from
|
||||
my_list.start
|
||||
until
|
||||
my_list.off
|
||||
loop
|
||||
print (my_list.item)
|
||||
my_list.forth
|
||||
end
|
||||
</code>
|
||||
|
||||
The <code>Initialization</code> part will attempt to set the cursor at the first item of the list. The loop will exit when there is no active list item. Then, in the <code>Loop_body</code>, the current list item will be printed, and the cursor advanced.
|
||||
|
||||
|
||||
===Debug===
|
||||
|
||||
Reference in New Issue
Block a user