Control Flow

Quartz provides familiar control flow statements for conditional execution and looping.

Conditional Statements

if Statement

let temperature = 75

if (temperature > 80) {
io.println("It's hot!")
}

if-else

let age = 16

if (age >= 18) {
io.println("You can vote!")
} else {
io.println("Too young to vote.")
}

if-else if-else Chain

let score = 85

if (score >= 90) {
io.println("Grade: A")
} else if (score >= 80) {
io.println("Grade: B")
} else if (score >= 70) {
io.println("Grade: C")
} else if (score >= 60) {
io.println("Grade: D")
} else {
io.println("Grade: F")
}

Nested Conditionals

let hasLicense = true
let age = 17

if (age >= 16) {
if (hasLicense) {
io.println("You can drive!")
} else {
io.println("Get a license first.")
}
} else {
io.println("Too young to drive.")
}

Compound Conditions

let age = 25
let hasID = true

// AND: both must be true
if (age >= 21 && hasID) {
io.println("Welcome to the bar!")
}

// OR: at least one must be true
if (age < 13 || age > 65) {
io.println("Discount applies!")
}

// NOT: inverts the condition
if (!isEmpty) {
io.println("Container has items")
}

While Loop

Repeat while a condition is true:

let count = 5

while (count > 0) {
io.println(count)
count = count - 1
}
io.println("Liftoff!")

Output:

5
4
3
2
1
Liftoff!

Infinite Loop with Break

let sum = 0

while (true) {
io.print("Enter a number (0 to stop): ")
let n = int(io.readLine())

if (n == 0) {
break  // Exit the loop
}

sum = sum + n
}

io.println("Sum: " + sum)

For Loop

The classic C-style for loop:

// Basic for loop
for (let i = 0; i < 5; i = i + 1) {
io.println("i = " + i)
}

Output:

i = 0
i = 1
i = 2
i = 3
i = 4

Iterate Over Array

let fruits = ["apple", "banana", "cherry"]

for (let i = 0; i < len(fruits); i = i + 1) {
io.println((i + 1) + ". " + fruits[i])
}

Counting Down

for (let i = 10; i >= 0; i = i - 1) {
io.println(i)
}
io.println("Happy New Year!")

Step by 2

// Even numbers 0-10
for (let i = 0; i <= 10; i = i + 2) {
io.print(i + " ")
}
// Output: 0 2 4 6 8 10

Break and Continue

break

Exit the loop immediately:

// Find first negative number
let numbers = [5, 3, -2, 8, -1, 4]

for (let i = 0; i < len(numbers); i = i + 1) {
if (numbers[i] < 0) {
io.println("Found negative: " + numbers[i])
break
}
}
// Output: Found negative: -2

continue

Skip to the next iteration:

// Print only positive numbers
let numbers = [5, -3, 2, -8, 1, -4]

for (let i = 0; i < len(numbers); i = i + 1) {
if (numbers[i] < 0) {
continue  // Skip negative numbers
}
io.println(numbers[i])
}
// Output: 5 2 1

Nested Loops

// Multiplication table
for (let i = 1; i <= 5; i = i + 1) {
for (let j = 1; j <= 5; j = j + 1) {
io.print((i * j) + "\t")
}
io.println("")
}

Output:

1   2   3   4   5   
2   4   6   8   10  
3   6   9   12  15  
4   8   12  16  20  
5   10  15  20  25

Breaking Out of Nested Loops

let found = false

for (let i = 0; i < 10 && !found; i = i + 1) {
for (let j = 0; j < 10; j = j + 1) {
if (i * j == 42) {
io.println("Found: " + i + " * " + j + " = 42")
found = true
break  // Breaks inner loop
}
}
}

Common Patterns

Sum of Array

let numbers = [1, 2, 3, 4, 5]
let sum = 0

for (let i = 0; i < len(numbers); i = i + 1) {
sum = sum + numbers[i]
}

io.println("Sum: " + sum)  // 15

Find Maximum

let numbers = [3, 7, 2, 9, 1, 5]
let max = numbers[0]

for (let i = 1; i < len(numbers); i = i + 1) {
if (numbers[i] > max) {
max = numbers[i]
}
}

io.println("Max: " + max)  // 9

Filter Array

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let evens = []

for (let i = 0; i < len(numbers); i = i + 1) {
if (numbers[i] % 2 == 0) {
evens[len(evens)] = numbers[i]
}
}

// evens = [2, 4, 6, 8, 10]

Input Validation Loop

let validInput = false

while (!validInput) {
io.print("Enter a positive number: ")
let input = int(io.readLine())

if (input > 0) {
validInput = true
io.println("You entered: " + input)
} else {
io.println("Invalid! Must be positive.")
}
}

Menu Loop

let running = true

while (running) {
io.println("\n=== Menu ===")
io.println("1. Say Hello")
io.println("2. Say Goodbye")
io.println("3. Exit")
io.print("Choice: ")

let choice = io.readLine()

if (choice == "1") {
io.println("Hello!")
} else if (choice == "2") {
io.println("Goodbye!")
} else if (choice == "3") {
running = false
io.println("Exiting...")
} else {
io.println("Invalid choice!")
}
}

Truthiness in Conditions

Values that evaluate to false:

All other values are truthy:

if ("hello") {
io.println("Non-empty strings are truthy")
}

if (42) {
io.println("Non-zero numbers are truthy")
}

if ([1, 2, 3]) {
io.println("Non-empty arrays are truthy")
}

💡 Tip Prefer explicit comparisons for clarity: if (count > 0) instead of if (count)

Next: Functions →