Booleans & Comparisons
Booleans are the foundation of every decision your program makes. GX keeps them simple and efficient.
The bool Type
Two values: true and false.
fn main() {
var is_ready = true
var game_over = false
print("Ready: {is_ready}\n")
print("Game over: {game_over}\n")
}
Comparison Operators
Compare two values to get a bool:
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal to |
< | Less than |
> | Greater than |
<= | Less than or equal |
>= | Greater than or equal |
fn main() {
var x = 10
var y = 20
print("x == y: {x == y}\n") // false
print("x != y: {x != y}\n") // true
print("x < y: {x < y}\n") // true
print("x > y: {x > y}\n") // false
print("x <= 10: {x <= 10}\n") // true
print("y >= 20: {y >= 20}\n") // true
}
Logical Operators
Combine boolean expressions:
| Operator | Meaning | True when… |
|---|---|---|
&& | And | Both sides are true |
|| | Or | At least one side is true |
! | Not | Flips true to false |
fn main() {
var age = 25
var has_ticket = true
// AND: both must be true
if (age >= 18 && has_ticket) {
print("Welcome in!\n")
}
// OR: at least one must be true
var is_vip = false
if (has_ticket || is_vip) {
print("You can enter\n")
}
// NOT: flip the value
var is_closed = false
if (!is_closed) {
print("The shop is open\n")
}
}
Combining Conditions
Build complex expressions by chaining operators:
fn main() {
var score = 85
var attempts = 2
if (score >= 80 && score < 90 && attempts <= 3) {
print("Grade: B (solid work!)\n")
}
var temp = 22
if (temp < 0 || temp > 40) {
print("Extreme temperature!\n")
} else {
print("Temperature is normal: {temp}C\n")
}
}
Booleans in Variables
Store the result of a comparison for later:
fn main() {
var x = 42
var is_even = (x % 2 == 0)
var is_positive = (x > 0)
var is_small = (x < 100)
print("Even: {is_even}\n")
print("Positive: {is_positive}\n")
print("Small: {is_small}\n")
if (is_even && is_positive && is_small) {
print("{x} is a small, positive, even number\n")
}
}
Try it — Build your own boolean expressions in the Playground.
Expert Corner
true and false compile to 1 and 0 in the generated C code. There’s zero abstraction cost — a boolean is just an integer at the machine level, exactly like C.
Short-circuit evaluation: GX evaluates && and || left-to-right and stops as soon as the result is known. In a && b, if a is false, b is never evaluated. In a || b, if a is true, b is never evaluated. This is important when b has side effects or involves expensive computation.
Why parentheses are required around conditions (if (x > 0) not if x > 0): this removes parsing ambiguity, keeps the grammar simple, and matches what most programmers already know from C, Java, and JavaScript. The parser stays boring — and boring parsers have fewer bugs.