Pros and Cons of i != n vs i < n in an int for loop

language-agnostic

Solution

I think the main argument against the first version is that it is a much less common idiom.

Remembering that code is read more often than it is written, it does not make sense to use a less familiar form of for loop if there isn't a very clear advantage to doing so. All it achieves is distracting anyone working on the code in future.

So primarily for code maintenance reasons (by others as well as the original coder) I would favour the more common second format.

Problem

What are the pros and cons of using one or the other iteration functions ? ``` function (int n) { for (int i = 1; i != n; ++i) { ... } } ``` vs ``` function (int n) { for (int i = 1; i < n; i++) { ... } } ```

Original source