Re: I don't understand the SWITCH command
Jan Schenkel
janschenkel at yahoo.com
Tue Jan 13 08:17:20 EST 2004
--- François Cuneo <francois.cuneo at cuk.ch> wrote:
> Hello everybody!
> I have a problem with the "Switch" command.
> Here is my little Code:
>
> on mouseup
> put word 2 of the selectedline of me into nbcalc
>
> switch nbcalc
>
> case 1
> put 0 into fld "nbcalc"
>
> case 2
>
> put 2 into fld "nbcalc"
> end switch
>
> end mouseup
>
> It's always the case 2 that's done, even if the
> nbcalc is 1!
> What's wrong please?
>
>
> Amicalement
> François
>
Bonjour François,
You'll need to insert a 'break' command at the right
spots ; so your script should look like :
on mouseup
put word 2 of the selectedline of me into nbcalc
switch nbcalc
case 1
put 0 into fld "nbcalc"
break
case 2
put 2 into fld "nbcalc"
break
end switch
end mouseup
If you don't use a 'break' command, the engine will
just execute the rest of the script in each case until
it bumps into a break.
It has its advantages, for instance when you need to
do something extra in special cases, but it can be a
bit confusing if you don't have a background in C.
Example :
--
switch tNumber
case 3
-- only do the following
...
break
case 2
-- do something special, as well as the code
-- for case tNumber = 1
...
-- so no 'break' at this point
case 1
-- do what needs done in this case
...
-- and break since we don't want the default
break
default
-- if none of the other cases are true
...
-- the break isn't necessary, but cleaner code
break
end switch
--
Hope this helped,
Jan Schenkel.
=====
"As we grow older, we grow both wiser and more foolish at the same time." (La Rochefoucauld)
__________________________________
Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
http://hotjobs.sweepstakes.yahoo.com/signingbonus
More information about the use-livecode
mailing list