This code seems to be valid Dart code, even though the last part of the for loop declaration i missing:
for( int i=0; i<1; ){ // Missing i
// Do something
}
The result is an infinite loop.
Why is this not considered a syntax error? There's no mention of this "feature" in the Dart Language Tour.
CodePudding user response:
Well it is a feature, it just gives you an extra control over your for loop. You are technically still following the contract of for-loop but stating that, incremental part will be handled by you later on.
This t just gives you a chance to do the increment within the for loop instead of doing it on the fly.
for (int i = 0; i < 10; ) {
i = 'test'.length i;
print('test $i');
}
CodePudding user response:
The same holds for C, C , java: even for (;;) {
is possible.
Consider other ways of using an index (java):
for (int i = 0; i < s.length(); ) {
...
int i2 = s.indexOf(',', i 1);
if (i2 == -1)
i2 = s.length();
String w = s.substring(i, i2);
...
i = i2;
}
In general one can only in specific cases guarantee that a program terminates. So the value of such requirement would be relative.