Previous Section Next Section

6.3 Iterative Actions

There are four types of iterative actions in e:

This section discusses these looping actions.

6.3.1 For Loop

The syntax of an e style for loop is as follows:

for var-name from from-exp [down] to to-exp [step step-exp ]
                                                [do] {action;...};

A for loop executes for the number of times specified by from to keywords. The for loop creates a temporary variable var-name of type int, and repeatedly executes the action block while incrementing (or decrementing if down is specified) its value from from-exp to to-exp in interval values specified by step-exp (defaults to 1). In other words, the loop is executed until the value of var-name is greater than the value of to-exp (or less than the value of to-exp if down is used). Example 6-7 shows various types of the for loop.

Example 6-7 For Loop
Example shows different variations of a for loop
<'
struct temp {
    a: int;
    meth() is {
      for i from 2 to 2 * a do { //Simple for loop
                                 //No need to declare i in an e
                                 //style for loop. Variable i is
                                 //automatically declared/initialized.
         out(i);
      }; // Outputs are 2, 3... 2*a

      for i from 1 to 4 step 2 do { //For loop with step
         out(i);
      }; // Outputs are 1,3

      for i from 4 down to 2 step 2 do { //For loop counting down
         out(i);
      }; // Outputs are 4,2
    }; //end of method meth()
}; //end of struct temp definition.
'>
6.3.1.1 C Style For Loop

A C style for loop is also available in e. The syntax of a C style for loop is as follows:

for {initial-action; bool-exp; step-action} [do] {action; ...};

This for loop executes the initial-action once, and then checks the bool-exp. If the bool-exp is TRUE, it executes the action block followed by the step-action. It repeats this sequence in a loop for as long as bool-exp is TRUE. Example 6-8 shows a C style for loop.

Example 6-8 C Style For Loop
 Example of C style for loop
 <'
 struct temp {
 meth () is {
  var i: int; //Variable i needs to be declared for C style for loop
  var j: int;
  for {i = 0; i < 10; i += 1} do { // C style for loop
    if i % 3 == 0 then {
        continue; //Continue to next iteration
    };
    j = j + i;
    if j > 100 then {
        break; //Break out of the loop
    };
 };
}; //end of method meth()
}; //end of struct temp definition.
'>

6.3.2 For each Loop

When iterating through each element of a list, line in a file, or a file in a directory, it is cumbersome to find out the size of the list and then perform the iteration. The for each loop solves this problem. The syntax for the for each loop is as follows:

for each [type] [(item-name)] [using index (index-name)]
        in [reverse] list-exp [do] {action; ...} ;

The for each loop executes the action block for each item in list-exp if its type matches type. Inside the action block, the implicit variable it (or the optional item-name) refers to the matched item, and the implicit variable index (or the optional index-name) reflects the index of the current item. If reverse is specified, list-exp is traversed in reverse order, from last to first. The implicit variable index (or the optional index-name) starts at zero for regular loops and is calculated to start at "(list.size() - 1)" for reverse loops. Example 6-9 shows various for each loops.

Example 6-9 For each Loops
Example of for each loops
<'
extend sys {
    do_it() is {
        var numbers := {8; 16; 24};
        for each in numbers { //for each loop
            print index; //Prints 0,1,2
            print it; //Prints 8, 16, 24
        };

        var sum: int; //Default initial value sum = 0
        for each (n) in numbers { //The for each loop can be applied
                                  //to a list of any type.
            print index; //Prints 0,1,2
            sum += n;
            print sum; //Prints the accumulated total
        };//Value of sum at end of loop is 48

    }; //End of method do_it()
 }; //End of struct extension
'>

6.3.3 While Loop

The syntax for the while loop is as follows:

while bool-exp [do] {action; ... };

The while loop executes the action block repeatedly in a loop while bool-exp is TRUE. Example 6-10 shows the usage of while loops.

Example 6-10 While Loops
Example of while loops
<'
define SMAX 200;
extend sys {
    ctr: uint;
    ctr_assn() is {
        var i: uint;//Default initial value of i = 0
        i = 100; //i is assigned a value 100
        while i <= SMAX { //Loop while value of i <= SMAX
            print ctr;
            ctr = ctr + 10;
            i+=1;
        };
    };
}: //end of sys extension
'>

6.3.4 Repeat Loops

The syntax for repeat loops is as follows:

repeat {action; ... } until bool-exp ;

A repeat loop executes the action block repeatedly in a loop until bool-exp is TRUE. A repeat until action performs the action block at least once. A while action might not perform the action block at all. Example 6-11 shows the usage of repeat loops.

Example 6-11 Repeat Loops
Example of repeat loops
<'
struct temp {
    i: int;
    meth() is {
      repeat {
          i+=1; //Increment i
          print i; //Print the value of i
      } until i==3; //until the value of i == 3
    }; //end of method meth()
}; //End of struct temp
'>
Previous Section Next Section