1.

Conditional Flow Control: Conditional flow like in any other programming language is used for controlling execution a block of code, when a condition is true.
2. Content
- Conditional Flow Control Part-I
- Conditional Flow Control Part-II
- Conditional Flow Control Part-III
if then else
if then else is used for checking for a condition and if condition is true, then the block of code inside the {} is excuted.
Syntax
if bool-exp [then] {action; ...} [else if bool-exp [then] {action; ...}] [else {action; ...}]
Syntax : if
if (condition) {
statements;
};
Syntax : if-else
if (condition) {
statements;
} else {
statements;
};
Syntax : nested if-else-if
if (condition) {
statements;
} else if (condition) {
statements;
}
} else {
statements;
};
if then else clause comprises one action, the semicolon comes at the end of the clause and not after each action block within the clause.
Example - if then else
1 <'
2 extend sys {
3 run() is also {
4 var a : int = 10;
5 var b : int = 11;
6 var c : int = 12;
7 outf("a = %d b = %d\n",a,b);
8 if a > b {
9 out("a is greater then b");
10 } else {
11 out("a is less then b");
12 };
13 if (a > b) then { // then is optional
14 out("a is greater then b");
15 } else if (b > c) then {
16 out("b is greater then c");
17 } else if (c > a) then {
18 out("c is greater then a");
19 };
20 };
21 };
22 '>
Simulator Output
a = 10 b = 11
a is less then b
c is greater then a
case labeled-case-item
Execute an action block based on whether a given comparison is true. Evaluates the case-exp and executes the first action-block for which label-case-item matches the case-exp. If no label--case-item equals the case-exp, executes the default-action block, if specified.
Syntax
case case-exp {labeled-case-item; ... [default: {default-action; ...}]}
Example - case labeled-case-item
1 <'
2 struct packet {
3 length: int;
4 keep length < 513;
5 keep length > 63;
6 };
7
8 struct tx_gen {
9 ! packet: packet;
10
11 txgen () is {
12 var i : int = 4;
13 for i from 0 to 4 do {
14 gen packet;
15 packet_class();
16 };
17 };
18
19 packet_class () is {
20 outf("Current size of packet : %d ",packet.length);
21 case packet.length {
22 64: {
23 out("minimal packet");
24 };
25 [65..256]: {
26 out("short packet");
27 };
28 [256..512]: {
29 out("long packet");
30 };
31 default: {
32 out("illegal packet length");
33 };
34 };
35 };
36 };
37
38 extend sys {
39 U_txgen : tx_gen;
40 run() is also {
41 U_txgen.txgen();
42 };
43 };
44 '>
Simulator Output
Current size of packet : 175 short packet
Current size of packet : 479 long packet
Current size of packet : 78 short packet
Current size of packet : 438 long packet
Current size of packet : 158 short packet
Â
Â
Â
Â
1 <'
2 struct packet {
3 length: int;
4 keep length < 513;
5 keep length > 63;
6 };
7
8 struct tx_gen {
9 ! packet: packet;
10
11 txgen () is {
12 var i : int = 4;
13 for i from 0 to 4 do {
14 gen packet;
15 packet_class();
16 };
17 };
18
19 packet_class () is {
20 outf("Current size of packet : %d ",packet.length);
21 case {
22 (packet.length == 64) {
23 out("minimal packet");
24 };
25 (packet.length >= 65 && packet.length <=256) {
26 out("short packet");
27 };
28 (packet.length >=256 && packet.length <= 512) {
29 out("long packet");
30 };
31 default {
32 out("illegal packet length");
33 };
34 };
35 };
36 };
37
38 extend sys {
39 U_txgen : tx_gen;
40 run() is also {
41 U_txgen.txgen();
42 };
43 };
44 '>
Simulator Output
Current size of packet : 175 short packet
Current size of packet : 479 long packet
Current size of packet : 78 short packet.
Iterative Control
There are four types of iterative actions in e:
While
repeat until
for each in
for from to
Â
Â
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 '>
Â
Â
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
Â
Â
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 '>
Â
Â
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 [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
Â
Â
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 };
Â
Â
2
3
4
5
6
7
8
Loop 2
1
4
Loop 3
7
5
3
Â
Â
- 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 '>
Â
Â
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
Â
Â
for each line in file
for each file matching
Â
Â
Syntax :
for each [line] [(name)] in file file-name-exp [do] {action; ...}
Example - for each line in file
Lets consider a ASCII file file.txt containing below lines
Lets write a e program to read this file and print it
1 <'
2 extend sys {
3 run() is also {
4 for each line in file "file.txt" {
5 out (it);
6 };
7 };
8 };
9 '>
Simulator Output
This is first line
This is second line
This is third line
for each file matching
For each file (in the file search path) whose name matches file-name-exp execute the action block. Inside the block, it (or optional name) refers to the matching file name.
Syntax
for each file [(name)] matching file-name-exp [do] {action; ...}
Example - for each file matching
1 <'
2 extend sys {
3 run() is also {
4 for each file (file_name) matching "f*.txt" {
5 outf("Found file %s\n",file_name);
6 for each line in file file_name {
7 out (it);
8 };
9 };
10 };
11 };
12 '>
Simulator Output
Found file file.txt
This is first line
This is second line
This is third line
Controlling the Program Flow
The actions described in this section are used to alter the flow of the program in places where the flow would otherwise continue differently. The e language provides the following actions for controlling the program flow:
- break
- continue
break
Breaks the execution of the nearest enclosing iterative action (for or while). When a break action is encountered within a loop, the execution of actions within the loop is terminated, and the next action to be executed is the first one following the loop.
Syntax
break
continue
Stops the execution of the nearest enclosing iteration of a for or a while loop, and continues with the next iteration of the same loop. When a continue action is encountered within a loop, the current iteration of the loop is aborted, and execution continues with the next iteration of the same loop.
You cannot place continue actions outside the scope of a loop
Syntax
Continue
Example - break and continue 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 };
Â
Â
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