- Home /
Question by
twinspectre90 · Mar 18, 2014 at 03:22 PM ·
c#for loop
for Loops c#
int myNumber = 0;
void Start (){
for(int i = 0; i < 10; i++)
{
myNumber = myNumber + i;
Debug.Log(myNumber);
}
}
i would like to know why the console print this result :
0
1
3
6
10
15
21
28
36
45
insted of :
0
1
2
3
4
5
6
7
8
9
i need explanation :(
Comment
Best Answer
Answer by CodeElemental · Mar 18, 2014 at 03:26 PM
Because each cycle you increment the myNumber by the value of i ( which is incremented each loop ).
example :
1st loop :
mynumber = 0
i = 0
2nd loop
i = 1
mynumber = 0 + 1
3rd loop :
i = 2
mynumber = mynumber(1) + 2 = 3
etc etc ...
If you want to get the output you posted , you need to change
mynumber = mynumber + i;
into :
mynumber = mynumber + 1;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Creating a timer for combat (C#) 1 Answer
Flip over an object (smooth transition) 3 Answers