# Trung tâm đào tạo thiết kế vi mạch Semicon

• Đăng ký
 Name: * Tên đăng nhập: * Email: * Mật khẩu: * Verify Password: * Fields marked with an asterisk (*) are required.

## Conditional Flow Control Part-I

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.

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.

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

# 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:09 )