.\" Made by website/plain/build.mjs from website/words/true.md; change that file. .TH SG-TRUE 7 2026-11-28 "SG" "SG reference" .SH NAME true \- the bool that is not false; what a comparison gives when it holds .SH SYNOPSIS .TP \fBbool ready = true, return true\fR the value, kept or given back .TP \fB10 > 3, names.contains("Ada")\fR what a comparison or a test gives when it holds .TP \fBwhile true { ... break }\fR a loop that runs until a break or a return .TP \fBready || check()\fR when ready is true, check() never runs .TP \fBtrue => ..., Lamp(true) => ...\fR an arm of a match, at the top or inside a variant .TP \fBflag ? 1 : 0\fR the number it stands for, written out .SH DESCRIPTION \fBtrue\fR is one of the two values of a [\fBbool\fR](bool.md); [\fBfalse\fR](false.md) is the other. It is what a comparison gives when it holds, and what a test such as \fBcontains\fR gives when the answer is yes. \fB!true\fR is \fBfalse\fR\&. .PP \fB||\fR stops at \fBtrue\fR: when its left side is \fBtrue\fR, so is the whole, and the right side is not run. \fB&&\fR with a \fBtrue\fR on the left gives its right side. .PP \fBtrue\fR is not a number. It is not \fB1\fR, \fB(int)true\fR is refused, and \fBflag ? 1 : 0\fR says which number it stands for. It is printed as \fBtrue\fR, and \fB\&.toString()\fR makes that text. A text is turned into a \fBbool\fR by comparing it: \fBword == "true"\fR\&. .PP A \fBwhile true\fR runs until a \fBbreak\fR or a \fBreturn\fR leaves it. A \fBmatch\fR on a \fBbool\fR has an arm \fBtrue\fR and one \fBfalse\fR; \fBtrue\fR is a pattern inside a variant too, as in \fBLamp(true)\fR\&. It is written in lower case only. .SH EXAMPLES .SS "Where true comes from" .RS 4 .nf void main() { bool ready = true var big = 10 > 3 var names = HashSet() var added = names.add("Ada") print($"{ready} {big} {added} {names.contains("Ada")}") print($"{ready == big} {!ready} {ready && big}") var text = true.toString() print($"{text} has {text.length()} letters") } .fi .RE .PP .RS 4 .nf true true true true true false true true has 4 letters .fi .RE .PP \fBnames.add\fR gives \fBtrue\fR when the item was not there before. .SS "while true" .RS 4 .nf void main() { var n = 27 var steps = 0 var highest = n while true { if n == 1 { break } n = n % 2 == 0 ? n / 2 : 3 * n + 1 highest = n > highest ? n : highest steps += 1 } print($"{steps} steps, highest {highest}") } .fi .RE .PP .RS 4 .nf 111 steps, highest 9232 .fi .RE .PP The test is inside the loop, where the value it needs is known; \fBbreak\fR leaves. .SS "|| stops at true" .RS 4 .nf bool askServer(string name) { print($" asking the server about {name}") return false } void main() { var isAdmin = true if isAdmin || askServer("ada") { print("ada may come in") } print(true || askServer("b")) print(false || askServer("c")) print(true && askServer("d")) } .fi .RE .PP .RS 4 .nf ada may come in true asking the server about c false asking the server about d false .fi .RE .PP The server is never asked about \fBada\fR or \fBb\fR: the left side was already \fBtrue\fR\&. \fB&&\fR needs its right side when the left is \fBtrue\fR, so \fBd\fR is asked. .SS "In a match" .RS 4 .nf enum Light { Lamp(bool on) Candle } string describe(read Light light) { return match light { Lamp(true) => "a lamp that is on" Lamp(false) => "a lamp that is off" Candle => "a candle" } } string sign(int n) { return match n >= 0 { true => "not negative" false => "negative" } } void main() { print(describe(read Light.Lamp(true))) print(describe(read Light.Lamp(1 > 2))) print(describe(read Light.Candle)) print(sign(-3)) print(sign(0)) } .fi .RE .PP .RS 4 .nf a lamp that is on a lamp that is off a candle negative not negative .fi .RE .PP An arm for \fBtrue\fR and one for \fBfalse\fR make the \fBmatch\fR whole, with no \fB_\fR\&. .SS "From text, and as a number" .RS 4 .nf void main() { for word in "true false yes TRUE".splitWhitespace() { bool flag = word == "true" print($"{word,-5} -> {flag,-5} {(flag ? 1 : 0)}") } var count = 0 var votes = List { true, false, true, true } for vote in read votes { count += vote ? 1 : 0 } print($"{count} of {votes.length()} said yes") } .fi .RE .PP .RS 4 .nf true -> true 1 false -> false 0 yes -> false 0 TRUE -> false 0 3 of 4 said yes .fi .RE .PP Only the text \fBtrue\fR, in lower case, is \fBtrue\fR here; the comparison says exactly which texts count. .SH "WHAT THE COMPILER REFUSES" .SS "true as a number" .RS 4 .nf void main() { print((int)true + 1) } .fi .RE .PP .RS 4 .nf error: a `bool` value cannot be converted to `int` --> example.sg:2:11 2 | print((int)true + 1) | ^^^^^^^^^ hint: a `bool` is not a number: `true ? 1 : 0` .fi .RE .PP \fB(true ? 1 : 0) + 1\fR is \fB2\fR\&. .SS "1 for true" .RS 4 .nf void main() { var n = 0 while 1 { n += 1 if n == 3 { break } } print(n) } .fi .RE .PP .RS 4 .nf error: expected `bool`, found `int` --> example.sg:3:11 3 | while 1 { | ^ hint: a `bool` is `true` or `false`: write `true` .fi .RE .PP \fBwhile true\fR is the loop that runs until a \fBbreak\fR\&. .SS "A bool? as the condition" .RS 4 .nf void main() { bool? answer = true if answer { print("yes") } } .fi .RE .PP .RS 4 .nf error: expected `bool`, found `bool?` --> example.sg:3:8 3 | if answer { | ^^^^^^ hint: it may be `null`: give a fallback with `answer ?? ...`, or test it with `is null` .fi .RE .PP \fBanswer ?? false\fR and \fBanswer == true\fR are \fBtrue\fR only when \fBanswer\fR holds \fBtrue\fR\&. .SH "SEE ALSO" .BR sg-bool (7), .BR sg-false (7), .BR sg-if (7), .BR sg-while (7), .BR sg-match (7), .BR sg-conditional (7) .PP https://sg\-lang.org/reference/true.html