Page 1 of 1
Using the Switch and Case control structure? - Solved
Posted: Mon Aug 07, 2017 11:32 am
by DR White
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
Re: Using the Switch and Case control structure?
Posted: Mon Aug 07, 2017 11:45 am
by [-hh]
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.]
Re: Using the Switch and Case control structure?
Posted: Mon Aug 07, 2017 12:28 pm
by DR White
-hh,
That works good.
Thanks,
David
Re: Using the Switch and Case control structure? - Solved
Posted: Mon Aug 07, 2017 3:23 pm
by dunbarx
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
Re: Using the Switch and Case control structure? - Solved
Posted: Mon Aug 07, 2017 4:38 pm
by [-hh]
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
Re: Using the Switch and Case control structure? - Solved
Posted: Mon Aug 07, 2017 8:49 pm
by SparkOut
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.
Re: Using the Switch and Case control structure? - Solved
Posted: Mon Aug 07, 2017 9:16 pm
by dunbarx
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
Re: Using the Switch and Case control structure? - Solved
Posted: Tue Aug 08, 2017 8:22 pm
by [-hh]
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")?
Re: Using the Switch and Case control structure? - Solved
Posted: Tue Aug 08, 2017 8:37 pm
by jacque
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
Re: Using the Switch and Case control structure? - Solved
Posted: Tue Aug 08, 2017 9:30 pm
by [-hh]
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 ...

Re: Using the Switch and Case control structure? - Solved
Posted: Tue Aug 08, 2017 10:28 pm
by jacque
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
Re: Using the Switch and Case control structure? - Solved
Posted: Tue Aug 08, 2017 10:43 pm
by [-hh]
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.
Re: Using the Switch and Case control structure? - Solved
Posted: Wed Aug 09, 2017 2:53 am
by bwmilby
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...
Re: Using the Switch and Case control structure? - Solved
Posted: Wed Aug 09, 2017 6:50 am
by SparkOut
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. )
Re: Using the Switch and Case control structure? - Solved
Posted: Sat Aug 12, 2017 3:20 am
by mwieder
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