to

In the context of the "for" loop in X3 programming language, the "to" keyword is used to specify the range of values over which the loop variable iterates. Here's how you would use the "to" keyword in conjunction with the "for" loop:

Basic Usage:

# Using 'to' keyword in 'for' loop to specify the range
for var i = 1 to 5 then
    print(i)
end

Explanation:

  • In this example, the "for" loop iterates over the values from 1 to 5 (inclusive).

  • The loop variable i takes on each value within this range in sequential order, and the code inside the loop (printing the value of i) is executed for each iteration.

Step Size:

# Using 'to' with step size in 'for' loop
for var i = 0 to 10 step 2 then
    show(i)
end

Explanation:

  • Here, the loop iterates over the values from 0 to 10 with a step size of 2.

  • The loop variable i increments by 2 in each iteration, and the code inside the loop (printing the value of i) is executed.

Iterating Backwards:

# Using 'to' to iterate backwards in 'for' loop
for var i = 10 to 1 step -1 then
    show(i)
end

Explanation:

  • This example demonstrates iterating backwards from 10 to 1 with a step size of -1.

  • The loop variable i decrements by 1 in each iteration, and the code inside the loop (printing the value of i) is executed.

Nested Loops:

# Nested 'for' loops using 'to' keyword
for var i = 1 to 3 then
    for var j = 1 to 3 then
        show("({i}, {j})")
    end
end

Explanation:

  • In this example, a nested "for" loop is used to create a grid-like structure.

  • The outer loop iterates over the values of i, and for each iteration of i, the inner loop iterates over the values of j.

By incorporating the "to" keyword in your X3 language's "for" loop syntax, you provide users with a clear and concise way to specify the range of values over which they want to iterate.

Last updated