Conditionals & Looping
The PAC-1 scripting language is Turing-complete, which means any logical function that can run in a Python script, can be programmed using it too.
Conditionals (if...else statements) are the only instance of forked branching where steps have two output channels - one which evaluates if the condition is true, and other if it is false.
If...Else conditionals
For instance, when the control flow reaches step 4
, if it evaluates to true, then it passes to the fork step 4.1.
main.py
# if...then else conditionals.
1. set {{payload}} to '{"num": 29}'
2. add 1 to {{payload.num}}
3. print {{payload.num}}
4. if {{payload.num}} is less than 36
- 4.1. go to step 3
5. else if {{payload.num}} is more than 36
- 5.1. print {{payload.num}}
Otherwise, if it evaluates to false, the control flow passes to step 5
.
main.py
# if...then else conditionals.
1. set {{payload}} to '{"num": 29}'
2. add 1 to {{payload.num}}
3. print {{payload.num}}
4. if {{payload.num}} is less than 36
- 4.1. go to step 3
5. else if {{payload.num}} is more than 36
- 5.1. print {{payload.num}}
PAC-1 interprets conditionals like this :
Looping and Recursion
The go to
step can similarly be used to loop / recurse over a function.
main.py
# looping using go to
1. trigger on receive message
2. set {{payload}} to '{"num": 29}'
3. add 1 to {{payload.num}}
4. print {{payload.num}}
5. if {{payload.num}} is less than 36
- 5.1. go to step 3
6. else if {{payload.num}} is more than 36
- 6.1. print {{payload.num}}
7. respond back with {{payload}}