﻿ for – Algosim documentation
Algosim documentation: for

# for

Evaluates an expression for every integer value in a range.

## Syntax

• `for(k, a, b, expr)`

• `k` is a symbol

• `a` and `b` are integers

• `expr` is an expression possibly containing `k`

## Description

`for(k, a, b, expr)` evaluates `expr(k)` `max(b − a + 1, 0)` times for `k = a`, `k = a + 1`, ..., and `k = b`. `for` always returns `null`, so it is used for its side effects.

## Examples

`c ≔ ToList(ZeroVector(10)); for(i, 1, 1000000, inc(c[1 + RandomInt(10)])); c`
```100055
100137
100092
99774
100109
100506
99629
100282
99376
100040
```
```for(k, 35, 81, (PercussionNoteOn(k, 75); sleep(.1)))
```

```x ≔ 600;
n ≔ 360;
pm ≔ CreatePixmap(x, x);
for(i, 1, n,
(
pm ≔ DrawLine(
pm,
RandomInt(3⋅x) − x,
RandomInt(3⋅x) − x,
RandomInt(3⋅x) − x,
RandomInt(3⋅x) − x,
"black")
)
);
GaussianBlur(ComponentHighlight(pm), 2, 3)
```

``` S ≔ '();
for
(
n, 0, 12,
VarAppend
(
S,
(
s ≔ '();
for(k, 0, n, VarAppend(s, StringPad(string(binomial(n, k)), 6, "c")));
join(s, "")
)
)
);
S @ (s ↦ StringPad(s, 80, "c"))
```
```                                       1
1     1
1     2     1
1     3     3     1
1     4     6     4     1
1     5     10    10    5     1
1     6     15    20    15    6     1
1     7     21    35    35    21    7     1
1     8     28    56    70    56    28    8     1
1     9     36    84   126   126    84    36    9     1
1     10    45   120   210   252   210   120    45    10    1
1     11    55   165   330   462   462   330   165    55    11    1
1     12    66   220   495   792   924   792   495   220    66    12    1
```