.\" Made by website/plain/build.mjs from website/words/false.md; change that file. .TH SG-FALSE 7 2026-11-28 "SG" "SG reference" .SH NAME false \- the bool that is not true; what a comparison gives when it does not hold, and the zero value of a bool .SH SYNOPSIS .TP \fBvar found = false, return false\fR the value, kept or given back .TP \fBbool done = false in a struct\fR the default of a field .TP \fBbool verbose = false in a parameter\fR the default of a parameter .TP \fBbool[30], bool30\fR an array of false: alone, it is filled with its zero value .TP \fBi < n && xs[i] > 0\fR when the left side is false, the right side never runs .TP \fB!found, !seen.add(x)\fR true where the value is false .TP \fBfalse => ..., Lamp(false) => ...\fR an arm of a match, at the top or inside a variant .SH DESCRIPTION \fBfalse\fR is one of the two values of a [\fBbool\fR](bool.md); [\fBtrue\fR](true.md) is the other. It is what a comparison gives when it does not hold, and what a test gives when the answer is no: \fBcontains\fR for an item that is not there, \fBadd\fR of a \fBHashSet\fR for an item that already was. \fB!false\fR is \fBtrue\fR\&. .PP \fB&&\fR stops at \fBfalse\fR: when its left side is \fBfalse\fR, so is the whole, and the right side is not run. That is what makes \fBi < xs.length() && xs[i] > 0\fR safe. \fB||\fR with a \fBfalse\fR on the left gives its right side. .PP A variable and a field get no value by themselves: a struct literal that leaves out a \fBbool\fR field is an error, unless the field has a default, \fBbool done = false\fR\&. A parameter may have one too. In an array, \fBfalse\fR is the zero value: \fBbool[30]\fR alone holds thirty \fBfalse\fR\&. .PP \fBfalse\fR is not \fB0\fR: \fBbool found = 0\fR is refused, and so is \fB!\fR on a number. It is printed as \fBfalse\fR\&. .SH EXAMPLES .SS "A default" .RS 4 .nf struct Job : Formattable { string title bool done = false } void show(read List jobs, bool verbose = false) { for job in read jobs { if verbose || !job.done { print($" {job}") } } } void main() { var jobs = List { Job { title = "write" }, Job { title = "test", done = true }, } print("open:") show(read jobs) print("all:") show(read jobs, true) } .fi .RE .PP .RS 4 .nf open: Job { title = "write", done = false } all: Job { title = "write", done = false } Job { title = "test", done = true } .fi .RE .PP \fBwrite\fR is built without \fBdone\fR, and gets \fBfalse\fR\&. A call that leaves out \fBverbose\fR gives it \fBfalse\fR\&. .SS "&& stops at false" .RS 4 .nf bool positiveAt(read List items, nint i) { return i < items.length() && items[i] > 0 } void main() { var items = List { 4, -2 } print(positiveAt(read items, 0)) print(positiveAt(read items, 1)) print(positiveAt(read items, 9)) } .fi .RE .PP .RS 4 .nf true false false .fi .RE .PP For \fB9\fR the left side is \fBfalse\fR, and \fBitems[9]\fR, which would stop the program, is never read. .SS "An array of false" .RS 4 .nf void main() { var composite = bool[30] for i in 2..30 { if composite[i] { continue } nint j = i * i while j < 30 { composite[j] = true j += i } } var primes = List() for i in 2..30 { if !composite[i] { primes.add(i) } } print(primes) } .fi .RE .PP .RS 4 .nf [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] .fi .RE .PP Every item starts \fBfalse\fR; a number marked \fBtrue\fR has a smaller factor. What is still \fBfalse\fR at the end is a prime. .SS "A test that says no" .RS 4 .nf void main() { var seen = HashSet() for word in "the cat saw the dog and the cat".splitWhitespace() { if !seen.add(word.clone()) { print($"again: {word}") } } print($"{seen.length()} words") print($"{seen.contains("bird")} {seen.add("bird".clone())} {seen.add("bird".clone())}") } .fi .RE .PP .RS 4 .nf again: the again: the again: cat 5 words false true false .fi .RE .PP \fBadd\fR gives \fBfalse\fR for a word that was already there, and \fB!\fR turns that into the test. .SS "A flag that starts false" .RS 4 .nf bool hasNegative(read List numbers) { for n in read numbers { if n < 0 { return true } } return false } void main() { var found = false var lines = List { "ok", "ok", "stop", "ok" } nint i = 0 while !found && i < lines.length() { found = lines[i] == "stop" i += 1 } print($"found: {found} after {i} lines") print(hasNegative(read List { 3, 1 })) print(hasNegative(read List())) } .fi .RE .PP .RS 4 .nf found: true after 3 lines false false .fi .RE .PP \fBreturn false\fR after the loop is the answer when nothing was found, an empty list included. .SH "WHAT THE COMPILER REFUSES" .SS "A field left out" .RS 4 .nf struct Job { string title bool done } void main() { var job = Job { title = "write" } print(job.done) } .fi .RE .PP .RS 4 .nf error: `Job` is missing `done` every field gets a value; there is no uninitialized memory --> example.sg:7:15 7 | var job = Job { title = "write" } | ^^^^^^^^^^^^^^^^^^^^^^^ .fi .RE .PP \fBbool done = false\fR in the struct gives the field its default. .SS "0 for false" .RS 4 .nf void main() { bool found = 0 print(found) } .fi .RE .PP .RS 4 .nf error: expected `bool`, found `int` --> example.sg:2:18 2 | bool found = 0 | ^ hint: a `bool` is `true` or `false`: write `false` .fi .RE .SS "! on a number" .RS 4 .nf void main() { var items = List() if !items.length() { print("empty") } } .fi .RE .PP .RS 4 .nf error: expected `bool`, found `nint` --> example.sg:3:9 3 | if !items.length() { | ^^^^^^^^^^^^^^ hint: a number is not a condition; compare it: `items.length() == 0`, in place of `!items.length()` .fi .RE .PP \fB!\fR turns a \fBbool\fR around and nothing else. C's \fB!n\fR is \fBn == 0\fR, so the hint compares with \fB==\fR\&. .SH "SEE ALSO" .BR sg-bool (7), .BR sg-true (7), .BR sg-if (7), .BR sg-while (7), .BR sg-match (7), .BR sg-struct (7) .PP https://sg\-lang.org/reference/false.html