diff --git a/documentation/current/method/eiffel-tutorial-et/et-instructions.wiki b/documentation/current/method/eiffel-tutorial-et/et-instructions.wiki index 444cc47c..a6484f10 100644 --- a/documentation/current/method/eiffel-tutorial-et/et-instructions.wiki +++ b/documentation/current/method/eiffel-tutorial-et/et-instructions.wiki @@ -46,6 +46,8 @@ The effect of such a multi-branch instruction, if the value of exp The loop construct provides a flexible framework for 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. +====Some examples==== + First let's take a look at two examples. These examples both use a loop to visit and print the content of each node of a linked list of character strings. So, the list in question might be declared like this: @@ -104,7 +106,7 @@ First there is the ''initialization'' part: The first thing to occur in the execution of the traditional loop is the execution of any instructions in the initialization part (it is permissible for the initialization part to be empty of instructions, but the keyword from must be present to distinguish the traditional loop form). In our example, the feature start is applied to my_list which will attempt to set the list cursor to the first element in my_list. -The ''Exit condition'' part: +The ''Exit condition part'': until @@ -132,7 +134,7 @@ And finally, there's the ''End'' part: end -''End part.'' +''end part.'' ====A closer look at the ''iteration'' form==== @@ -157,7 +159,7 @@ The "as ic" indicates that a local iteration cursor object referenc ''loop body part''. -And lastly, of course, the iteration form includes an end part ... at the end. +And lastly, of course, the iteration form includes an ''end part'' ... at the end. Notice that the loop body does not contain the call to the structure's forth feature, as our example in traditional form did. Neither do you see the call to start nor the check of off in the exit condition. The iteration form abstracts these for you, relieving you of their burden ... while eliminating some opportunities for error. @@ -165,12 +167,67 @@ Notice also that the call "print (ic.item)"" accesses the current i Concerning cursors, both ways of using the loop construct to traverse a structure employ a cursor. In the traditional usage, the cursor is internal to the structure object. In the case of the example, that would be the instance of LINKED_LIST [STRING] called my_list. Applying the feature item to my_list retrieves the list element currently referenced by the cursor. In the iteration version of traversal, the variable ic holds the iteration cursor, external to the list object. So, you apply ic.item to get the current list element. The advantage to the external cursor is that multiple traversals of the structure can occur simultaneously without interfering with one another. This is possible in the traditional usage, but only by saving and restoring the structure's cursor. - ====The ''iteration'' form as a boolean expression==== -In ''Loop example 2'', the loop behaves as an instruction. But it is possible to have the iteration loop form behave as a boolean expression. This is helpful in cases in which you might want to ask a question that can only be answered by traversing all or part of a structure. +In ''Loop example 2'', the loop behaves as an instruction. But it is possible to have the iteration loop form behave as a boolean expression. This is helpful in cases in which you might want to ask a question that can be answered by traversing all or part of a structure. + +To get this effect, you use the iteration form with one of two alternative body notations, the ''all body part'' or the ''some body part'' in place of the ''loop body''. When you use either of these notations, the ''body'' is a boolean expression. So, the forms for these body parts are: + + + all boolean_expression + +''all body part.'' + + + some boolean_expression + +''some body part.'' + + +So, to know if all the strings in my_list have lengths greater than three characters, we could code: + + + across my_list as ic all ic.item.count > 3 end + +''Loop example 3.'' + + +To know if at least one string in my_list has a length greater than three characters, we would use the ''some body part'': + + + across my_list as ic some ic.item.count > 3 end + +''Loop example 4.'' + +Of course you can use loops like the two above as you would any boolean expression, in a [[#Conditional|conditional]], for example. + + +====Loop anatomy and validity==== + +Now that we've seen examples of the two forms of loops and broken down their component parts, we're ready to examine the anatomy of the entire construct. You may remember from the beginning of this discussion of loops that the flexibility of the construct lies in its ability to use or omit its various parts to gain certain effects. + +Here are all the possible loop parts, most of which we've seen in examples, in the order in which they must appear when we code them: + +{| border="2" +! This loop part: !! Looks like this: +|- +| ''Iteration part'' || across my_list as ic +|- +| ''Initialization part'' || from my_list.start +|- +| ''Invariant part'' +|- +| ''Exit condition part'' || until my_list.off +|- +| ''Body part'' +|- +| ''Variant part'' +|- +| ''end part'' || end +|} + +====Loop invariants and variants==== -To get this effect, you use the iteration form with one of two alternative body notations, the ''all body'' or the ''some body'' in place of the ''loop body''. ===Debug===