Skip to main content

Flow control

The following flow control structures are supported within Circuitscript.

if .. else statement

An expression is evaluated and when it is true, the block within the if statement will be executed. Otherwise the block within the else statement will be executed.

Multiple if .. else can be defined to create a circuit graph based on different conditions.

Here is an example:

from "std" import *

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

use_resistor = false

at vcc
wire down 100
branch:
wire right 200 down 100

if use_resistor:
add res(10k)
else:
add cap(100n)

wire down 100
to gnd

wire down 100
add res(10k)

wire down 100
to gnd
line 8:1VCCComponent C1: line 16:9C1100nline 19:8GNDComponent R1: line 22:1R110kline 25:4GND

Loops

while loop

An expression is evaluated and as long as it is true, the block within the while statement will be executed.

Here is an example:

from "std" import *

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

at vcc
wire right 100

counter = 0
while counter < 5:
add led("yellow") pin 2
wire right 100
counter += 1

add res(10k)

wire right 100 down 100
to gnd
line 6:1VCCComponent D1_1: line 11:512D1_1yellowComponent D1_2: line 11:512D1_2yellowComponent D1_3: line 11:512D1_3yellowComponent D1_4: line 11:512D1_4yellowComponent D1_5: line 11:512D1_5yellowComponent R1: line 15:1R110kline 18:4GND

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

Interrupting flow control

break

When this is used within a loop (while or for), it will stop further execution of the loop.

Example:

from "std" import *

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

at vcc
wire right 100

counter = 0

# Loop through an array of colors
while true:
add led("yellow") pin 2
wire right 100
counter += 1

# once this condition is reached, then stop the loop
if counter > 4:
break

add res(10k)

wire right 100 down 100
to gnd
line 6:1VCCComponent D1_1: line 13:512D1_1yellowComponent D1_2: line 13:512D1_2yellowComponent D1_3: line 13:512D1_3yellowComponent D1_4: line 13:512D1_4yellowComponent D1_5: line 13:512D1_5yellowComponent R1: line 21:1R110kline 24:4GND

break within circuit paths

If break is used within a branch command, it will stop further execution within that branch and return to the original branch location.

continue

When this is used, it will jump to the next iteration of the loop. Unlike the break keyword, this does not stop the loop completely.

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"]:

# do not create red or green led!
if i == "red" || i == "green":
continue

add led(i) pin 2
wire right 100

add res(10k)

wire right 100 down 100
to gnd
line 6:1VCCComponent D1_1: line 16:512D1_1yellowComponent D1_3: line 16:512D1_3blueComponent D1_5: line 16:512D1_5purpleComponent R1: line 19:1R110kline 22:4GND