Loops and values · 2 of 4
Conditions
if runs a block only when a condition holds. else adds an alternative, and else if chains more conditions:
if i % 3 == 0 {
place Pad at (i * pitch, 0)
} else if large {
place Pad with side = 120um at (i * pitch, 0)
} else {
place Pad with side = 40um at (i * pitch, 0)
}Conditions are built from comparisons (==, !=, <, <=, >, >=) and combined with && (and), || (or) and ! (not); a bool parameter such as large above can be used directly. % is the remainder of an integer division, handy for patterns that repeat.
Loops nest, so two loops walk through a grid:
for i in 0..columns {
for j in 0..rows {
...
}
}Exercise
Turn Board into an n × n checkerboard of squares of side size: loop over the columns i and the rows j, and draw a square wherever i + j is even. The square of column i and row j has its bottom-left corner at (i * size, j * size).