Initialize variable in a "for"

Asked

Viewed 172 times

2

I remember that in C language I didn’t need to initialize the variable (in case i) with a value, I tried to do the same on C# and got a build error.

In C# I am required to set the value of the variable at the start of for? Like this:

int i = 1;
for (i = 1; i <= 10; i++)

I can’t just do it the same way in C?

int i = 1;
for (i; i <= 10; i++)

  • 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.

2 answers

5


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 forand 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.

  • I could only answer you by answering my own question, take a look at my other comment, please.

  • 1

    The point is, I answered what you asked. Here is not a forum that we discuss the subject in interactions, is a site Q&A, you ask a question and receive an answer, so you need to think about the question you will ask to get the answer you want. If you have other doubts, then you should ask other questions, not give an answer as if it were a question.

  • Got it. It’s the first question I ask here on the website anyway, I still don’t know how it works right. I managed to solve the problem using 2 loops in the way you said (; i <= 10; i++) Sorry for the poorly prepared question, I wrote the post in a hurry and ended up not being very clear. Vlw for help. I will delete "topic".

-1

The initializer of the loop for runs only once, before entering the loop and can contain the following criteria:

  • The declaration and initialization of a local variable, accessible only within the loop

for (int i = 0; i < 5; i++)

  • Instructions for assigning, incrementing, decreasing or method invocation.
int i=0;
for(i=0; i < 5; i++)
for(i++; i < 5; i++)
`for(--i; i < 5; i++)
for(i = getRandomInt(); i < 5; i++)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.