for
The general declaration of a for
loop in C like for (init; condition; step)
will be removed. Instead, for
loops shall only (ONLY!) be used for range based loops:
NAL:
for (i in myArray) {...}
for (i in [5 .. n]) {...}
C:
for (size_t i = 0; i < _nalArraySize(myArray); ++i) {...}
for (size_t i = 5; i < n; ++i) {...}
()
can be removed? This is how it looks: for i in myArray {...}
i
is always an index. If an actual value of an array needs to be addressed, one still needs to call the array operator (or however it is called).while
.While in C, the type size_t
is commonly used, a keyword without underscore is preferred explicitely providing functionality for index counting. Proposition: idx
, []
, counter
, sizet
, tick
That type shall provide functionality to define an undefined index, like an over- and underflow. How is not yet solved.
The index variable is always defined inside the loop. But sometimes, one wants to know the resulting index for use after the loop. In such a case, the retulting index will be returned from the for-loop like so:
NAL:
i32 lastIndex = for (i in [5 .. n]) {...}
C:
i32 lastIndex = 5;
for (size_t i = 5; i < n; ++i) {
...
lastIndex = (i32)i;
}
If the break
statement is used, the current value of the index variable will be returned:
NAL:
i32 lastIndex = for (i in [5 .. n]) {
...
if(i == 20) { break; }
}
C:
i32 lastIndex = 5;
for (size_t i = 5; i < n; ++i) {
...
if(i == 20) {
lastIndex = (i32)i;
break;
}
lastIndex = (i32)i;
}
If desired, with a break
statement, the programmer may return a custom index like so:
NAL:
i32 lastIndex = for (i in [5 .. n]) {
...
if(i == 20) { break 111; }
}
C:
i32 lastIndex = 5;
for (size_t i = 5; i < n; ++i) {
...
if(i == 20) {
lastIndex = (i32)111;
break;
}
lastIndex = (i32)i;
}