.\" Made by website/plain/build.mjs from website/words/if.md; change that file. .TH SG-IF 7 2026-11-28 "SG" "SG reference" .SH NAME if \- runs a block when a condition holds .SH SYNOPSIS .TP \fBif c { ... }\fR runs the block when c is true .TP \fBif c { ... } else { ... }\fR one block or the other .TP \fBif c { ... } else if d { ... }\fR a chain: the first condition that holds picks its block .TP \fBif a && b, if a || b, if !a\fR conditions joined; the right side runs only when it is needed .TP \fBif x is null, if x is not null\fR a test of a T?; where it holds, x is narrowed .TP \fBif x is Circle(r)\fR a test of one variant, with names for what it carries .TP \fBc ? a : b\fR no if: a value picked by a condition .SH DESCRIPTION \fBif\fR runs its block when its condition is \fBtrue\fR, and skips it when it is \fBfalse\fR\&. The condition must be a \fBbool\fR: a number, a text or a \fBT?\fR is not one, and is compared or tested instead (\fBcount != 0\fR, \fBname is not null\fR). The block is always in braces, \fB{\fR at the end of the line, and an \fBelse\fR goes on the line of the \fB}\fR before it. .PP A block is a scope of its own: a variable declared in it is gone after the \fB}\fR\&. \fB&&\fR and \fB||\fR stop as soon as the answer is known, so \fBi < xs.length() && xs[i] > 0\fR never reads past the end. .PP A test with \fBis\fR tells the compiler more than \fBtrue\fR or \fBfalse\fR\&. After \fBif x is not null\fR, \fBx\fR is a \fBT\fR in the block; after \fBif x is null { return }\fR, it is a \fBT\fR for the rest of the function. The names of \fBx is Circle(r)\fR are seen in the block. [\fBis\fR](is.md) has the whole of it; \fBnull\fR is tested with \fBis\fR, never with \fB==\fR or \fB!=\fR\&. .PP \fBif\fR is a statement, never a value. A value picked by a condition is written \fBc ? a : b\fR: only the arm chosen runs, both arms have one type, and the first arm sees what an \fBis\fR in \fBc\fR narrows, as the block of an \fBif\fR does. .SH EXAMPLES .SS "A block that runs when a condition holds" .RS 4 .nf void main() { var stock = 3 if stock < 5 { print("order more") } if stock == 0 { print("sold out") } print("done") } .fi .RE .PP .RS 4 .nf order more done .fi .RE .PP A short block can stay on one line. The second condition is \fBfalse\fR, so its block is skipped. .SS "One way or the other" .RS 4 .nf void main() { var hour = 20 if hour < 18 { print("open") } else { print("closed") } } .fi .RE .PP .RS 4 .nf closed .fi .RE .PP Exactly one of the two blocks runs. [\fBelse\fR](else.md) has more on the second one. .SS "A chain of else if" .RS 4 .nf string sizeOf(int bytes) { if bytes < 1024 { return $"{bytes} B".toString() } else if bytes < 1024 * 1024 { return $"{bytes / 1024} KB".toString() } else { return $"{bytes / (1024 * 1024)} MB".toString() } } void main() { print(sizeOf(500)) print(sizeOf(4096)) print(sizeOf(3000000)) } .fi .RE .PP .RS 4 .nf 500 B 4 KB 2 MB .fi .RE .PP The conditions are tried from the top, and the first that holds picks its block. Every way through the chain returns, so the function needs no \fBreturn\fR after it. .SS "Conditions joined" .RS 4 .nf void main() { var scores = List { 7, 0, 12 } var temperature = 31 var raining = false if temperature > 30 && !raining { print("go to the sea") } if temperature < 5 || raining { print("take a coat") } nint i = 3 if i < scores.length() && scores[i] > 5 { print("a high score") } else { print("no score there") } } .fi .RE .PP .RS 4 .nf go to the sea no score there .fi .RE .PP \fBscores\fR has no item at index 3, but \fBscores[i]\fR is never read: \fBi < scores.length()\fR is \fBfalse\fR, and \fB&&\fR stops there. .SS "A test of null" .RS 4 .nf void greet(read string? name) { if name is null { print("hello, stranger") return } print($"hello, {name}, {name.length()} letters") } void main() { greet(null) greet("Ada") string? city = "Izmir" if city is not null && city.length() > 3 { print(city.toUpper()) } } .fi .RE .PP .RS 4 .nf hello, stranger hello, Ada, 3 letters IZMIR .fi .RE .PP The block of the first \fBif\fR returns, so after it \fBname\fR is a \fBread string\fR and has a \fBlength()\fR\&. On the right of \fB&&\fR, and in the block, \fBcity\fR is a \fBstring\fR\&. .SS "A variant, with is" .RS 4 .nf enum Command { Move(int steps) Turn(string direction) Stop } void run(read Command command) { if command is Move(n) && n > 0 { print($"forward {n}") } else if command is Turn(way) { print($"turn {way}") } else { print("stay") } } void main() { run(read Command.Move(3)) run(read Command.Move(0)) run(read Command.Turn("left")) run(read Command.Stop) } .fi .RE .PP .RS 4 .nf forward 3 stay turn left stay .fi .RE .PP \fBn\fR is seen on the right of \fB&&\fR and in the first block; \fBway\fR in the second. A test of every variant at once is a [\fBmatch\fR](match.md). .SS "A value picked by a condition" .RS 4 .nf int sign(int n) { return n > 0 ? 1 : n < 0 ? -1 : 0 } void main() { var count = 1 print($"{count} {(count == 1 ? "file" : "files")}") print(sign(-8)) string? name = null var letters = name is not null ? name.length() : 0 print(letters) } .fi .RE .PP .RS 4 .nf 1 file -1 0 .fi .RE .PP \fB?:\fR groups to the right, so a chain reads as \fBelse if\fR\&. Inside \fB{ }\fR of \fB$"..."\fR it goes in parentheses, since a \fB:\fR there starts the format. In the first arm of the last one, \fBname\fR is a \fBstring\fR\&. .SH "WHAT THE COMPILER REFUSES" .SS "A number as a condition" .RS 4 .nf void main() { var count = 3 if count { print("some") } } .fi .RE .PP .RS 4 .nf error: expected `bool`, found `int` --> example.sg:3:8 3 | if count { | ^^^^^ hint: a number is not a condition; compare it: `count != 0` .fi .RE .SS "An assignment as a condition" .RS 4 .nf void main() { var count = 3 if count = 3 { print("three") } } .fi .RE .PP .RS 4 .nf error: an assignment cannot be a condition --> example.sg:3:14 3 | if count = 3 { | ^ hint: comparison is written `==` .fi .RE .SS "A block without braces" .RS 4 .nf void main() { var count = 3 if count > 2 print("many") } .fi .RE .PP .RS 4 .nf error: expected `{`, found end of line --> example.sg:3:17 3 | if count > 2 | ^ .fi .RE .SS "null compared with !=" .RS 4 .nf void main() { string? name = "Ada" if name != null { print(name.length()) } } .fi .RE .PP .RS 4 .nf error: `null` is not compared with `!=` --> example.sg:3:8 3 | if name != null { | ^^^^^^^^^^^^ hint: write `name is not null` .fi .RE .PP \fBis not null\fR is the test the compiler narrows by. .SS "if as a value" .RS 4 .nf void main() { var count = 3 var word = if count > 2 { "many" } else { "few" } print(word) } .fi .RE .PP .RS 4 .nf error: expected an expression, found `if` `if` is a statement, not a value --> example.sg:3:16 3 | var word = if count > 2 { "many" } else { "few" } | ^^ hint: a value picked by a condition is written with `?:`: `count > 2 ? "many" : "few"` .fi .RE .PP \fBif\fR gives no value. When the \fBif\fR is written on one line, the hint is the line with \fB?:\fR; otherwise it shows the form, \fBcondition ? a : b\fR\&. .SH "SEE ALSO" .BR sg-else (7), .BR sg-is (7), .BR sg-bool (7), .BR sg-while (7), .BR sg-match (7), .BR sg-null (7) .PP https://sg\-lang.org/reference/if.html