A C-like for-statement, which has been adopted by many languages never was an option for SharpBASIC, nor were constructs with a step or by clause to influence the loop direction and steps. But the shortcut operators allow for clean, concise and flexible syntax and they can be applied consistently throughout the language. Let's have a look at an example:
Code: Select all
const a = 0;
const b = 20;
dim i:itr;
main do
' even numbers 0-20
for i = a to b :++ do
if i % 2 == 0 do
print(i);
end
end
' flag loop completed
print(i == b + 1);
end
Code: Select all
' even numbers 0-20
for i = a to b :+2 do
print(i);
end
' flag loop completed
print(i == b + 2);
Iters cannot be initialized upon declaration, nor can they be used as parameter or function type. Similarly, for-statements will not accept other datatypes as iterators. However, SharpBASIC provides both itr (signed) and uitr (unsigned). The next example uses unsigned iter and leaves the value null after the loop:
Code: Select all
' unsigned itr
dim i: uitr;
main do
' numbers 10 to 1
for i = 10 to 1 :-- do
print(i);
end
print(i); ' null
end