Using the Switch and Case control structure? - Solved

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: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
DR White
Posts: 514
Joined: Fri Aug 23, 2013 12:29 pm
Location: Virginia, USA

Using the Switch and Case control structure? - Solved

Post by DR White » Mon Aug 07, 2017 11:32 am

I want to initiate action for different ranges of a single variable. Seems like a good usage for the "Switch" statement, but I cannot make it work. I have looked in the dictionary, but no help in using ranges. I using the following code that does NOT work:

-- Select whcich scroller is active
switch sMslMdX
case (sMslMdX > 5 and sMslMdX < 140)
-- Scroller 1
put 1 into ScrollerSelected
put vOffset into vOffset_1
set the vScroll of group "HelpScroll_1" to vOffset_1
answer "Scroller Case"
break
case (sMslMdX > 175 and sMslMdX < 290)
-- Scroller 2
put 2 into ScrollerSelected
put vOffset into vOffset_2
set the vScroll of group "HelpScroll_2" to vOffset_2
break
end Switch

I am tempted just to use some "If" statements, by I am trying to learn to use "Switches".

Any help be appreciated.

Thanks,

David
Last edited by DR White on Mon Aug 07, 2017 12:29 pm, edited 1 time in total.

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Using the Switch and Case control structure?

Post by [-hh] » Mon Aug 07, 2017 11:45 am

Your switch compares the value of sMslMdX, which is a number, with "true|false"-cases.
Write only "switch" instead of "switch sMslMdX" and it will work.
[You could also think about adding a default case.]
shiftLock happens

DR White
Posts: 514
Joined: Fri Aug 23, 2013 12:29 pm
Location: Virginia, USA

Re: Using the Switch and Case control structure?

Post by DR White » Mon Aug 07, 2017 12:28 pm

-hh,

That works good.

Thanks,

David

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6699
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Using the Switch and Case control structure? - Solved

Post by dunbarx » Mon Aug 07, 2017 3:23 pm

What Hermann said.

Your initial thinking is not invalid, just that you overworked the syntactical limits of the control structure. There are two ways this works. Here is the other way:

Code: Select all

put any item of "3,4" into var
switch var
  case 3
    answer "You have a 3"
    break
  case 4
    answer "You have a 4"
    break
  end switch
In this form, the case instances check the value of var directly. In the variant Hermann gave you, each case establishes its own conditions. You use either as appropriate, but I do not think you can mix them.

Craig Newman

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Using the Switch and Case control structure? - Solved

Post by [-hh] » Mon Aug 07, 2017 4:38 pm

Hi Craig,

he asked for a switch of his cases of ranges of values.
Nevertheless, as nearly always when you come in, it is a subject that is worth to go deeper.

Code: Select all

switch [switchExpression]
    case {caseValue | caseCondition}
        [statementList]
    [default
        defaultStatementList]
end switch
TMHO the dictionary description is incomplete (may be misleading, see David's example above):
From the dictionary entry to switch:
"... In general, when LiveCode enters a switch control structure, it looks for the first case section whose caseValue is equal to the switchExpression, or whose caseCondition is true."
This should read:
"it looks for the first case section whose
caseValue or caseCondition is equal to the switchExpression, or,
if there is no switchExpression,
for the first case whose caseValue or caseCondition is true."

As long there is a switchExpression, the value of the switchExpression is compared to the caseValue or caseCondition (whatever is there).

What is your opinion?

Hermann
shiftLock happens

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

Re: Using the Switch and Case control structure? - Solved

Post by SparkOut » Mon Aug 07, 2017 8:49 pm

I agree Hermann. We know from experience how the switch syntax works with and without switchExpression. As a newbie, I am not at all sure that I could have originally understood the difference without assistance from additional sources beyond the dictionary alone.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6699
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Using the Switch and Case control structure? - Solved

Post by dunbarx » Mon Aug 07, 2017 9:16 pm

Hermann.

I like your wording a lot.

I made a QCC request a while back where I asked for an enhancement to the "switch" control structure:
case1
case2
case3
doThis
break

All well and good. But the following construction, that does not contain any "break" lines at all, works in a way that seems counterIntuitive, and worse, leaves a great deal of potential functionality on the table:

switch
case 1 < 2
doThis
case 2 < 1
doThat
case 1 < 1000
doSomethingElse

Because the first case "fires", and no "break" line is included in that instance, all subsequent cases fire as well. Why this should be so is a mystery to me. I know the dictionary addresses this.

This precludes the ability to assemble multiple cases in order to run any number of conditional results. In other words, it would be terrific to be able NOT to be limited to a single case instance, but rather that all case structures be evaluated on their own merits.

If I had my way, in the above example, "doThis" and "doSomethingElse" would run, but not "doThat". There is no obligstory exit from the switch construct, the case statement conditionals alone controlling the flow. A "break" line could of course still be inserted anywhere depending on the exigencies of the author's intent.

I know it is unlikely that this request will gain traction, either because I am the only one who thinks it important, or because either legacy or structural issues stand against the required effort.
Now THAT is something to discuss. The team thought it was a good idea, but difficult to implement. It languishes.

Craig

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Using the Switch and Case control structure? - Solved

Post by [-hh] » Tue Aug 08, 2017 8:22 pm

This is an interesting argument, but I would prefer to discuss this with a different example. Your example uses cases that are _always_ true or _always_ false, so this is equivalent to

Code: Select all

switch
   case true
     doThis
   case false
     doThat
   case true
     doSomethingElse
end switch
what doesn't make much sense, as it is now, as a use case.

Hopefully I got this correctly:
The very interesting thing you have in mind is a "switchAll" that doesn't ignore all other conditions after the first case that fires but checks always _all_ cases it reaches (until break).
I don't believe this can easily be built into the current switch, but I would second a feature request to have that as an own control structure.

Craig and Sparkout, what's your opinion about such a switch-variant ("Craig's switch")?
shiftLock happens

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5399
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Using the Switch and Case control structure? - Solved

Post by jacque » Tue Aug 08, 2017 8:37 pm

Because the first case "fires", and no "break" line is included in that instance, all subsequent cases fire as well. Why this should be so is a mystery to me.
Well, it's standard syntax in just about every programming language. Changing that would cause LC to be an outlier. Compliancy with the majority of other languages makes LC easier to learn.

Selective omission of breaks allows us to treat one case specially and then fall through to another more generic behavior. Here's a silly example that only allows a button to move if the user types a 1 or a 3, and provides a default if the number isn't one of those:

Code: Select all

on moveBtn pNum
  switch
    case pNum is not in "13"
      put 1 into pNum 
    case pNum = 1
      move btn "animate" relative -10,0
      break
    case pNum = 3
      move btn "animate" relative 10,0
      break
    default
  end switch
end moveBtn
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Using the Switch and Case control structure? - Solved

Post by [-hh] » Tue Aug 08, 2017 9:30 pm

jacque wrote:... Here's a silly example that only allows a button to move if the user types a 1 or a 3, and provides a default if the number isn't one of those:

Code: Select all

on moveBtn pNum
  switch
    case pNum is not in "13"
      put 1 into pNum
    case pNum = 1
      move btn "animate" relative -10,0
      break
    case pNum = 3
      move btn "animate" relative 10,0
      break
    default
  end switch
end moveBtn
If pNum is the key then this button moves certainly with every keydown ... ;-)
shiftLock happens

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5399
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Using the Switch and Case control structure? - Solved

