Actually I was taught so by a former colleague of mine who is a c++ guru and has contributed to the works of Stroustrup. I'll try to defend it as I practice it and as he originally argued well for its use. When you work with iterators for instance, you define range as a place to start and a place to end. I'm not doing anything differently by telling at exactly which value to stop at.
As Antheus already mentioned, using iterators in a for loop is legacy syntax. For loops predate iterators and standard library containers, and that syntax was retrofitted to emulate the foreach construct. Now that there is a proper foreach construct in the standard library, iterator syntax in a for loop for going over the elements of a container is no longer idiomatic.
Are you not aware of when to stop counting? If your code is broken, and you "accidentally" skip past the value to end at, you have much bigger issues than putting a const before a variable: you cannot defend how the code will behave.
Once again, we come back to floating point values. While using such values is less common than simple counters, there is no reason why I cannot loop over the interior of a circle in pi/variable increments and have my loop terminate once I have reached ~2pi. Or perhaps I simply want to loop from 0.0 to 1.0 by 0.1 increments, or perhaps I even want to have a variable store the way in which I want to increment that cannot be determined at compile time, but I know that I want to stop when I have exceeded such a value. Also, (i < 10) implies that the loop is valid only for numbers less than 10, (10 != i) implies that the loop is invalid for the value 10, and requires additional information to be parsed in order to fully understand the purpose and intent of the loop.
You can (somewhat rightfully) state that I should not be using a for loop, but I do not find that very compelling, primarily because of how Bjarne Stroustrup has described the for loop statement as "iterating over a sequence of numbers." A numeric sequence is an ordered list of numbers described by a discrete function, and optionally bounded. Referring to my previous post, the natural language description of a sequence whose bound is written (n < 10) is:
All positive integers less than 10.
Conversely, if the bound is (10 != n), the language is less clear:
All positive integers where 10 is not equal to n.
While anyone versed in boolean expressions can distinguish between the two, the natural language reading of one is more obvious than the other. Also, a sequence of numbers includes floating point values, or an unknown quantity (at compile time). When the sequence involves ascending values, the < operator always defines a perfect upper bound, and the the > operator a lower bound for descending values. (10 != i) simply describes a single invariant.
If I break something, I will instantly know because i write so that problems will show themselves at compile time.
I am unsure as to how (10 != i) will produce a compile time error as opposed to (i < 10).
You mention written works arguing against this practice, (error prone and against good coding style) -could you help me to the source for it? I cannot find it.
Firstly, in approximately 10 C++ specific books that I currently have sitting on my shelf (sorry, about 3 of those are various editions of the same book), none of them contain an example of a for loop using your suggested syntax. All of them use a variation of the for (int i = 0; i < 10; ++i) syntax. These books include:Programming: Principles and Practice Using C++
- Bjarne StroustrupThe C++ Programming Language
- Bjarne StroustrupC++ Primer Plus
- Stephen Prata (multiple editions)
I consider these three books and a similar book on the C++ Standard Library to be the definitive works on the C++ language.
In addition, I remember language covering a similar topic in Code Complete
, but I do not currently have my copy on hand to quote the relevant passages.
I have multiple books on the following languages: C, Java, C#, python, etc. All of which share the C style for loop syntax, and all examples follow the exact same pattern. I like to believe that I have most of the essential and definitive treatises on the subjects they contain, and I have not seen one mention a example similar to the one you have brought up. I have worked with a few people who use it and have talked about it, but I they have always been in the minority. I suppose I could also do my own namedropping of several prominant founders of Linux and Bell Labs, but I think that is unnecessary.
As a parting word, I would also like to express how greatly I prefer functional language for syntax. The use of list and sequence syntax is much more descriptive, and I would say that the natural language parsing of functional syntax is closer to that of my suggested for syntax.
Happy for your input although this thread has somewhat strayed from the original purpose. ;-)
I certainly hope that you do not take this the wrong way, as I am a language nerd and will politely discuss any range of minutiae ad nauseum. No one is incorrect here.