Control Flow

If

An if statement is a conditional statement that executes a block of code if a condition is true. Use the if ... do ... syntax to create an if statement.

let enabled be true

if enabled do
  print "it's enabled!"

You may attach an otherwise block to an if statement to execute code if the condition is false.

let enabled be false

if enabled do
  print "it's enabled!"
otherwise
  print "it's disabled!"

You may chain multiple if otherwise blocks together.

let running be true
let enabled be true

if running do
  print "it's up and running!"
otherwise if enabled do
  print "it's idle!"
otherwise
  print "it's disabled!"

While

A while statement is a loop that executes a block of code as long as a condition is true. Use the while ... do ... syntax to create a while statement.

let remaining_cycles be 10

while remaining_cycles is greater than 0 do
  print "remaining cycles: {remaining_cycles}"
  update remaining_cycles to remaining_cycles minus 1

print "done!"

For Each

A for each statement is a loop that executes a block of code for each element in a collection. Use the for each ... do ... syntax to create a for each statement.

For strings, it iterates over the characters in the string.

let message be "hello world"

for each character in message do
  print character

For lists, it iterates over the elements in the list.

let fruits be list with "apple", "banana", and "orange"

for each fruit in fruits do
  print fruit

For objects, it iterates over the properties in the object.

let person be object with name as "Alice", and age as 22

for each property in person do
  print property
  print person at property

Sparkle doesn't support the traditional for loops you might be familiar with in other languages.