Why would a for loop use subtraction instead of an absolute number? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 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 }

7th Aug 2020, 7:57 AM
silentlearner
silentlearner - avatar
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++);
7th Aug 2020, 8:08 AM
Ore
Ore - avatar
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.
7th Aug 2020, 9:17 AM
Seb TheS
Seb TheS - avatar
0
Seb TheS That is not a valid Python program 😐
7th Aug 2020, 9:18 AM
Ore
Ore - avatar
0
Yes. Just for understanding.. Otherwise better to use 2 in place 3-1 for to have one less calculation..
7th Aug 2020, 9:24 AM
Jayakrishna 🇮🇳