New course! Every coder should learn Generative AI!
Try a free lesson+ 2
Initial value is your requirement.. You can start at 0 or any other value..
For clarity, can you show a code snippet which you are taking about..
+ 1
M .Queen
for(int i=0; i<10; i++)
{
..
}
//i variable not exist outside loop..
Or
int i=0;
for(;i<10;i++)
Or
int i=0; //it has global scope.
for(int i=0; i<10; i++) //here i has local scope. This i is different from outside loop i, it over hidden outer i.
{
..
}
int i=0;
for(;;)
{
i++;
if(i>10)
break;
}
Here i exist, and value is 11
All valid, so just use depends on your need.
Hope it clears..
0
If you need that i value after the also, you have declare it before outside loop.
But if you don't need the value of i after loop anywhere then it's better to declare within loop and hence that, in some languages it is automatically garbage collected (freed from memory to speed up).
Like this :(it works perfectly but 'variable i not exist after loop) :
for(int i=0; i<=10; i++)
{
... //i available here within loop only..
// i have Local scope.
}