Control Flow
DOCUMENTATION
Stepped For (
XCX 4.4.1 Control Flow
Conditional Statements (if/elseif/else)
NOTE:
Semicolons after block keywords are optional (recommended style: without).
Write then, do, else, end without ;. The ; form still compiles
but is removed in XCX 5.0a. Statement semicolons inside block bodies stay
required.
xcx
_
⛶
×
1if (condition) then
2 --- block
3elseif (other_condition) then
4 --- block
5else
6 --- block
7end
Aliases:
| Keyword | Aliases |
|---|---|
elseif | elif |
The short forms elf and els were removed in XCX 4.4 - use elseif/elif
and else.
xcx
_
⛶
×
1i: score = 75;
2if (score >= 90) then
3 >! "A";
4elif (score >= 75) then
5 >! "B";
6elseif (score >= 60) then
7 >! "C";
8else
9 >! "F";
10end
Loops
Range For
Range is inclusive on both sides.
xcx
_
⛶
×
1for i in 1 to 3 do
2 >! i;
3end
4--- prints: 1, 2, 3
Stepped For (@step)
xcx
_
⛶
×
1for j in 0 to 6 @step 2 do
2 >! j;
3end
4--- prints: 0, 2, 4, 6
Collection Iteration
Works on arrays, sets, and fibers. The loop variable receives the element, not the index.
xcx
_
⛶
×
1--- Array
2array:i: nums {10, 20, 30};
3for el in nums do
4 >! el;
5end
6
7--- Set
8set:N: primes {2, 3, 5, 7, 11};
9for p in primes do
10 >! p;
11end
12
13--- Fiber
14fiber:i: f = gen(5);
15for val in f do
16 >! val;
17end
While Loop
xcx
_
⛶
×
1i: cnt = 0;
2while (cnt < 3) do
3 cnt = cnt + 1;
4 >! cnt;
5end
Break and Continue
break exits the current loop. continue skips to the next iteration. Both affect only the immediately enclosing loop.
xcx
_
⛶
×
1for n in 1 to 5 do
2 if (n % 2 == 0) then continue; end
3 if (n == 5) then break; end
4 >! n;
5end
NOTE:
break inside a for loop over a fiber automatically calls .close() on that fiber.