++I or I++ in for Loops

++i or i++ in for loops ??

For integers, there is no difference between pre- and post-increment.

If i is an object of a non-trivial class, then ++i is generally preferred, because the object is modified and then evaluated, whereas i++ modifies after evaluation, so requires a copy to be made.

i++ or i-- in a for loop?

It's usually recommended to concentrate on making code as clear and as logical as possible, without worrying about micro-optimizations or other factors. In your case, the first one is the better choice, since most programmers are more used to traverse an array in that order.

Both versions will have the same result (given that they're implemented correctly) and will have exactly the same run time.

EDIT: @Zane mentioned in a comment that looping backwards to zero was faster some time ago. It was, the reason for it was that comparing a variable to zero was faster. Given that computers were much much slower those days, such optimizations were encouraged. Those days are indeed over...

is there a reason to use ++$i in for loop?

++$i is a micro-optimisation, it executes fractionally faster than $i++. However, unless the $subusers array is being changed within the loop so that count($subusers) can change from one iteration to the next, then any slight positive gain in speed is being negated (and then some) by counting the number of array entries every iteration.

Note that $i++ and ++$i would both execute at the end of each iteration of the loop. It isn't the same as initialising $i to 1 rather than to 0.

i++ vs. ++i in a JavaScript for loop

In JS and PHP it does not make any difference, I think even in Java it does not make any difference but in pure c when compiler is not optimizing code it does, and that is why a lot of people use ++i because they are used to it from c.

EDIT:
This is an answer for JS if you want history of pre and post increment searc C/C++ pre/post increment. Or see comments on @Orvev's answer.

What's the meaning of 'i' in for loop?

You have given two different example.

For the first one

for i in 'string':
print(i)

For this one, the variable 'i' is the variable where the value will be put from your parameter (here 'string'). If i gave an array as parameter for instance, each value of array will be put in 'i', step by step (element [0], then element[1], etc...).

Note that this is a simplified view of this question and it doesn't work exactly like that for the program. But you can understand it as it.

For the second one

for(var i ; i<100 ; i++) {
console.log(i);
}

You declare explicitly a variable to be used. This declared will be used by the loop, incrementig by the step you had defined until it reaches limit you also defined.

It is slightly the same thing for both example. Hope i explain well

Can a for loop increment/decrement by more than one?

Use the += assignment operator:

for (var i = 0; i < myVar.length; i += 3) {

Technically, you can place any expression you'd like in the final expression of the for loop, but it is typically used to update the counter variable.

For more information about each step of the for loop, check out the MDN article.

Is ++i really faster than i++ in for-loops in java?

No, it's not true. You could measure the performance by timing each loop for a large number of iterations, but I'm fairly certain they will be the same.

The myth came from C where ++i was regarded as faster than i++ because the former can be implemented by incremeting i then returning it. The latter might be implemented by copying the value of i to a temporary variable, incrementing i, then returning the temporary. The first version doesn't need to make the temporary copy and so many people assume that it is faster. However if the expression is used as a statement modern C compilers can optimize the temporary copy away so that there will be no difference in practice.

Position and Alternatives of Naming Java loop control variable in `for(){}` loops

In my opinion, it is perfectly acceptable to use variables other than i or j in loops. Programming is about writing understandable code, so a variable called ´cupsOfCoffee´ is very self-explanatory. It could thus also be useful for developers, working on the project you are working on.



Related Topics



Leave a reply



Submit