typestar

Loops in Igor Pro

for and do-while, and the reminder that a wave assignment usually beats both.

Function SumPositive(WAVE w)
    Variable i, total = 0

    for (i = 0; i < numpnts(w); i += 1)
        if (w[i] <= 0)
            continue
        endif
        total += w[i]
    endfor

    return total
End

Function CountDown(Variable n)
    do
        n -= 1
    while (n > 0)
    return n
End

How it works

  1. for takes the C shape and closes with endfor.
  2. do closes with while, so the body always runs once.
  3. break leaves the loop; continue jumps to the next turn.

Keywords and builtins used here

The run, in numbers

Lines
19
Characters to type
225
Tokens
69
Three-star pace
75 tpm

At the three-star pace of 75 tokens a minute, this run takes about 55 seconds.

Type this snippet

Step 2 of 4 in Flow control, step 9 of 18 in Language basics.

← Previous Next →

Loops in other languages