Control structures
Chalk’s two control structures, decided as the document compiles: @for repeats source — a count, a range or each row of a dataset — and @if keeps or drops it.
On this page
Chalk has two control structures: , a loop, and , a conditional. Both are decided as the document compiles, and the internote a reader opens holds only what they produced.
#Repeating with @for
writes its body out again for every pass. Its implicit value says how many passes: a whole count, counted from 1, an inclusive range that counts upward, or a dataset, one pass per row.
@for:3{
Attempt {{i}} of 3.
}That is three paragraphs, Attempt 1 of 3. to Attempt 3 of 3. :0 writes nothing; :-2..2 makes five passes, −2 to 2. A range written downward is refused: descending range `3..1` — `from..to` iterates upward, so `from` must not exceed `to`.
The body is any Chalk that is legal where the stands. Inside a graph it repeats marks; at the top of the document it repeats whole scenes. The counter is spliced in as text before the body is read, so it lands wherever text can — in a heading, in maths, in an expression:
@for:2..4{
scene{
## $x^{{i}}$
Near the origin, $x^{{i}}$ is flatter than $x^{{i - 1}}$.
}[
graph{
curve(expression: x^{{i}}; domain: [-1.5, 1.5])
}(
y domain: [-2, 2]
)
]
}Three scenes, for x^2, x^3 and x^4.
Inside one graph, the same loop draws a family of curves, each pass a curve with its own label:


graph{
@for:1..4{
curve(expression: x^{{i}}; domain: [0, 1.3]; label: $x^{{i}}$)
}
}(
x-domain: [0, 1.4]
y-domain: [0, 3]
)#The counter and its ids
The counter is {{i}} unless as: names it. Inside the body it is a number, so {{i^2}} and {{i - 1}} are arithmetic.
@for(n: 1..3; as: row){
@for(n: 1..3; as: col){
{{row}} × {{col}} = {{row * col}}.
}
}The counter exists only inside the body. So does anything the body declares: a inside a pass is gone after it, and a outside with the counter's name is hidden inside the loop and back afterwards.
An id written in the body is written once per pass, and ids are unique across the document, so a fixed id is refused on the second pass:
graph{
@for:0..2{
point#p(x: {{i}}; y: 0)
}
}[reference]duplicate id `#p` — already declared by a node at line 3
Splicing the counter into the id gives one id per pass, and each can be reached like any other:
scene{
The last point sits at height {{#p-4.y}}.
}[
graph{
@for:0..4{
point#p-{{i}}(x: {{i}}; y: {{i^2}})
}
}(
x domain: [-1, 5]
y domain: [-1, 17]
)
]Inside a template, ids made this way are private to each use; Modularity covers how.
#Once per row of a dataset
Given a dataset's id, makes one pass per row, top to bottom. {{i}} is the row number from 1 and {{i.<column>}} that row's cell:
@data#islands{
island | area | species
Baltra | 25.09 | 58
Bartolomé | 1.24 | 31
Santa Cruz | 903.82 | 444
}
@for:#islands{
{{i}}. {{i.island}} covers {{i.area}} km$^2$.
}The comes first: `@for` reads its dataset in document order, so the `@data` must come first. A pack's dataset is walked the same way, :#biology.lynx-hare (Modularity). How cells are named, what a hyphenated column can and cannot do in arithmetic, and the refusals for a column or a boundary file in place of a dataset are on Datasets.
#Keeping or dropping with @if
keeps its body when its condition holds, and its detail, if it has one, when it fails. The condition is when:, an expression that gives true or false:
@let(audience: teacher)
@let(answers)
@if(when: audience = teacher){
note{
Answers are collected in the closing scene.
}
}[
note{
Answer each question before moving on.
}
]
@if(when: audience = teacher and answers){
The worked answers follow each question.
}<,<=,>and>=compare numbers, and dates by which comes first;and,orandnotjoin conditions;x in 0..5tests a range, either end of which may be left open (x in 5..) or left out of it with a<beside the dots (x in 0..<5,x in 0<..);trueandfalseare themselves.=and!=compare loosely:Teacher = teacherand1.0 = 1hold.==and!==compare exactly, soTeacher == teacherdoes not. Two texts compare as text: under(a: 1.0)and(b: 1),a = bfails anda = 1holds, as"1.0" = "1"fails.- Beside
=, a word nothing declares is a name, kept as written and possibly several words:audience = teacher,country = new zealand. Quoted, it is text and nothing else:country = "new zealand". - A bare key tests for
trueand!keyforfalse:(answers)is(when: answers). A bare(answers)istrue. - Inside a condition a hyphen is a minus, so a hyphenated constant goes in braces:
(when: {{show-value}} and answers). - A condition gives a boolean, never a number: @if: `k + 1` gives a number where a boolean is wanted — compare it, as in `k + 1 > 0`. With no detail, a failed
leaves nothing behind. - The constant is declared above. Bare, a name nothing declares is the word itself:
(when: level)is @if: `level` holds `level`, which is not a boolean, and(when: level > 3)is @if: `level` is not a valid number. A template's attributes and acounter or cell count as constants.
A key: value pair is not a condition, and is refused with the fix: @if: `audience: teacher` is not an attribute of `@if` — write `@if(when: audience = teacher)`. The same operators work in any {{…}} and in every attribute that takes a condition, on Expressions.
What the kept branch declares stays inside it, as in a body.
(answers){is four} is printed as written.#Choosing rows
A over a dataset has no filter or sort. An in its body chooses rows, by a cell or by the row number:
@data#islands{
island | area | species | inhabited
Baltra | 25.09 | 58 | true
Bartolomé | 1.24 | 31 | false
Santa Cruz | 903.82 | 444 | true
}
graph{
@for(n: #islands; as: row){
@if(row.inhabited){
point(x: {{row.area}}; y: {{row.species}}; label: {{row.island}})
}
}
}Two points, Baltra and Santa Cruz. (when: row.island = Santa Cruz) keeps one row by name, (when: row = 1) the first, and (when: row.area > 10 and row.inhabited) the rows that pass both tests.
#Decided as the document compiles
and run while the document compiles. The compiled internote holds the passes and the kept branch, and nothing a reader does changes which. So a count and a condition read only what is settled then: written values, constants, a template's attributes, loop counters and cells, and expressions over them.
@let(sides: 6)
@let(diagonals: {{sides * (sides - 3) / 2}})
@for(n: {{diagonals}}){
Diagonal {{i}} of {{diagonals}}.
}A computed count is written in the named form, (n: {{…}}); the implicit value is a single token, and :{{diagonals}} is refused with that fix. A value the reader can move — a fit's slope, a parameter — is bound, and refused. Here #trend is a fit:
@let(slope: {{#trend.slope}})
@if(when: slope > 0.9){
The trend rises.
}[expression]`@if` is decided while the document compiles and cannot depend on `#trend.slope` (`when: slope > 0.9` is bound)
What changes as the internote is read lives on the canvas: steps and cues on Motion, parameters set by controls on Controls. Which values are bound is on Expressions.