SWITCH - can it be used with an expression

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
golive
Posts: 98
Joined: Wed Jul 01, 2015 5:16 am

SWITCH - can it be used with an expression

Post by golive » Sun Sep 06, 2015 3:10 am

Can one use a condition in the CASE statement?

I tried this but it doesn't work.

Code: Select all

     switch tCount
         case tCount <10
            -- do something
            break
         case tCount <20
             -- do something else
            break

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: SWITCH - can it be used with an expression

Post by SparkOut » Sun Sep 06, 2015 7:16 am

Yes, just leave out the reference to tCount in the switch statement and only in the conditional tests

Code: Select all

switch
  case tCount >20
    answer "count is high"
    break
  case tCount <5
    answer "count is low"
    break
  default
    answer "count is just right"
    break
end switch

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: SWITCH - can it be used with an expression

Post by SparkOut » Sun Sep 06, 2015 7:24 am

Also, a switch statement unmatched with a variable like that can check multiple conditions such as

Code: Select all

switch
  case tGender is "male"
    set the cPermission of this stack to false
    answer "Sorry, ladies only"
    break
  case tAge < 18
    set the cPermission of this stack to false
    answer "Sorry, adults only"
    break
end switch
Last edited by SparkOut on Sun Sep 06, 2015 8:49 pm, edited 2 times in total.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: SWITCH - can it be used with an expression

Post by bn » Sun Sep 06, 2015 5:36 pm

Hi golive,

you might also have a look here:

http://forums.livecode.com/viewtopic.php?f=8&t=25082

switch statements can get very tricky.

I mostly use if-then, except when choosing from a larger number of options. Then I use the "switch variable" form. With appropriate break.

If you do conditionals on a variable as done in above link, be careful and know what you are doing. You can get surprising result. Although they are consistent with the logic of Switch case.

Kind regards
Bernd

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: SWITCH - can it be used with an expression

Post by FourthWorld » Sun Sep 06, 2015 6:52 pm

Switch statements are trickiest when you use the case evaluation form without break statements. Beyond that very specific circumstance outlined in the forum discussion Bernd linked to, they're pretty straightforward and very useful for a wide range of circumstances where if-then blocks may be cumbersome or less flexible.

The key here is that you can either evaluate a condition in the switch statement, or within each case statement, but not both.

As SparkOut wrote, in your code you were evaluating the condition with each case statement, so just removing the comparison value from the switch statement addresses the issue you encountered.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

golive
Posts: 98
Joined: Wed Jul 01, 2015 5:16 am

Re: SWITCH - can it be used with an expression

Post by golive » Mon Sep 07, 2015 3:40 am

Very useful info from all, many thanks.

Suggestion: For beginners like me, more info like the above in the Dictionary would be useful :)

Post Reply