Header Ads Widget

Case Statements

Case Statements

Switch and case statement is available in a variety of languages. The syntax of case statement is as follows:

  1.     switch E  
  2.                 begin   
  3.                              case V1: S1  
  4.                              case V2: S2  
  5.                              .  
  6.                              .  
  7.                              .  
  8. case Vn-1: Sn-1  
  9. default: Sn  
  10.                 end  

The translation scheme for this shown below:

Code to evaluate E into T

  1. goto TEST  
  2.                 L1:         code for S1  
  3.                               goto NEXT  
  4.                 L2:         code for S2  
  5.                               goto NEXT  
  6.                               .  
  7.                               .  
  8.                               .  
  9.                 Ln-1:      code for Sn-1  
  10.                               goto NEXT  
  11.                 Ln:         code for Sn  
  12. goto NEXT  
  13.                 TEST:      if T = V1 goto L1       
  14.                                if T = V2goto L2  
  15.                                .  
  16.                                .  
  17.                                .  
  18.                                if T = Vn-1 goto Ln-1  
  19.                                goto   
  20. NEXT:  
  • When switch keyword is seen then a new temporary T and two new labels test and next are generated.
  • When the case keyword occurs then for each case keyword, a new label Li is created and entered into the symbol table. The value of Vi of each case constant and a pointer to this symbol-table entry are placed on a stack.

Post a Comment

0 Comments