2
for (string::size_type ix = 0; ix != str.size(); ++ix)

why not use != instead of < in loop for C++?

Adrian Panasiuk
  • 7,249
  • 5
  • 33
  • 54
jiafu
  • 6,338
  • 12
  • 49
  • 73
  • a bit hard to tell what you want to argue? Why not choose `<`, indeed? Most of the time in C++ you'd use iterators, which then makes sense with `!=` and the value returned by the iterator function when encountering the end of the list. In your case `<` looks sensible. – 0xC0000022L May 29 '13 at 01:11
  • 2
    If it were me, I'd use `<`. – lurker May 29 '13 at 01:11
  • 1
    There is a duplicate with high view count and lots of good answers, let's see if I could find it before this question gets closed... – Sergey Kalinichenko May 29 '13 at 01:11
  • Conventionwise, < is preferred. `!=` is correct but it's not common, and thus looks like an error. – Alan May 29 '13 at 01:12
  • 6
    I was too slow... Anyway, check out this answer: [link](http://stackoverflow.com/a/8884617/335858). – Sergey Kalinichenko May 29 '13 at 01:13
  • 1
    @mbratch - +1 just in case the container .size method was broken and returned -1, using < is a bit more robust. – franji1 May 29 '13 at 01:14
  • I just think it states the intent better. :) – lurker May 29 '13 at 01:16
  • I stand corrected (see other similar questions/answers) – franji1 May 29 '13 at 01:16
  • In many error cases, `<` will terminate while `!=` will continue to iterate. For a loooooong time. This is the traditional reason for preferring `<`. Also, Fortran DO loops and the like always implemented with `<` (when using a positive increment), so it's familiar. – Hot Licks May 29 '13 at 01:28
  • 1
    Voted to remain closed - @dasblinkenlight found a duplicate – David May 29 '13 at 01:53
  • possible duplicate of [format of for loops](http://stackoverflow.com/questions/1783822/format-of-for-loops) (thx to dasblinkenlight for finding it) – jogojapan May 29 '13 at 05:26

4 Answers4

4

It's a matter of preference mostly - the syntax is usually considered in human readable form to mean "while ix is not equal to (value) do (action) and keep incrementing ix". This is why it's often typed as != to directly 'translate' the "is not equal" consideration.

For most loops it doesn't matter which one you use, there is however the consideration that using < instead of != reduces the chance of overflow errors occurring, for example when using:

for(int i = 0; i != 25; i += 2)

While it is obvious in this case the loop will never terminate, if the 25 or 2 come from another source it might not be. Thus it's preferable to use < consistently as a precautionary habit.

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
4

why not use != instead of < in loop for C++?

Sometimes this will lead to some bugs hard to catch. If I want to print odd numbers below 10, then != is not the right choice. Following loop runs for ever.

for (int i=1; i!=10; i+=2)
{
     cout << i << endl;
}
Sanish
  • 1,699
  • 1
  • 12
  • 21
1

The != idiom is necessary when you use iterators rather than integer indexes. If you often use iterators it just becomes habit.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
1

You can use either, and in C, I think < is more common. In C++, the STL adds a new twist:

for(FooType::iterator i = foo.begin(); i != foo.end(); i++)
    ... do stuff ...

Should use !=, because in some (most?) cases < will not even be defined for the iterator type. So you might want to use != in non-STL loops too, just for consistency.

Note: I normally use <, because as other answers say, it means you can't miss the sentry value. But this is no big deal; if you are unexpectedly missing the sentry, then you already have a bug.

Adrian Ratnapala
  • 5,485
  • 2
  • 29
  • 39