Unit 2 · Lesson 4 Beginner ⏱ ~35 minutes

Conditionals and Logic

Scripts that always do the same thing aren't very useful. Real applications need to make decisions — do this if something is true, do that otherwise. In this lesson you'll learn how to use if, else, and logical operators to make your scripts intelligent.

🎯 Learning Objectives
  • Use if / then / end if to run code conditionally
  • Use else to handle the case when a condition is false
  • Use else if to test multiple conditions in sequence
  • Use comparison operators to compare values
  • Combine conditions with and, or, and not

🔀 if / then

The simplest conditional is if / then. It runs a block of code only when a condition is true, and does nothing otherwise:

on mouseUp ask "What is the answer to life, the universe and everything?" if (it = 42) then answer "You got it!" end if end mouseUp
Script editor showing a simple if/then handler
A simple if/then — only runs if the condition is true — click to enlarge

The structure is always:

if condition then
   code to run
end if

If the condition is false, execution skips everything between then and end if and continues with whatever comes after.

💡 In HyperXTalk, = means equals in a condition. This is different from some other languages where you'd use ==. You can also write is instead of = — if it is 42 then reads very naturally!

↔️ if / else

Often you want to do one thing if a condition is true and something different if it's false. That's where else comes in:

on mouseUp ask "What is the answer to life, the universe and everything?" if (it = 42) then answer "You got it!" else answer "Try again." end if end mouseUp
Script editor showing if/then/else
Adding else — now both outcomes are handled — click to enlarge

Now the script always does something — either "You got it!" or "Try again." depending on what the user typed. Exactly one of the two branches will run.

🔢 else if

When you need to test more than two possibilities, chain them together with else if:

on mouseUp ask "Enter a number from 1 to 3:" if it = 1 then answer "You chose one." else if it = 2 then answer "You chose two." else if it = 3 then answer "You chose three." else answer "That's not between 1 and 3!" end if end mouseUp

HyperXTalk tests each condition in order from top to bottom. The moment it finds one that's true, it runs that branch and skips all the rest. If none are true, the else branch runs.

🟢 Always put the most specific or most likely condition first. HyperXTalk stops checking as soon as it finds a match, so the order of your else if branches matters.

⚖️ Comparison Operators

A condition is built using a comparison operator to compare two values. HyperXTalk supports both symbolic and plain-English operators — use whichever reads most naturally:

-- Equals if tScore = 100 then ... -- or: if tScore is 100 then -- Not equal if tName <> "" then ... -- or: if tName is not empty then -- Greater than / less than if tAge > 18 then ... if tAge < 18 then ... if tAge >= 18 then ... -- greater than or equal if tAge <= 65 then ... -- less than or equal -- Text comparisons if tField is empty then ... if tField is not empty then ...

🧠 and / or / not

You can combine multiple conditions in a single if statement using and, or, and not:

and — both conditions must be true:

if tAge >= 18 and tAge <= 65 then answer "Working age!" end if

or — at least one condition must be true:

on mouseUp ask "What is the answer to life, the universe and everything?" if (it = 42) OR (it = "moof") then answer "You got it!" else answer "Try again." end if end mouseUp
Script editor showing if with OR condition
Using OR — accepts either answer — click to enlarge

not — reverses a condition:

if not (tField is empty) then -- same as: if tField is not empty then answer "Field has content!" end if
🟢 Use parentheses to group conditions and make complex logic easier to read: if (tA = 1) and (tB = 2) then is clearer than if tA = 1 and tB = 2 then.
🛠️ Exercise — A Password Checker

In this exercise you'll build a simple password checker that uses conditionals to validate input. Click any thumbnail to enlarge it.

  1. Create a new stack. Add a Field named PasswordInput with the passwordField property enabled in the Property Inspector. Add a Button labelled Check Password and a Field named Result with Lock text enabled.
  2. Edit the script of the Check Password button:
    button "Check Password"
    on mouseUp put the text of field "PasswordInput" into tPassword if tPassword is empty then set the text of field "Result" \ to "Please enter a password." else if tPassword = "HyperXTalk" then set the text of field "Result" \ to "✅ Correct! Welcome." else set the text of field "Result" \ to "❌ Incorrect password." end if end mouseUp
    if/else structure
    if / else structure
  3. Switch to Browse mode and test it — try leaving the field empty, entering the wrong password, and entering HyperXTalk.
  4. Try each of the three cases and check the Result field updates correctly.

Bonus challenge: Add a condition that checks if the password is less than 8 characters and shows a different message — "Password too short." — before checking if it's correct. Hint: the length of tPassword returns the number of characters.

📝 Review Questions
Question 1
What happens if the condition in an if / then statement is false and there is no else branch?
Nothing happens — HyperXTalk skips everything between then and end if and continues with the code that comes after the conditional block.
Question 2
What is the difference between and and or when combining conditions?
With and, both conditions must be true for the overall condition to be true. With or, at least one condition must be true — if either one is true, the block runs.
Question 3
How do you check if a field is empty in HyperXTalk?
if the text of field "MyField" is empty then — or store it in a variable first: put the text of field "MyField" into tValue then if tValue is empty then.
Question 4
In an if / else if / else chain, how many branches can run?
Exactly one — HyperXTalk tests conditions from top to bottom and runs the first branch whose condition is true, then skips all the rest. If no condition is true and there's an else, that runs instead.
Question 5
What are two ways to write "equals" in a HyperXTalk condition?
You can use = (e.g. if tScore = 100 then) or the plain English keyword is (e.g. if tScore is 100 then). Both mean exactly the same thing.