Post by jacque » Tue Aug 08, 2017 10:28 pm

If pNum is the key then this button moves certainly with every keydown ...
Yes, that's right, though I'd use it with a keyUp instead. I told you it was a silly example. :) Perhaps imagine a game where the user types on the keypad to move a character. If they mis-type, they will get the default movement.

The main idea was to show how falling through a case statement can be useful. I've used this method many times, but when I had to think of a simple example I blanked out.

Code: Select all

on moveBtn pNum
  switch
    case pNum is not in "13"
      put 1 into pNum 
    case pNum = 1
      move btn "animate" relative -10,0
      break
    case pNum = 3
      move btn "animate" relative 10,0
      break
    default
  end switch
end moveBtn

on keyUp pKey
  if pKey is not a number then exit to top
  moveBtn pKey
end keyUp
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Using the Switch and Case control structure? - Solved

Post by [-hh] » Tue Aug 08, 2017 10:43 pm

I got it. That's why the smiley was there.

Yes, you are right, what you are nearly always ;-), with the multiple use cases of switch.
Nevertheless: Craig's idea (which I named a "switchAll") allows also such a falling through until the first break. The difference is that it checks all further conditions where it arrives when falling through (the ordinary switch ignores these).

This sounds very clear for me and would be a good supplement to have another "switch"-structure instead of a complicated "if"-structure.
shiftLock happens

bwmilby
Posts: 299
Joined: Wed Jun 07, 2017 5:37 am
Location: Henrico, VA
Contact:

Re: Using the Switch and Case control structure? - Solved

Post by bwmilby » Wed Aug 09, 2017 2:53 am

This would do the same thing in this case (since the value of pNum isn't used after the test):

Code: Select all

on moveBtn pNum
  switch
    case pNum is not in "13"
    case pNum = 1
      move btn "animate" relative -10,0
      break
    ...
Here's a way to get the effect of the switchAll:

Code: Select all

repeat
	if conditionA then
		actionA
	end if
	if conditionB then
		actionB
		exit repeat
	end if
	if conditionC then
		actionC
	else if conditionD then
		actionD
	end if
	exit repeat
end repeat
Not very pretty, but if you need that type of logic...
Brian Milby

Script Tracker https://github.com/bwmilby/scriptTracker

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

Re: Using the Switch and Case control structure? - Solved

Post by SparkOut » Wed Aug 09, 2017 6:50 am

I think a switchAll type structure would be very useful in neatening up convoluted if then else if nested code. It might even encourage newer coders to try switch statements as it seems to have an arcane reputation. All logic can be performed without a switchAll structure though so it would not be something I would choose if it meant compromising something else, such as lazy evaluation. (If a condition is met that resolves the test to true then the remaining tests are ignored and the code block for the true outcome is fired. This is as it should be. switchAll seems to go against that grain, but if this happened only in a switchAll block it really would be a code styling advantage. )

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3013
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Using the Switch and Case control structure? - Solved

Post by mwieder » Sat Aug 12, 2017 3:20 am

Craig-

no changes to LiveCode necessary - here's what your code doing what you want

Code: Select all

switch
  case 1 < 2
    doThis
  case 1 < 1000
    doSomethingElse
    break
  case 2 < 1
    doThat
end switch

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”