There’s a lot of confusion there.
In C you are not required to initialize the variable with a value, but this is always an error to use in a for
because it gets unpredictable from where he’s going to start. It may even work in several situations, but it is not correct and there will be situations that will not get the expected result. In C# it really does not allow to do this because it is almost 100% right that it is wrong (in for
It’s always, but he doesn’t even know the difference).
This second code doesn’t make any sense in C or C#, even if one of them accepts it, why use a i
there if it has no function? In one does the same as before and in the other does nothing.
The first code looks much better in almost all situations like this:
for (int i = 1; i <= 10; i++)
So why do it any different? Is there a reason? There is a reason, but it doesn’t seem to be the case for you. This goes for C or C#, but in C it is common for some people to be stuck to the way they did 30 years ago, it makes no sense to declare the variable before using it.
If it is really the case to make the statement before, then it should do (in C or C#):
int i = 1;
for (; i <= 10; i++)
This only makes sense if the i
is used after the for
, and if it is manipulated internally in the for
or if it is possible to leave the for
before completing the count (perform all programmed steps), otherwise do not do so. keep the variable in the scope of the for
.
By the question it is not very clear what you want to do in C# and how you do in C, but it seems to be a mistake.
But to answer your question: you don’t have to initialize a for
, you just have to do it right, and you can’t do something almost random and hope it works.
- Or declares and initializes the variable in
for
,
- or declares and initializes before the
for
,
- or declares the variable before the
for
and initializes in it.
What you cannot do is try to use a variable without initializing.
Even the question implies that the error is because of the for
, but even though the error is not presented in the question I can state that it has to do with the use of the variable and nothing to do with the for
, what is the point of using a variable without doing anything with it? Even outside the for
would make the same mistake (by chance there is in the for
and gave error in it, but only coincidence). It’s a pity that C does not give error.
Consider that you shouldn’t do this in C either, just because you can do it doesn’t mean you should, you have to know well because you’re doing it to work, otherwise at most it will work by coincidence. And if the second code is what it does in C, that doesn’t make any sense.
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.
– Maniero