Models 707B and 708B Switching Matrix Reference Manual
Section 6: Instrument programming
707B-901-01 Rev. A / August 2010
6-27
Example: Numeric for
Code
list = {"One", "Two", "Three", "Four", "Five", "Six"}
---------- For loop -----------
print("Counting from one to three:")
for element = 1, 3 do
print(element, list[element])
end
print("Counting from one to four, in steps of two:")
for element = 1, 4, 2 do
print(element, list[element])
end
Notes and output
The numeric
for
loop repeats a block of code while a control variable runs through an
arithmetic progression.
Counting from one to three:
1.00000 One
2.00000 Two
3.00000 Three
Counting from one to four, in steps of two:
1.00000 One
3.00000 Three
Example: Generic for
Code
days = {"Sunday",
"Monday", "Tuesday",
"Wednesday", "Thursday",
"Friday", "Saturday"}
for i, v in ipairs(days) do
print(days[i], i, v)
end
Notes and output
The generic
for
statement works by using functions called
iterators
. On each iteration, the
iterator function is called to produce a new value, stopping when this new value is nil.
Output
Sunday 1.00000 Sunday
Monday 2.00000 Monday
Tuesday 3.00000 Tuesday
Wednesday 4.00000 Wednesday
Thursday 5.00000 Thursday
Friday 6.00000 Friday
Saturday 7.00000 Saturday