[comp.sys.mac.hypercard] Why no 'case' statement in Hypertalk? Suggestions?

earl@cogdev.cognet.ucla.edu (Earl Williams) (06/05/91)

Is there really no 'case' statement in Hypertalk, or am I missing its
equivalent?  For simple keypress trapping, I'd like to do something like
the following:

on keyDown key
  case
    key = 'a' then do this else
    key = 'b' then do that else
    key = 'c' then do the other
  end
end keyDown

Instead, I have to use standard if-then-else statements which, when
formatted, give the false impression that I'm doing nesting.

on keyDown key
  if key = 'a' then do this else
    if key = 'b' then do that else
      if key = 'c' then do the other
      end if
    end if
  end if
end keyDown

This is extremely inelegant, if not confusing (when used in a more
complex example than this, of course.

Any suggestions?  What am I missing here?  Any help will be
much appreciated.

-----------------------------------------------------------
Earl M. Williams       ?8-)      UCLA Psychology Department
earl@cognet.ucla.edu             2344B Franz Hall
"No matter where you go, there you are."   -Buckaroo Banzai

jdevoto@Apple.COM (Jeanne A. E. DeVoto) (06/06/91)

In article <1991Jun4.234844.1280@cognet.ucla.edu>
earl@cogdev.cognet.ucla.edu (Earl Williams) writes:
>Is there really no 'case' statement in Hypertalk, or am I missing its
>equivalent?  For simple keypress trapping, I'd like to do something like
>the following:
>
>on keyDown key
>  case
>    key = 'a' then do this else
>    key = 'b' then do that else
>    key = 'c' then do the other
>  end
>end keyDown
>
>Instead, I have to use standard if-then-else statements which, when
>formatted, give the false impression that I'm doing nesting.
>
>on keyDown key
>  if key = 'a' then do this else
>    if key = 'b' then do that else
>      if key = 'c' then do the other
>      end if
>    end if
>  end if
>end keyDown
>
>This is extremely inelegant, if not confusing (when used in a more
>complex example than this, of course.

Write your conditional like this:

  if key is "a" then do this
  else if key is "b" then do that
  else if key is "c" then do the other

Or, if your statements are more than one line (even if they're not, if
you're a neatness freak or want complete clarity when using nested ifs):

  if key is "a" then
    do this
  else if key is "b" then
    do that
  else if key is "c" then
    do the other
  end if

You can embellish this with an exception case at the end:

  if key is "a" then
    do this
  else if key is "b" then
    do that
  else if key is "c" then
    do the other
  else
    do something else
  end if

Just one end if, at the end, and no Vogage to the Righthand Edge of
the Script Editor.
-- 
========= jeanne a. e. devoto ========================================
 jdevoto@apple.com     |  You may not distribute this article under a
 jdevoto@well.sf.ca.us |  compilation copyright without my permission.
______________________________________________________________________
 Apple Computer and I are not authorized      |        CI$: 72411,165
 to speak for each other.                     |

fwb@pollux.tmc.edu (Fred Brehm) (06/06/91)

In article <1991Jun4.234844.1280@cognet.ucla.edu> earl@cogdev.cognet.ucla.edu (Earl Williams) writes:
>Is there really no 'case' statement in Hypertalk, or am I missing its
>equivalent? 

The equivalent is "else if"

> ... For simple keypress trapping, I'd like to do something like
>the following:
>
>on keyDown key
>  case
>    key = 'a' then do this else
>    key = 'b' then do that else
>    key = 'c' then do the other
>  end
>end keyDown

on keyDown key
    if key = 'a' then do this
    else if key = 'b' then do that
    else if key = 'c' then do the other
    end if
end keyDown

-- 
Frederic W. Brehm	Siemens Corporate Research	Princeton, NJ
fwb@demon.siemens.com	-or-	...!princeton!siemens!demon!fwb

stadler@Apple.COM (Andy Stadler) (06/07/91)

In article <1991Jun4.234844.1280@cognet.ucla.edu> earl@cogdev.cognet.ucla.edu
 (Earl Williams) writes:

>Is there really no 'case' statement in Hypertalk, or am I missing its
>equivalent?  For simple keypress trapping, I'd like to do something like
>
>  [..hypothetical case statement syntax described..]
>
>Instead, I have to use standard if-then-else statements which, when
>formatted, give the false impression that I'm doing nesting.
>
>on keyDown key
>  if key = 'a' then do this else
>    if key = 'b' then do that else
>      if key = 'c' then do the other
>      end if
>    end if
>  end if
>end keyDown

Instead, type it like this:

on keydown key
  if key is 'a' then dosomething
  else if key is 'b' then dosomethingelse
  else if key is 'c' then doyetanother
end keydown

I think this is what you want.

Andy Stadler
Apple Computer, Inc.

johnston@oscar.ccm.udel.edu (06/07/91)

Still simpler (forget all the else statements):

on keyDown key
  if key is "a" then beep
  if key is "b" then beep 2
  if key is "c" then beep 3
  pass keyDown
end keyDown

Bill (johnston@minnie.me.udel.edu)

Harry.Myhre@p2.f863.n102.z1.fidonet.org (Harry Myhre) (06/07/91)

Earl Williams writes in a message to All on 05 Jun 91

EW> Is there really no 'case' statement in Hypertalk, or am I missing 
EW> its equivalent? 

You're right, HyperTalk doesn't have a case statement. You could have coded
your if statement:

on keyDown key
  if key = 'a' then do this else
    if key = 'b' then do that else
      if key = 'c' then do the other
      end if
    end if
  end if
end keyDown

like this instead:

on keyDown key
     if key = 'a' then
          do this
     else if key = 'b' then
          do that
     else if key = 'c' then
          do the other
     end if
end keyDown

Either way you do it, it's uglier than a nice case statement. And harder to
read and understand what's going on.

 

--  
: Harry Myhre - via FidoNet node 1:102/851 (818)352-2993
: ARPA/INTERNET: Harry.Myhre@p2.f863.n102.z1.fidonet.org
: UUCP: ...!{elroy,elroy!bohica}!mcws!863.2!Harry.Myhre
: Compu$erve: >internet:Harry.Myhre@p2.f863.n102.z1.fidonet.org

bcarter@claven.idbsu.edu (Bruce Carter) (06/07/91)

In article <55655@nigel.ee.udel.edu> johnston@oscar.ccm.udel.edu writes:
>Still simpler (forget all the else statements):
>
>on keyDown key
>  if key is "a" then beep
>  if key is "b" then beep 2
>  if key is "c" then beep 3
>  pass keyDown
>end keyDown

Simpler, but slower.  In this construction all of the if statements have to be
evaluated individually.
                                     <->
Bruce Carter, Courseware Development Coordinator      bcarter@claven.idbsu.edu
Boise State University, Boise, ID  83725              duscarte@idbsu.bitnet
(This message contains personal opinions only)        (208)385-1250@phone

scasterg@magnus.acs.ohio-state.edu (Stuart M Castergine) (06/08/91)

In article <55655@nigel.ee.udel.edu> johnston@oscar.ccm.udel.edu writes:
>Still simpler (forget all the else statements):
>
>on keyDown key
>  if key is "a" then beep
>  if key is "b" then beep 2
>  if key is "c" then beep 3
>  pass keyDown
>end keyDown
>
>Bill (johnston@minnie.me.udel.edu)

But much slower -- you force the script to evaluate all the
expressions, which is time-consuming if there are very many. If you
insert else statements, flow will exit the construct after it finds a
condition that tests true, saving you many needless expression
evaluations. 

-- 
scasterg@magnus.acs.ohio-state.edu	Stuart M Castergine
"Step by step they were led to practices which disposed to vice -- the
lounge, the bath, the elegant banquet. All this in their ignorance
they called civilisation, when it was but part of their servitude." 

EIVERSO@cms.cc.wayne.edu (06/15/91)

 Harry.Myhre@p2.f863.n102.z1.fidonet.org (Harry Myhre) writes

>Earl Williams writes in a message to All on 05 Jun 91

>EW> Is there really no 'case' statement in Hypertalk, or am I missing
>EW> its equivalent?

>You're right, HyperTalk doesn't have a case statement. You could have coded
>your if statement:

>on keyDown key
>  if key = 'a' then do this else
>    if key = 'b' then do that else
>      if key = 'c' then do the other
>      end if
>    end if
>  end if
>end keyDown

>like this instead:

>on keyDown key
>     if key = 'a' then
>          do this
>     else if key = 'b' then
>          do that
>     else if key = 'c' then
>          do the other
>     end if
>end keyDown

>Either way you do it, it's uglier than a nice case statement. And harder to
>read and understand what's going on.

I'd write it like this:

on keyDown
  if key = "a" then doThis
  if key = "b" then doThat
  if key = "c" then doTheOther
end keyDown

Fewer lines, fewer indents, more legible code.
Closer to what you like about a case statement.
I'd save the "else if" for multi line proceedures after an if.
If you want to debate that, be my guest.

A few "nigling" little points...

Double quotes are for containing literal strings, not single quotes.
"Do" and "The" are reserved words.

--Eric

psych@watserv1.waterloo.edu (R. Crispin - Psychology) (06/16/91)

In article <1991Jun14.175048.20051@cs.wayne.edu> EIVERSO@cms.cc.wayne.edu writes:
>I'd write it like this:
>
>on keyDown
>  if key = "a" then doThis
>  if key = "b" then doThat
>  if key = "c" then doTheOther
>end keyDown
>

This is a slower technique since no matter what value KEY has all 3 statements
are executed. The only time this is faster is if key is unassigned. A
better technique is as follows

on keyDown Key
  if key = "a" then doThis
  else if key = "b" then doThat
  else if key = "c" then doTheOTher
end keydown

I ran some tests and this can be up to 25% faster but usually it is 10% faster.
Richard Crispin              Phone:    (519)888-4781
Dept. of Psychology          EMail:    psych@watdcs.uwaterloo.ca 
University of Waterloo                 psych@watserv1.uwaterloo.ca 
Waterloo, Ont.   Canada   N2L 3G1

estier@uni2a.unige.ch (06/19/91)

In article <1991Jun4.234844.1280@cognet.ucla.edu> 
earl@cogdev.cognet.ucla.edu (Earl Williams) writes:
> Is there really no 'case' statement in Hypertalk, or am I missing its
> equivalent?  For simple keypress trapping, I'd like to do something like
> the following:
> 
> on keyDown key
>   case
>     key = 'a' then do this else
>     key = 'b' then do that else
>     key = 'c' then do the other
>   end
> end keyDown

In fact, there is no need of a case statement in HyperTalk since the 
handler mechanism can very conveniently be substituted to it. So instead of
writing borrowing sequences of "if-then-else-end if" for your example, why
not writing:

   on keyDown key
      if key is in "a b c" then send key
   end keyDown

   on a
     do this
   end a

   on b
     do that
   end b

   on c
     do the other
   end c

(Please don't write me that it takes too much time to execute, just wait 
 for the next (hopefully more powerfull)  Mac ;-)

 Thibault

+-----------------------------
| estier@macmail.unige.ch   or
| estier@cui.unige.ch       or
| ESTIER@CGEUGE51           
+-----------------------------