.\" Made by website/plain/build.mjs from website/words/in.md; change that file. .TH SG-IN 7 2026-11-28 "SG" "SG reference" .SH NAME in \- stands between the name and the items in a for .SH SYNOPSIS .TP \fBfor i in a..b\fR the name i, then the numbers it takes .TP \fBfor x in xs\fR the name x, then the collection it goes over .TP \fBfor x in read xs, for x in ref xs\fR the same, with the items lent .SH DESCRIPTION \fBin\fR is written only in the head of a \fBfor\fR: the name of each round comes before it, and what the loop goes over comes after it. Everything about the loop is on the page of \fBfor\fR\&. .PP \fBin\fR is not an operator. Whether a list, a map, a set or a text holds something is asked with \fBcontains\fR\&. And since \fBin\fR is a word of the language, it cannot be the name of a variable. .SH EXAMPLES .SS "A range and a list" .RS 4 .nf void main() { var words = List { "red", "green", "blue" } for i in 0..words.length() { print($"{i} {words[i]}") } for word in read words { print(word.length()) } } .fi .RE .PP .RS 4 .nf 0 red 1 green 2 blue 3 5 4 .fi .RE .PP After \fBin\fR comes a range, \fB0..words.length()\fR, or a collection, here lent with \fBread\fR\&. .SS "Whether something is there" .RS 4 .nf void main() { var words = List { "red", "green", "blue" } print(words.contains("green")) var ages = HashMap { {"ada", 36} } print(ages.contains("grace")) print("green tea".contains("tea")) var n = 7 print(n >= 1 && n <= 9) } .fi .RE .PP .RS 4 .nf true false true true .fi .RE .PP \fBcontains\fR asks a list, a map (by its keys) or a text. A number is held between two ends with \fB>=\fR and \fB<=\fR\&. .SH "WHAT THE COMPILER REFUSES" .SS "in as a test" .RS 4 .nf void main() { var xs = List { 1, 2 } if 2 in xs { print("yes") } } .fi .RE .PP .RS 4 .nf error: expected `{`, found `in` `in` is written only at the head of a `for` --> example.sg:3:10 3 | if 2 in xs { | ^^ hint: whether a collection holds a value: `xs.contains(2)` .fi .RE .PP The test is written \fBif xs.contains(2)\fR\&. \fBprint(2 in xs)\fR is refused the same way. .SS "A for without in" .RS 4 .nf void main() { var xs = List { 1, 2 } for x of xs { print(x) } } .fi .RE .PP .RS 4 .nf error: expected `in`, found `of` --> example.sg:3:11 3 | for x of xs { | ^^ .fi .RE .SS "Two names before in" .RS 4 .nf void main() { var xs = List { 1, 2 } for i, x in read xs { print(x) } } .fi .RE .PP .RS 4 .nf error: a `for` has one loop variable --> example.sg:3:10 3 | for i, x in read xs { | ^ hint: with the positions: `for i in 0..xs.length()`, and the item is `xs[i]` .fi .RE .PP A \fBfor\fR has one name. For the positions too, go over \fB0..xs.length()\fR and read \fBxs[i]\fR\&. .SS "in as a name" .RS 4 .nf void main() { var in = 3 print(in) } .fi .RE .PP .RS 4 .nf error: expected a variable name, found `in` `in` is a word of the language, not a name --> example.sg:2:9 2 | var in = 3 | ^^ .fi .RE .SH "SEE ALSO" .BR sg-for (7), .BR sg-read (7), .BR sg-ref (7) .PP https://sg\-lang.org/reference/in.html