switch case question

Kay C Lan lan.kc.macmail at gmail.com
Tue Oct 31 05:22:46 EST 2006


On 10/22/06, Martin Baxter <mb.userev at harbourhosting.co.uk> wrote:

> put gtimedtest is empty into isempty
> switch isempty
>   case true
>    stuff
>    break
>   case false
>    other stuff
>    break
> end switch

I've come into this a little late, no internet for a week or so, but
be aware that switch is slower than if-then-else-end if, no matter who
many nested ifs are required to do 'the same' as a mult-case switch
statement. It's only a couple of milliseconds over 1000 executions,
but it is slower.

If what you want is as stated, then I think you'd be better served by:

if (gTimeTest is empty) then
  --do empty stuff
else
  --do full stuff
end if

Having said that, I've started to use switch a lot because what
appears at first glance to be a simple yes/no,true/false,black/white,
after a week of tweaking I realise that yes/no/maybe,true/false/no
comment, and black/white and all the other colours of the rainbow also
need to be considered: and this is where switch is so easy to read and
understand.

Here is another version for your switch command:

switch
 case (gTimeTest is empty)
   --do empty stuff
 break
 default
   -- do full stuff
end switch

Which then makes it easy when you decide to do things based on the
exact contents of gTimeTest

switch
 case (gTimeTest = "1:00")
    revSpeak "It's one o clock"
 break
 case (gTimeTest = "2:00")
    revSpeak "It's too o clock"
 break
 ...
 ...
 case (gTimeTest contains ":30")
   beep
 break
 case (gTimeTest is not empty)
    --do whatever
 default
    answer "There is an error, gTimeTest is empty"
end switch



More information about the Use-livecode mailing list