For Loop: Less Than or Not Equal to?

Should I use a less than symbol:

 
for(unsigned int rep = 0; rep < 5; rep++)


Or a not equal to symbol?

 
for(unsigned int rep = 0; rep != 5; rep++)
Last edited on
Hello FluorescentGreen5,

Generally most for loop will use < and sometimes >. Once in awhile <= or >= is used. I can not say that I have ever seen != used. Not sure how well it would work, but its use would be very specific.

Some of the other people here might have more input and may have experienced using !=.

Hope that helps,

Andy
is there any performance difference?
is there any performance difference?

Nope.

1
2
3
4
5
6
#include <iostream>
int main( )
{
	for (unsigned int rep = 0; rep != 5; rep++)
		std::cout << rep;
}

assembly output:
1
2
3
4
5
6
7
8
9
.L3:
        cmp     DWORD PTR [rbp-4], 5
        je      .L2
        mov     eax, DWORD PTR [rbp-4]
        mov     esi, eax
        mov     edi, OFFSET FLAT:std::cout
        call    std::basic_ostream<char, std::char_traits<char> >::operator<<(unsigned int)
        add     DWORD PTR [rbp-4], 1
        jmp     .L3

https://godbolt.org/g/qiJ2iy

1
2
3
4
5
6
#include <iostream>
int main( )
{
	for (unsigned int rep = 0; rep <= 5; rep++)
		std::cout << rep;
}

assembly output:
1
2
3
4
5
6
7
8
9
.L3:
        cmp     DWORD PTR [rbp-4], 5
        ja      .L2
        mov     eax, DWORD PTR [rbp-4]
        mov     esi, eax
        mov     edi, OFFSET FLAT:std::cout
        call    std::basic_ostream<char, std::char_traits<char> >::operator<<(unsigned int)
        add     DWORD PTR [rbp-4], 1
        jmp     .L3

https://godbolt.org/g/gEHfNh

As you can see, the assembly output is exactly the same, thus zero performance difference.
Topic archived. No new replies allowed.