+ 1
Why would a for loop use subtraction instead of an absolute number?
If you come across a code with a for loop that does something similar to the code below, why do you think a subtraction is used instead of an absolute number? for (int i = 0; i < 3 - 1; i++) { //some code }
4 Antworten
+ 2
To make people understand it more.
e.g.
//sum of all elements up to the 50th
for(int i = 0; i < 50 -1; i++);
Is easily understandable than
for(int i= 0; i < 49; i++);
0
In Python I discovered that - operator required little more runtime than + operator, thus I would use:
for (int i = 0; i + 1 < 3; i++) {}
To save few nanoseconds from your life.
0
Yes. Just for understanding.. Otherwise better to use 2 in place 3-1 for to have one less calculation..