Progress
Language Tutorial
for Character


Using the CASE Statement

Another means of conditional control is the CASE statement. Use the CASE statement when you want to test the result of an expression against several values. You could use nested IF statements, but the CASE statement is much clearer.

This is the syntax for CASE.

SYNTAX
CASE expression :
  { WHEN value [ OR WHEN value ] ...
    THEN { block | statement }
  } ...
  [ OTHERWISE { block | statement } ]
END [ CASE ] 

Follow these steps to see how a CASE statement and a selection list can work together:

  1. Open lt-06-05.p and run it. The display shown below appears:
  2. The selection list allows you to choose a chapter. A short description of the chapter then appears in the editor widget.

  3. Use ¦ and Ø to move the highlight bar in the selection list.
  4. Choose Exit, then press SPACEBAR to return to the Procedure Editor.

This is code that created the display:

lt-06-05.p
      /**********  DEFINE WIDGETS  **********/
/*1*/  DEFINE VARIABLE Chapter AS CHARACTER LABEL "Description"
        VIEW-AS EDITOR INNER-CHARS 25 INNER-LINES 6.
/*2*/  DEFINE VARIABLE Tut-List AS CHARACTER LABEL "Tutorial" 
        INITIAL "Chapter 7" VIEW-AS SELECTION-LIST INNER-CHARS 12 
        INNER-LINES 6 LIST-ITEMS "Chapter 7", "Chapter 8", "Chapter 9", 
        "Chapter 10", "Chapter 11", "Chapter 12".
      DEFINE BUTTON btn-Exit LABEL "Exit".
  
       /**********  DEFINE FRAMES  **********/                 
      DEFINE FRAME Frame1
        Tut-List AT ROW 2 COLUMN 2 Chapter AT ROW 2 COLUMN 30
         btn-Exit AT ROW 12 COLUMN 2
           WITH SIDE-LABELS TITLE "Tutorial: Coming Attractions" CENTERED
              THREE-D.

      /**********  DEFINE TRIGGERS  **********/
      ON VALUE-CHANGED OF Tut-List DO:    
/*3*/     CASE Tut-List:SCREEN-VALUE:
/*4*/      WHEN "Chapter 7" THEN 
             ASSIGN Chapter = "Thoroughly covers the use, syntax, and " +
                       "programming techniques of each data widget.".
           WHEN "Chapter 8" THEN 
             ASSIGN Chapter = "Covers the Progress data handling " +
                              "statements.".
           WHEN "Chapter 9" THEN 
             ASSIGN Chapter = "Covers record selection, sorting, and " +
                              "relating.".
           WHEN "Chapter 10" THEN 
             ASSIGN Chapter = "Summarizes what you already know about " +
                              "frames and introduces some new options.".
           WHEN "Chapter 11" THEN 
             ASSIGN Chapter = "Shows you how to define and use a " +
                              "menu bar, menus, and menu items.".
           WHEN "Chapter 12" THEN 
             ASSIGN Chapter = "Covers the 4GL features used to create " +
                              "reports.".
           END CASE.                         
          DISPLAY Chapter WITH FRAME Frame1.
       END.

      /**********  MAIN LOGIC  *********/
      Tut-List:SCREEN-VALUE = "Chapter 7".
      ENABLE ALL WITH FRAME Frame1.
      APPLY "VALUE-CHANGED" TO Tut-List IN FRAME Frame1.
      WAIT-FOR CHOOSE OF btn-Exit. 

These notes help explain the code:

  1. This DEFINE VARIABLE sets up the editor widget.
  2. This DEFINE VARIABLE statement sets up the selection list using the VIEW-AS phrase.
  3. This syntax makes the screen value of the selection list the conditional expression of the CASE statement.
  4. For each value of the selection list, the branches of the CASE statement assign an appropriate value to the editor widget.
  5. Practice Problems

    These problems give you some practice with conditional processing.

    Problem 6-4: lt-06-s4.p

    Using the IF statement, identify the number of customers that have BBB for their sales rep, the number of customers that have the sales rep SLS, and the number of customers that have any other sales rep. Use the Customer.Sales-Rep field.

    Problem 6-5: lt-06-s5.p

    Do the same thing as in Problem 6-4, except use the CASE statement.


Copyright © 2004 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095