Previous Section Next Section

6.2 Conditional Actions

Conditional actions are used to specify code segments that will be executed only if a certain condition is met.

6.2.1 If-then-else

The syntax of if-then-else is shown below:

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

If the first bool-exp is TRUE, the then action block is executed. If the first bool-exp is FALSE, the else if clauses are executed sequentially: if an else if bool-exp is found that is TRUE, its then action block is executed; otherwise the final else action block is executed. Example 6-5 shows various if-then-else actions. Note that the then keyword is optional but recommended.

Example 6-5 If-then-else Actions
Example showing various if-then-else actions
<'
struct test1 {
    a: int;
    b: int;

    meth1() is { //Define a method called meth
     if a > b then { //then keyword is optional
        print a, b;
     } else {
        print b, a;
     };
    }; //end of meth1 definition
}; //end of struct test1 definition

struct test2 {
    a_ok: bool;
    b_ok: bool;
    x: int;
    y: int;
    z: int;

    meth2() is {
     //Complex if-else-if clause
     if a_ok { //Note that then keyword is optional
        print x;
     } else if b_ok {
        print y;
     } else {
        print z;
     };
   }; //end of meth2 definition
}; //end of struct test2 definition
'>

6.2.2 Case Action

The syntax to define a case action is as follows:

case case-exp { label-exp : action-block;...};

The case action executes an action block based on whether a given comparison is true. The case action evaluates the case-exp and executes the first action-block for which label-exp matches the case-exp. If no label-exp equals the case-exp, the case action executes the default-action block, if specified. After an action-block is executed, Specman Elite proceeds to the line that immediately follows the entire case statement. Example 6-6 shows various case actions.

Example 6-6 Case Action
Example shows case actions
<'
struct packet {
    length: int;
};

struct temp {
    packet1: packet; //Instantiate the packet struct

    meth() is {
     case packet1.length { //Case action
    64:          {out("minimal packet")}; //label-exp and action block
   [65..256]:   {out("short packet")};
   [256..512]:  {out("long packet")};
   default:     {out("illegal packet length")};
   };
 }; //end of method meth()
}; //end of struct temp

'>
Previous Section Next Section