Matching multiple cases in a switch

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
nextyoyoma
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 37
Joined: Sat Oct 06, 2012 6:29 am

Matching multiple cases in a switch

Post by nextyoyoma » Wed Aug 28, 2013 8:03 pm

Let's say I have the following:

Code: Select all

set the cColor of btn "btn1" to "blue"
set the cShape of btn "btn1" to "round"

switch
	case the cColor of btn "btn1" is "blue"
		answer "it's blue"
		break
	case the cShape of btn "btn1" is "round"
		answer "it's round"
		break
	default
		answer "It's neither blue nor round"
end switch
The switch will never execute the second case script because the switch exits once the first condition is satisfied. If I want each condition to be checked, should I just do standalone if statements? like:

Code: Select all

if the cColor of btn "btn1" is "blue" then answer "it's blue"
if the cShape of btn "btn1" is "round" then answer "it's round"
Or is there a better way to do that?

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Contact:

Re: Matching multiple cases in a switch

Post by dave_probertGA6e24 » Wed Aug 28, 2013 10:09 pm

Using the switch statement in the way of your example requires the 'case' parts to return true, and it's basically the same as the if option you presented.

I would recommend using the switch based on a specific factor like this:

Code: Select all

switch the cColor of btn "btn1"
   case "blue"
      put "it's blue" into coltxt
      break
  case "red"
      put "it's red" into coltxt
      break
  default
    put "" into coltxt
end switch

switch the cShape of btn "btn1"
  case "round"
      put "it's round" into shptxt
      break
  default
    put "" into shptxt
end switch

if coltxt is not empty then
  put coltxt after output
end if
if shptxt is not empty then
  if output is not empty then put comma&space after output
  put shptxt after output
end if
if output is empty then put "There is no colour or shape" into output
answer output
That's a lot longer, but very flexible when there are a lot of choices. If there are only a few choices though - then a bunch of if statements is likely to be easier to handle.

It all depends on what you are trying to achieve.

Cheers,
Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

Post Reply