initializing iterator before for loop in Dart

You don’t need to put anything in the first part of the for:

var i = 0;
for (; i < list.length - list.indexOf('.'); i++) ...

Putting an i there is technically valid, but kindof useless since it doesn’t do anything, (which is why you get a warning).

You can also do:

var i = 0;
for (var n = list.length - list.indexOf('.'); i < n; i++) ...

and avoid computing the end for every step of the loop. That would be quite a lot more efficient.

You can also do:

var n = list.length - list.indexOf('.');
for (var i = 0; i < n; i++) ...

and then use n afterwards instead of i. That would make it a more traditional loop for readers looking at it.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top