Felice
Felice

Reputation: 65

Can I use OR in a CASE statement?

Is there a way to use OR in a Case statement? In this example, if phoneType is one of several values, I want to call a function. Do I need to list every possible value with a separate WHEN statement?

    ```CASE ttPhoneData.phoneDescription:
            WHEN "Cell" OR "PASTORS PHONE" OR "Alternative" THEN
                RUN CreatePhone("Mobile", TRUE, ttPhoneData.phone).
            WHEN "Business" OR "ChurchPhone" OR "ChurchPhone2" THEN
                RUN CreatePhone("Work",TRUE,ttPhoneData.phone).
            WHEN "Home" THEN 
                RUN CreatePhone("Home",TRUE,ttPhoneData,phome).
        END CASE.``` 

Upvotes: 0

Views: 551

Answers (2)

Tom Bascom
Tom Bascom

Reputation: 14020

The syntax is kind of wordy and you do need a WHEN for every value:

define variable test as character no-undo.

do while true:

  update test.

  case test:
    when "a" or when "b" then
      message "a or b".
    when "c" then
      message "c".
    when "d" or when "e" then
      message "d or e".
    when "z" then
      leave.
    otherwise
      message "something else".
  end.

end.

If you want something less wordy try this:

define variable test as character no-undo.

do while true:

  update test.

  if lookup( test, "a,b" ) > 0 then
      message "a or b".
   else if lookup( test, "c" ) > 0 then
      message "c".
   else if lookup( test, "d,e" ) > 0 then
      message "d or e".
   else if lookup( test, "z" ) > 0 then
      leave.
   else
      message "something else".

end.

or

define variable test as character no-undo.

do while true:

  update test.

  if ( test = "a" or test ="b" ) then
      message "a or b".
   else if ( test = "c" ) then
      message "c".
   else if ( test = "d" or test = "e" ) then
      message "d or e".
   else if ( test = "z" ) then
      leave.
   else
      message "something else".

end.

Upvotes: 4

nwahmaet
nwahmaet

Reputation: 3909

Yes, you were very close. See doc at https://docs.progress.com/bundle/openedge-abl-reference-122/page/CASE-statement.html?labelkey=product_openedge_122

CASE ttPhoneData.phoneDescription:
  WHEN 'cell' OR
  WHEN 'alternative' THEN
      . // do something
  WHEN 'business' OR
  WHEN 'churchphone' THEN
     . // somethingg else
  WHEN 'home'  THEN
     . //another thing
END CASE.

Upvotes: 4

Related Questions