Skip to content

Branching and Looping

Matt DeFano edited this page Mar 9, 2019 · 2 revisions

If-Then-Else | Repeat

HyperTalk supports simple conditional branching (if-then-else; but no concept of switch/case), plus a very flexible syntax for looping.

Conditional branching

Conditionals have the following syntax:

if <expression> then
   <statementList>
[else
   <statementList>]
end if

For example,

if 1 < 2 and 3 < 4 then
  answer "This is true!"
end if
if the first line of field "Greeting" contains "hello" then
  put "Hello" into the message box
else
  put "Goodbye" into the message box
end if

Single-statement branches may appear on the same line as the if or else, clause:

if the first word of the long date is not "Friday" then answer "Don't you wish it were Friday?"

To address the dangling else problem, HyperTalk does not support a multiline else-if construct. That said, nesting complex conditional logic can be achieved by nesting if statements. For example:

ask "Yes, no or maybe?" with ""

if it is "yes" then
  answer "Thank you for your support."
else
  if it is "maybe" then
    answer "Make up your mind already."
  else
    answer "We never liked you anyway."
  end if
end if

Loop Constructs

HyperTalk provides a variety of looping constructs. The overall syntax for each of them is

repeat <repeat-condition>
  <statement-list>
end repeat

At any point in the loop, the next repeat command may be used to terminate the current iteration (that is, skip all subsequent statements in <statement-list>) and continue looping. Similarly, the exit repeat command can be used to terminate the loop entirely, returning control to the next statement in the handler after end repeat.

Repeat forever

repeat forever

Executes the enclosed statement-list forever. Sort of. Type cmd-. or ctrl-. at anytime to break execution of the loop. Note that WyldCard intelligently manages thread priority within an infinite loop; creating an infinite loop does not "lock up" the application.

-- Count to infinity (and beyond!)
repeat forever
  add 1 to the message
end repeat

Repeat until

repeat until <boolean-expression>

Executes the enclosed statement-list until the Boolean expression is true; if the expression is initially true, the statement-list will not be executed.

-- Make this part follow the mouse
repeat until the mouse is down
  set the location of me to the mouseLoc
end repeat

Repeat while

repeat while <boolean-expression

Executes the enclosed statement-list as long as the Boolean expression remains true; if the expression is initially false, the statement-list will not be executed.

-- Repeatedly send message to card while mouse hovers
repeat while the mouse is within the rect of me
  send hovering to this card
end repeat

Repeat for

repeat for <numeric-expression> [times]

Executes the enclosed statement-list a pre-determined number of times.

-- Beep three times
repeat for 3 times
  beep
end repeat

Repeat with

repeat with <container> = <expression> [down] to <expression>

Executes the enclosed statement-list for as long as the first expression remains less than the second expression (or vice versa when using down to). Increments the first expression by one each time the loop executes and places the incremented value into the given container (decrements when using down).

-- Hides all buttons and fields on this card
repeat with n = 1 to the number of card parts
  hide card part n
end repeat
-- Shows all buttons and fields on this card
repeat with n = the number of card parts down to 1
  show card part n
end repeat
Clone this wiki locally