Skip to main content

for

for .. in loop

Loop through each item of an array, or list of values/objects.

Here is an example:

from "std" import *

vcc = supply("VCC")
gnd = dgnd()

at vcc
wire right 100

# Loop through an array of colors
for i in ["yellow", "red", "blue", "green", "purple"]:
add led(i) pin 2
wire right 100

add res(10k)

wire right 100 down 100
to gnd
line 6:1VCCComponent D1_1: line 11:512D1_1yellowComponent D1_2: line 11:512D1_2redComponent D1_3: line 11:512D1_3blueComponent D1_4: line 11:512D1_4greenComponent D1_5: line 11:512D1_5purpleComponent R1: line 14:1R110kline 17:4GND

You can also use the range(start, end) built-in method to create multiple items:

from "std" import *

vcc = supply("VCC")
gnd = dgnd()

at vcc
wire right 100

# Loop through an array of colors
for i in range(0, 5):
add led("GREEN") pin 2
wire right 100

add res(10k)

wire right 100 down 100
to gnd
line 6:1VCCComponent D1_1: line 11:512D1_1GREENComponent D1_2: line 11:512D1_2GREENComponent D1_3: line 11:512D1_3GREENComponent D1_4: line 11:512D1_4GREENComponent D1_5: line 11:512D1_5GREENComponent R1: line 14:1R110kline 17:4GND