Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should one use < or <= in a for loop [closed]

People also ask

Can we use or condition in for loop?

It is possible to use multiple variables and conditions in a for loop like in the example given below. Show activity on this post. actually I don't think this version looks prettier than the same expression within the for (...) construct.

Can we use == in for loop?

You can use == but you can iterate max one time.

Should I use a for or while loop?

Use a for loop when you know the loop should execute n times. Use a while loop for reading a file into a variable. Use a while loop when asking for user input. Use a while loop when the increment value is nonstandard.


The first is more idiomatic. In particular, it indicates (in a 0-based sense) the number of iterations. When using something 1-based (e.g. JDBC, IIRC) I might be tempted to use <=. So:

for (int i=0; i < count; i++) // For 0-based APIs

for (int i=1; i <= count; i++) // For 1-based APIs

I would expect the performance difference to be insignificantly small in real-world code.


Both of those loops iterate 7 times. I'd say the one with a 7 in it is more readable/clearer, unless you have a really good reason for the other.


I remember from my days when we did 8086 Assembly at college it was more performant to do:

for (int i = 6; i > -1; i--)

as there was a JNS operation that means Jump if No Sign. Using this meant that there was no memory lookup after each cycle to get the comparison value and no compare either. These days most compilers optimize register usage so the memory thing is no longer important, but you still get an un-required compare.

By the way putting 7 or 6 in your loop is introducing a "magic number". For better readability you should use a constant with an Intent Revealing Name. Like this:

const int NUMBER_OF_CARS = 7;
for (int i = 0; i < NUMBER_OF_CARS; i++)

EDIT: People aren’t getting the assembly thing so a fuller example is obviously required:

If we do for (i = 0; i <= 10; i++) you need to do this:

    mov esi, 0
loopStartLabel:
                ; Do some stuff
    inc esi
                ; Note cmp command on next line
    cmp esi, 10
    jle exitLoopLabel
    jmp loopStartLabel
exitLoopLabel:

If we do for (int i = 10; i > -1; i--) then you can get away with this:

    mov esi, 10
loopStartLabel:
                ; Do some stuff
    dec esi
                ; Note no cmp command on next line
    jns exitLoopLabel
    jmp loopStartLabel
exitLoopLabel:

I just checked and Microsoft's C++ compiler does not do this optimization, but it does if you do:

for (int i = 10; i >= 0; i--) 

So the moral is if you are using Microsoft C++†, and ascending or descending makes no difference, to get a quick loop you should use:

for (int i = 10; i >= 0; i--)

rather than either of these:

for (int i = 10; i > -1; i--)
for (int i = 0; i <= 10; i++)

But frankly getting the readability of "for (int i = 0; i <= 10; i++)" is normally far more important than missing one processor command.

† Other compilers may do different things.


I always use < array.length because it's easier to read than <= array.length-1.

also having < 7 and given that you know it's starting with a 0 index it should be intuitive that the number is the number of iterations.


Seen from an optimizing viewpoint it doesn't matter.

Seen from a code style viewpoint I prefer < . Reason:

for ( int i = 0; i < array.size(); i++ )

is so much more readable than

for ( int i = 0; i <= array.size() -1; i++ )

also < gives you the number of iterations straight away.

Another vote for < is that you might prevent a lot of accidental off-by-one mistakes.


@Chris, Your statement about .Length being costly in .NET is actually untrue and in the case of simple types the exact opposite.

int len = somearray.Length;
for(i = 0; i < len; i++)
{
  somearray[i].something();
}

is actually slower than

for(i = 0; i < somearray.Length; i++)
{
  somearray[i].something();
}

The later is a case that is optimized by the runtime. Since the runtime can guarantee i is a valid index into the array no bounds checks are done. In the former, the runtime can't guarantee that i wasn't modified prior to the loop and forces bounds checks on the array for every index lookup.


It makes no effective difference when it comes to performance. Therefore I would use whichever is easier to understand in the context of the problem you are solving.