
Iterative Control
There are four types of iterative actions in e:
While
repeat until
for each in
for from to
for
while bool-exp [do] {action; ...}
Syntax
Example – while
1 <'
2 extend sys {
3 run() is also {
4 var i : int = 0;
5 while (i < 10) {
6 outf("Current value of i : %x\n",i);
7 i = i + 1;
8 };
9 };
10 };
11 '>
Simulator Output
Current value of i : 0
Current value of i : 1
Current value of i : 2
Current value of i : 3
Current value of i : 4
Current value of i : 5
Current value of i : 6
Current value of i : 7
Current value of i : 8
Current value of i : 9
repeat until
Execute 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.
Syntax
repeat {action; ...} until bool-exp
Example - repeat until
1 <'
2 extend sys {
3 run() is also {
4 var i : int = 0;
5 repeat {
6 outf("Current value of i : %x\n",i);
7 i = i + 1;
8 } until (i == 10);
9 };
10 };
11 '>
Simulator Output
Current value of i : 0
Current value of i : 1
Current value of i : 2
Current value of i : 3
Current value of i : 4
Current value of i : 5
Current value of i : 6
Current value of i : 7
Current value of i : 8
Current value of i : 9
for each in
For each item in list-exp, if its type matches type, execute the action block. 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.
Syntax
for each [type] [(item-name)] [using index (index-name)]
in [reverse] list-exp [do] {action; ...}
Each for each in action defines two new local variables for the loop, named by default it and index. Keep the following in mind:
- If loops are nested one inside the other, the local variables of the internal loop hide those of the external loop. To overcome this hiding, specify the item-name and index-name with unique names.
- Within the action block, you cannot assign a value to it or index-- or to item-name or index-name.
Example - for each in
1 <'
2 struct packet {
3 length: int;
4 keep length < 513;
5 keep length > 63;
6 };
7
8 struct tx_gen {
9 ! packets: list of packet;
10 keep packets.size() == 2;
11 ! packet : packet;
12
13 txgen () is {
14 gen packets;
15 out("--------Generated---------------------");
16 print packets;
17 for each (packet) in packets do {
18 packet_class(packet);
19 };
20 out("--------With it------------------------");
21 for each in packets do {
22 packet_class(it);
23 };
24 out("--------With reverse-------------------");
25 for each in reverse packets do {
26 outf("Current packet position is %d\n", index );
27 packet_class(it);
28 };
29
30 };
31
32 packet_class (pkt : packet) is {
33 outf("Current size of packet : %d\n",pkt.length);
34 };
35 };
36
37 extend sys {
38 U_txgen : tx_gen;
39 run() is also {
40 U_txgen.txgen();
41 };
42 };
43 '>
Simulator Output
--------Generated---------------------
packets =
item type length
---------------------------------------------------------------------------
0. packet 175
1. packet 479
Current size of packet : 175
Current size of packet : 479
--------With it------------------------
Current size of packet : 175
Current size of packet : 479
--------With reverse-------------------
Current packet position is 1
Current size of packet : 479
Current packet position is 0
Current size of packet : 175
for from to
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 (if down is not specified) or until the value of var-name is less than the value of to-exp (if down is specified).
Syntax
for var-name from from-exp [down] to to-exp [step step-exp] [do] {action; ...}
Example - for from to
1 <'
2 extend sys {
3 run() is also {
4 var i : int = 0;
5 out ("Loop 1");
6 for i from 2 to 2 * 4 do {
7 out(i);
8 };
9 out ("Loop 2");
10 for i from 1 to 5 step 3 do {
11 out(i);
12 };
13 out ("Loop 3");
14 for i from 7 down to 1 step 2 do {
15 out(i);
16 };
17 };
18 };
19 '>
Simulator Output
Loop 1
2
3
4
5
6
7
8
Loop 2
1
4
Loop 3
7
5
3
1
for - C style
The for loop works similarly to the for loop in the C language. 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.
- You must enter an initial-action.
- If you use a loop variable within a for loop, you must declare it before the loop (unlike the temporary variable of type int automatically declared in a for from to loop).
- Although this action is similar to a C-style for loop, keep in mind that the initial-action and step-action must be e style actions.
Syntax
for {initial-action; bool-exp; step-action} [do] {action; ...}
Example – for
1 <'
2 extend sys {
3 run() is also {
4 var i : int = 0;
5 var j : int = 0;
6 for {i=0; i < 15; i = i + 1} do {
7 outf("Current value of i : %d\n",i);
8 outf("Current value of j : %d\n",j);
9 if ( i < 3) {
10 continue;
11 } else {
12 j = j + 1;
13 };
14 if (j > 4) {
15 break;
16 };
17 };
18 };
19 };
20 '>
Simulator Output
Current value of i : 0
Current value of j : 0
Current value of i : 1
Current value of j : 0
Current value of i : 2
Current value of j : 0
Current value of i : 3
Current value of j : 0
Current value of i : 4
Current value of j : 1
Current value of i : 5
Current value of j : 2
Current value of i : 6
Current value of j : 3
Current value of i : 7
Current value of j : 4
Bạn Có Đam Mê Với Vi Mạch hay Nhúng - Bạn Muốn Trau Dồi Thêm Kĩ Năng
Mong Muốn Có Thêm Cơ Hội Trong Công Việc
Và Trở Thành Một Người Có Giá Trị Hơn
Mong Muốn Có Thêm Cơ Hội Trong Công Việc
Và Trở Thành Một Người Có Giá Trị Hơn