Unit 2 · Lesson 5 Beginner ⏱ ~35 minutes

Loops and Repetition

One of the most powerful things a computer can do is repeat an action many times quickly. Instead of writing the same code over and over, you use a loop — a block of code that runs repeatedly until a condition is met. HyperXTalk has several loop types, each suited to different situations.

🎯 Learning Objectives
  • Use repeat X times to run code a fixed number of times
  • Use repeat with to loop with a counter variable
  • Use repeat while to loop until a condition becomes false
  • Use repeat for each to iterate over a list of items
  • Use exit repeat to break out of a loop early

🔁 repeat X times

The simplest loop runs a fixed number of times. Use it when you know exactly how many repetitions you need:

on mouseUp local tCounter repeat 10 times add 1 to tCounter end repeat answer tCounter end mouseUp
Script editor showing a repeat X times loop
repeat 10 times — the answer dialog will show 10 — click to enlarge

The code inside runs exactly 10 times and then execution continues after end repeat. In this case tCounter will be 10 when the dialog appears.

💡 Notice local tCounter at the top — this explicitly declares a local variable. In HyperXTalk this is optional (you can just use a variable without declaring it) but it's good practice as it makes your intent clear and helps the Debugger.

🔢 repeat with

repeat with gives you a counter variable that automatically increments with each iteration. It's the loop to reach for when you need to know which iteration you're on:

on mouseUp local tCounter local tResults repeat with tCounter = 1 to 10 add 1 to tResults end repeat answer tResults end mouseUp
Script editor showing a repeat with loop
repeat with — tCounter is available inside the loop body — click to enlarge

tCounter starts at 1, and after each iteration HyperXTalk automatically adds 1 to it. When it exceeds 10, the loop ends. You can use tCounter inside the loop body — for example to build a numbered list or access items by index.

You can also count down using down to:

repeat with tCounter = 10 down to 1 -- tCounter goes 10, 9, 8 ... 1 end repeat

⏳ repeat while

repeat while keeps looping as long as a condition remains true. It's the right choice when you don't know in advance how many iterations you'll need:

on mouseUp local tCounter repeat while tCounter < 10 add 1 to tCounter end repeat answer tCounter end mouseUp
Script editor showing a repeat while loop
repeat while — loops until the condition becomes false — click to enlarge

HyperXTalk checks the condition before each iteration. If it's already false when the loop starts, the body never runs at all.

⚠️ Make sure something inside the loop will eventually make the condition false — otherwise you'll have an infinite loop that freezes HyperXTalk. If that happens, press Cmd+. (macOS) or Ctrl+. (Windows/Linux) to stop execution.

📋 repeat for each

repeat for each is the most natural way to process a list. It steps through every item in a comma-separated list, assigning each one to a variable in turn:

on mouseUp local tItems put "car,purse,soda" into tItems local tItem repeat for each item tItem in tItems answer tItem end repeat end mouseUp
Script editor showing a repeat for each loop over a list
repeat for each item — steps through the list one item at a time — click to enlarge

This will show three dialogs in sequence — "car", then "purse", then "soda". You can also iterate over words or lines:

-- Loop over words (separated by spaces) repeat for each word tWord in "Hello World HyperXTalk" answer tWord end repeat -- Loop over lines (separated by return characters) repeat for each line tLine in field "Notes" -- process each line of the field end repeat
🟢 Iterating over the lines of a field is one of the most common patterns in HyperXTalk — it's how you process multi-line data entered by a user or loaded from a file.

🚪 exit repeat

Sometimes you need to stop a loop early — for example, when you've found what you were looking for. Use exit repeat to jump straight to the code after end repeat:

-- Find the first number over 50 repeat with tNum = 1 to 100 if tNum > 50 then answer "First number over 50 is: " & tNum exit repeat end if end repeat

Without exit repeat, the loop would continue all the way to 100 even though we already found what we needed. Using it makes your scripts faster and more efficient.

🛠️ Exercise — Times Table Generator

In this exercise you'll build a times table generator using a repeat with loop. Click any thumbnail to enlarge it.

  1. Create a new stack. Add a Field named NumberInput for the user to enter a number, a Button labelled Generate, and a scrolling Field named Output with Lock text enabled to display the results.
  2. Edit the script of the Generate button:
    button "Generate"
    on mouseUp put the text of field "NumberInput" into tNumber if tNumber is empty then answer "Please enter a number first." exit mouseUp end if put empty into tOutput repeat with tCount = 1 to 12 put tNumber & " x " & tCount \ & " = " & (tNumber * tCount) \ & return after tOutput end repeat set the text of field "Output" to tOutput end mouseUp
    repeat with loop
    repeat with loop
  3. Switch to Browse mode, type a number like 7 and click Generate. The Output field should show the 7 times table from 1 to 12.

Bonus challenge: Add a second field where the user can enter how far the times table should go (instead of always stopping at 12). Use the text of field "LimitInput" in place of the hardcoded 12.

📝 Review Questions
Question 1
Which loop type would you use when you know exactly how many times you want to repeat something?
repeat X times — e.g. repeat 10 times. Use this when the number of repetitions is fixed and known in advance.
Question 2
What is the difference between repeat with and repeat X times?
repeat with gives you a counter variable that's available inside the loop body and increments automatically. repeat X times just repeats without giving you access to which iteration you're on.
Question 3
When does repeat while check its condition — before or after each iteration?
Before each iteration. If the condition is already false when the loop is first reached, the body never runs at all. This also means you need to make sure something inside the loop will eventually make the condition false, or the loop will run forever.
Question 4
What separates items in a list when using repeat for each item?
Commas — e.g. "car,purse,soda". For repeat for each word it's spaces, and for repeat for each line it's return characters (line breaks).
Question 5
What command do you use to stop a loop before it has finished all its iterations?
exit repeat — it immediately jumps to the code after end repeat, skipping any remaining iterations.