Problem with switch

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Problem with switch

Post by bjb007 » Wed Jul 16, 2008 5:26 am

I have some code to check the contents
of variables tVar1[Line01] to tVar1[Line11].
The contents of the elements of tVar1 are
either 1 or 0. There can be one or two cases
where the variable is 1.

The switch code is

case tVar[Line01] > 0
do the biz
...
case tVar[Line11] > 0
do the biz

Problem is that if Line07 is 1 the code is run OK
but then all the following case statements
code is run even though the variable value is 0.
So in a case where Line6 and Line7 are set to
1 the result I get is 6 and 11 instead of 6 and 7.

Seems that after finding one case where
the variable is 1 it ignores the "case" condition
for all the remaining case statements.

Can't use "break" after the first "1" value is
found since the second case won't be checked.

Any ideas?
Life is just a bowl of cherries.

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Wed Jul 16, 2008 10:50 am

Hi bjb007
Seems that after finding one case where
the variable is 1 it ignores the "case" condition
for all the remaining case statements.
If you stick to if...then statements in this instance, you shouldn't have any probs, I reckon, i.e:

Code: Select all

if tVar[Line01] = 1 then
--do the biz
end if
if tVar[Line02] = 1 then
--do the biz
end if
Sandwiching the above between lock screen and unlock screen might help as well...

Hope I've given you good advice there!

:)

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Problem with switch

Post by bjb007 » Wed Jul 16, 2008 2:21 pm

gyro

Got bored with if...end if statements and
decided to have a change!

Guess that "switch" is only intended for
picking one option from a number
of choices. Hence the "break" word.

Wonder are their any other ways of making
multiple choices than if..end if.
Life is just a bowl of cherries.

malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Contact:

Post by malte » Wed Jul 16, 2008 3:01 pm

you can have more than one case.

case tVar[1]>0
case tVar[11]>0
doTheBiz
break
default
doOtherStuff
break

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Wed Jul 16, 2008 3:08 pm

That's useful info from Malte, I've learned something there! (I've tended to steer clear of case stuff, in preference to if...then, don't know why...)

Just thought I'd add (but I'm sure you know anyway) that when you do use multiple if...then statements, they can be simplified if put on one line, so without any need for the "end if":

Code: Select all

if tVar[Line01] = 1 then --do the biz--
if tVar[Line02] = 1 then --do the biz --
:)

malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Contact:

Post by malte » Thu Jul 17, 2008 8:35 am

Maybe a tad bit early to post.
However, there is a small article on switch coming up in the developers section of my site. Click here to view it.

Post Reply