This a clear example of early optimization.... IMHO, that is something that programmers new to their craft are way to prone to worry about. If you must worry about it, learn to benchmark and profile your code so that your worries are based on evidence rather than supposition.
Speaking to your specific questions. First, a <=
is not implemented as two operations testing for <
and ==
separately in any C compiler I've met in my career. And that includes some monumentally stupid compilers. Notice that for integers, a <= 5
is the same condition as a < 6
and if the target architecture required that only <
be used, that is what the code generator would do.
Your second concern, that while (i != 10)
might be more efficient raises an interesting issue of defensive programming. First, no it isn't any more efficient in any reasonable target architecture. However, it raises a potential for a small bug to cause a larger failure. Consider this: if some line of code within the body of the loop modified i
, say by making it greater than 10, what might happen? How long would it take for the loop to end, and would there be any other consequences of the error?
Finally, when wondering about this kind of thing, it often is worthwhile to find out what code the compiler you are using actually generates. Most compilers provide a mechanism to do this. For GCC, learn about the -S
option which will cause it to produce the assembly code directly instead of producing an object file.