In loop what does i=i+1 and counter ++ mean?


#1

not understanding the meaning it coz both the codes present same answers


#2

got it ..that's the code itself. where both means the same but the latter is used to work it faster.


#3

The general idea is the following:
A loop well ... "loops" while the condition is true so you use a variable for which the condition is true and over the time you modify it in a way that it becomes false. One simple way to loop for a certain amount of times (that is actually what the for loop is for) is by moving in a certain amount of integer values e.g.

for(var name =0;name < end;name++){

What this means is that you start at a value of 0 and stop if the value of name is equal to the end value and as long as it is not you keep looping and increase the value of name after every successful loop.

As this is therefore mainly a counting variable you could use count as the name of the variable, but as it is rather unimportant and should be easy to write (no need for long names) and as your dealing with integer step width i is probably the most common name. Also you could start at 1 but real programmers like to call from 0 on also it has the advantage that you can compute the end value pretty nice. Say you want to loop 5 times you could simply say i<5 because 0,1,2,3,4 are 5 loops. If you'd start at one you would have to calculate stopping value+1 aso so starting at 0 is pretty handy.

So what the i=i+1 and i++ does is just increase the value of i by one so that the condition gets false when it should. If you consider their behaviour you could treat them as equal and as i++ is shorter it would be favorible for stepwidth of 1. Whereas for stepWidth other than one you would need to stick to i=i+x or switch to i+=x which is another short form for i=i+x. There is a difference between their values but as you don't use them very often this is something for another post.