quick.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

   

space.gif

   

space.gif

  ../images/main/bullet_green_ball.gif 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.

   

space.gif

  ../images/main/bulllet_4dots_orange.gif 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.

   

space.gif

Syntax:

   

space.gif

if bool-exp [then] {action; ...} [else if bool-exp [then] {action; ...}] [else {action; ...}]

   

space.gif

Syntax : if

if (condition) {

statements;

};

   

space.gif

Syntax : if-else

if (condition) {

statements;

} else {

statements;

};

   

space.gif

Syntax : nested if-else-if

if (condition) {

statements;

} else if (condition) {

statements;

}

................

................

} else {

statements;

};

   

space.gif

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.

   

space.gif

  ../images/main/bullet_star_pink.gif Example - if then else
   

space.gif


  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 '>
You could download file flow_control1.e here
   

space.gif

Simulator Output

   

space.gif

a = 10 b = 11
a is less then b
c is greater then a
   

space.gif

   

space.gif

  ../images/main/bulllet_4dots_orange.gif 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.

   

space.gif

Syntax

case case-exp {labeled-case-item; ... [default: {default-action; ...}]}

   

space.gif

  ../images/main/bullet_star_pink.gif Example - case labeled-case-item
   

space.gif


  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 '>
You could download file flow_control2.e here
   

space.gif

Simulator Output

   

space.gif

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
   

space.gif

  ../images/main/bulllet_4dots_orange.gif 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.

   

space.gif

Syntax

   

space.gif

case {bool-case-item; ... [default {default-action; ...}]}

   

space.gif

  ../images/main/bullet_star_pink.gif Example - case bool-case-item
   

space.gif


  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 '>
You could download file flow_control3.e here
   

space.gif

Simulator Output

   

space.gif

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
   

space.gif

   

space.gif

   

space.gif

   

space.gif

   

space.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

  

Copyright © 1998-2014

Deepak Kumar Tala - All rights reserved

Do you have any Comment? mail me at:deepak@asic-world.com