Your First Program
Let's write some real Quartz programs. We'll start simple and progressively build more interesting examples.
Hello, World!
Create a file called hello.qz:
// hello.qz - The classic first program
import system.io as io
io.println("Hello, World!")
Run it:
quartz hello.qz
Output:
Hello, World!
Congratulations! Let's break down what happened:
// comment— Comments start with//import system.io as io— Import the I/O module with aliasioio.println("...")— Print a line to standard output
Variables and Types
Quartz uses let for variable declarations:
import system.io as io
// Integers
let age = 25
let year = 2024
// Floating-point numbers
let pi = 3.14159
let temperature = -40.5
// Strings
let name = "Quartz"
let greeting = "Hello, " + name + "!"
// Booleans
let isAwesome = true
let isBoring = false
io.println(greeting)
io.println("Age: " + age)
io.println("Pi: " + pi)
User Input
Read input from the user:
import system.io as io
io.print("What is your name? ")
let name = io.readLine()
io.print("How old are you? ")
let ageStr = io.readLine()
let age = int(ageStr)
io.println("Hello, " + name + "!")
io.println("Next year you'll be " + (age + 1) + " years old.")
Defining Functions
Functions are defined with fn:
import system.io as io
// Simple function
fn greet(name) {
io.println("Hello, " + name + "!")
}
// Function with return value
fn add(a, b) {
return a + b
}
// Use the functions
greet("World")
io.println("2 + 3 = " + add(2, 3))
Number Guessing Game
Here's a complete program that puts it all together:
// guess.qz - A number guessing game
import system.io as io
import system.math as math
class GuessingGame {
constructor(maxNumber) {
self.maxNumber = maxNumber
self.secretNumber = math.randint(1, maxNumber)
self.attempts = 0
self.won = false
}
fn makeGuess(guess) {
self.attempts = self.attempts + 1
if (guess < self.secretNumber) {
return "Too low!"
} else if (guess > self.secretNumber) {
return "Too high!"
} else {
self.won = true
return "Correct!"
}
}
fn play() {
io.println("=== Number Guessing Game ===")
io.println("I'm thinking of a number between 1 and " + self.maxNumber)
io.println("")
while (!self.won) {
io.print("Your guess: ")
let input = io.readLine()
let guess = int(input)
let result = self.makeGuess(guess)
io.println(result)
}
io.println("")
io.println("You won in " + self.attempts + " attempts!")
}
}
// Start the game
let game = GuessingGame(100)
game.play()
Execution Modes
Quartz offers two execution modes:
Interpreter Mode (Default)
quartz yourprogram.qz
Direct execution, great for development and debugging.
Bytecode Mode
quartz -c yourprogram.qz
Compiles to bytecode first, faster execution for larger programs.
Pre-compile Bytecode
# Compile to .qzb file
quartz -b yourprogram.qz
# Run compiled bytecode
quartz yourprogram.qzb