+ 1
Every time you run through the for-loop, does code made inside the for-loop reset (e.g. like counters inside a for-loop).
trying to understand how to program and not mess up
3 odpowiedzi
+ 11
We may need an example.
for (int i = 0; i < 5; i++)
{
// codes
}
Every time this block of code is encountered, i will always be initialised to 0.
int i = 0;
for (; i < 5; i++)
{
// codes
}
This, one the other hand, has had it's variable declared and initialised outside the loop, and manipulated by the loop. The next time the loop is encountered, the variable will not be re-initialized to 0.
Taking another example of nested for loops:
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
// codes
}
}
Let S be the set which contains all values of i and j which occurs chronologically and simultaneously.
S = {i, j} = { (0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4) }
You can see that j gets re-initialized to 0 for each time i loops.
+ 7
No...
Like, if a var is in the loop, it won't be reset.
0
than I really need help understanding how the pyramid lesson works. Thanks