Syntax Basics
Comments
Section titled “Comments”// Single-line comment
/* Multi-line comment*/Variables
Section titled “Variables”Immutable Bindings
Section titled “Immutable Bindings”let name = "Zap"let pi = 3.14159let active = trueImmutable bindings cannot be reassigned after declaration:
let x = 10x = 20 // ✗ Compile error: cannot reassign immutable bindingMutable Bindings
Section titled “Mutable Bindings”Use mut when you need to reassign:
let mut counter = 0counter = counter + 1 // ✓ OKType Annotations
Section titled “Type Annotations”Type annotations are optional when the compiler can infer the type:
let x: Int = 42 // explicitlet y = 42 // inferred as Intlet name: String = "Zap" // explicitOperators
Section titled “Operators”Arithmetic
Section titled “Arithmetic”| Operator | Description | Example |
|---|---|---|
+ |
Addition | 3 + 4 |
- |
Subtraction | 10 - 2 |
* |
Multiplication | 6 * 7 |
/ |
Division | 15 / 3 |
% |
Remainder | 10 % 3 |
Comparison
Section titled “Comparison”| Operator | Description | Example |
|---|---|---|
== |
Equal | x == y |
!= |
Not equal | x != y |
< |
Less than | x < y |
> |
Greater than | x > y |
<= |
Less or equal | x <= y |
>= |
Greater or equal | x >= y |
Logical
Section titled “Logical”| Operator | Description | Example |
|---|---|---|
and |
Logical AND | a and b |
or |
Logical OR | a or b |
not |
Logical NOT | not a |
Expressions vs Statements
Section titled “Expressions vs Statements”In Zap, almost everything is an expression — it produces a value:
// `if` is an expressionlet status = if active { "on" } else { "off" }
// Block expressions — the last expression is the valuelet result = { let a = 10 let b = 20 a + b // evaluates to 30}String Interpolation
Section titled “String Interpolation”let name = "world"print("Hello, {name}!") // Hello, world!Next Steps
Section titled “Next Steps”Learn about Zap’s type system.