When to use loop? How many conditions?

Hey guys, so I am having a theotrically question.
I have for example the following codes which are doing the same:
1
2
for(i=0;i<2;i++){
if N[i]==N[i+1] j++;}

or
1
2
if N[0]==N[1] j++;
if N[1]==N[2] j++;

Is there any difference between those two(speed or something else)?
Because atm I am having some conditions which have things in common to write a loop, but a loop for 2-3 conditions seems to be more work than just writing the conditions.

Thanks in advance greetings lafo
Last edited on
Speed/performance wise, practically no. But the loop version is better. Currently you have only array of two, in real code the number of items will come dynamically, in which case you wont know how many iterations the loop will go through. It will also become unwieldy to write all these assignments by hand
closed account (G30oGNh0)
Don't take my word for it but I think there is a negligible speed difference in between the two.

The for loop will just make your life a little easier as you can define the limit without having to write a big block of if statements.

But as I said don't take my word for it, I'm sure the coding enthusiasts can give you a better explanation.

Hope it helps.
Last edited on
@ codewalker
ah ok, so if I just have 2 conditions or max. 3 which are static it doesn't matter?


Correct. Doesn't matter, there is no practical difference in speed. In other words for two-three static values not using loop is not going to give you any measurable speed gains over using loop
Ok, that helped me a lot.
Thanks for the responses!
Topic archived. No new replies allowed.