Control Flow
Execution Flow
merx programs execute by traversing the flowchart from Start to End:
- Begin at the
Startnode - Follow the outgoing edge to the next node
- At each node:
- Process node: Execute all statements, then follow the outgoing edge
- Condition node: Evaluate the condition, then follow the
YesorNoedge
- Repeat until the
Endnode is reached
Conditional Branching
Use a Condition node {} with a trailing ? to branch based on a condition. The node must have exactly two outgoing edges labeled Yes and No:
mermaid
flowchart TD
Start --> A[x = 42]
A --> B{x > 0?}
B -->|Yes| C[println 'positive']
B -->|No| D[println 'not positive']
C --> End
D --> Endconsole
$ merx run branch.mmd
positiveThe condition expression must evaluate to a bool value. If it evaluates to true, the Yes edge is followed; if false, the No edge is followed.
Nested Conditions
You can chain multiple conditions by connecting Condition nodes:
mermaid
flowchart TD
Start --> A[x = 15]
A --> B{x % 15 == 0?}
B -->|Yes| C[println 'FizzBuzz']
B -->|No| D{x % 3 == 0?}
D -->|Yes| E[println 'Fizz']
D -->|No| F{x % 5 == 0?}
F -->|Yes| G[println 'Buzz']
F -->|No| H[println x]
C --> End
E --> End
G --> End
H --> EndLoops
Loops are created by connecting an edge back to a previous node. There is no special loop syntax; you simply point an edge to an earlier node in the flowchart:
mermaid
flowchart TD
Start --> A[i = 0]
A --> B{i < 5?}
B -->|Yes| C[println i; i = i + 1]
C --> B
B -->|No| Endconsole
$ merx run loop.mmd
0
1
2
3
4In this example:
iis initialized to0- The Condition node checks if
i < 5 - If
Yes, the value ofiis printed and incremented - The edge from
Cgoes back toB, creating a loop - When
ireaches5, theNoedge leads toEnd
Nested Loops
You can nest loops by using multiple Condition nodes:
mermaid
flowchart TD
Start --> A[i = 1]
A --> B{i <= 3?}
B -->|No| End
B -->|Yes| C[j = 1]
C --> D{j <= 3?}
D -->|No| E[i = i + 1]
E --> B
D -->|Yes| F[println i as str + ' * ' + j as str + ' = ' + (i * j) as str]
F --> G[j = j + 1]
G --> Dconsole
$ merx run nested.mmd
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9