0
Why should we increase the value by 1 while looping?
2 odpowiedzi
+ 6
In order for the loop to run finite number of times, for example in for loop:-
for(int i=0;i<10;i++)
Now in the above code if we do not increase the value of i after each iteration obviously i will remain eqaul to 0 and will always satisfy the looping condition which is i<10, and hence it will never stop and will run infinite times.
It is not necessary to always increase it by 1 but you can also increase it by 2 or any other number.
Similar explanation goes for other types of loops
+ 3
You increase by one a counter, if you want to count one by one in ascending order.
But you could as well choose any step you need for counting: adding 2, 3 or even float values, and negative values for decreasing order ^^