Review Conditional Flow Control

In

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

 

case bool-case-item
 Evaluates the bool-exp conditions one after the other; executes the action-block associated with the first TRUE bool-exp. If no bool-exp is TRUE, executes the default-action-block, if specified.

 

 

Each of the bool-exp conditions is independent of the other bool-exp conditions, and there is no main case-exp to which all cases refer. This case action has the same functionality as a single if then else action in which you enter each bool-case-item as a separate else if then clause.
 Syntax
 case {bool-case-item; ... [default {default-action; ...}]}
 Example - case bool-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  {

 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

 

  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:

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.

 

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

 

File Iterative Control
In e the way we open and read ASCII files is slightly different then C and C++. There are two types of looping constructs that are provided to help with handling ASCII files.

 

for each line in file

for each file matching

 

for each line in file
Executes the action block for each line in the text file file-name. Inside the block, it (or optional name) refers to the current line (as string) without the final "\n" (the final new line character, CR).

 

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:

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 };

 

 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

Bạn Chưa Biết Phương Thức Nào Nhanh Chóng Để Đạt Được Chúng

Hãy Để Chúng Tôi Hỗ Trợ Cho Bạn. SEMICON  

 

Lần cập nhật cuối ( Thứ ba, 24 Tháng 5 2022 23:36 )