for loop

A

Arjen

Hi,

I'm using multiple times the for loop in my application.

All times I start with int i = 0. VS says that I can only use the i once.
So, now I'm using i, j, h and x... is there a better way?... I like to use
the same variable or an other method.

Thanks!
 
P

Peter van der Goes

Arjen said:
Hi,

I'm using multiple times the for loop in my application.

All times I start with int i = 0. VS says that I can only use the i once.
So, now I'm using i, j, h and x... is there a better way?... I like to use
the same variable or an other method.

Thanks!

Sure.
Declare and initialize your loop control variable in the for() header like
this:

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

{

Console.WriteLine("First loop");

}

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

{

Console.WriteLine("Second loop");

}

That way i is local to the loop itself.
 
K

Kevin Spencer

A variable is scoped. If you declare a variable at the top of a function,
you can use it throughout the function, but not outside of it. If you
declare a variable in a looping construct, you can only use it inside that
looping construct. If you declare it at class level, you can use it
throughout the class. Of course, at class level, it is a field, and you can
control access to it from outside the class as well. However, if you're
using a variable for looping, your best bet (if you want to save yourself a
lot of grey hairs) is to declare it at the top of your function, and reuse
it only inside that function.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
M

Michael A. Covington

Arjen said:
Hi,

I'm using multiple times the for loop in my application.

All times I start with int i = 0. VS says that I can only use the i once.
So, now I'm using i, j, h and x... is there a better way?... I like to use
the same variable or an other method.

Thanks!

Create it once and use it many times.

int i;

for (i=0; i<25; i++) { something; }

for (i=0; i<100; i++) { somethingelse; }
 
J

Jon Skeet [C# MVP]

Michael A. Covington said:
Create it once and use it many times.

int i;

for (i=0; i<25; i++) { something; }

for (i=0; i<100; i++) { somethingelse; }

Unless you actually *need* it outside the loop, I find it much nicer to
keep the scope tighter:

for (int i=0; i < 25; i++)
{
something;
}

for (int i=0; i < 100; i++)
{
something;
}
 
J

Jako Menkveld

Jon Skeet said:
Unless you actually *need* it outside the loop, I find it much nicer to
keep the scope tighter:

for (int i=0; i < 25; i++)
{
something;
}

for (int i=0; i < 100; i++)
{
something;
}


What about nested loops...
 
J

jeremiah johnson

Doesn't that question answer itself?

if you're in scope for i, and i is your usual for variable, then use
another letter for the nested for loop.
 
J

Jako Menkveld

It does, you're right, but the question originally posted question did
specify whether the loops are nested or not. In nested loops you have to
used multiple counter variables and as far as I know there's no other way to
do that. I just wanted to point that out...
 
M

Mabden

A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on Usenet and in email?
 
K

Kevin Spencer

For a programmer to declare a matter of personal choice and preference to be
"the most annoying this on Usenet and in email" as if this were a universal
opinion, fact, or some kind of standard, displays a lack of logic and/or
knowledge. I refer you to:

http://en.wikipedia.org/wiki/Top-posting

As for my own reasons for top-posting, old information, such as that which
has been previously stated, is less important than information which solves
a problem. I quote the previous message and/or thread to provide context,
and to assist with newsreader software which hides previously read messages.
I post the solution to a problem, or my contribution to the solution to a
problem at the top of the message, where it is easily found and read. If I
want to reply to individual portions of a message, I will re-post those
portions, and reply in-line. It is my opinion that this makes it easier, in
the context of problem-solving, for the OP to find the relevant information
they need, and therefore, solve their problem quickly.

I have been participating in newsgroups for over a dozen years. I am well
aware of the standards of netiquette. I do not participate in newsgroups for
conversational purposes, nor do I "chat." That is a waste of my precious
time. In the context of a support newsgroup, I have developed a style which
I feel is most appropriate for this particular venue. However, I would never
make the mistake of thinking that my methodology in replying was better than
someone else's, or that it constituted in any way some sort of "standard."
In fact, unless I find an answer unreadable, it is of no concern to me what
style the poster used to reply. That is also of no importance, and not worth
my precious time. Helping people, and contributing to the betterment and
happiness of myself and mankind certainly is worth my time, and that's why I
participate.

If I give someone incorrect technical information, I welcome correction. The
only way to become right is to admit when one is wrong. It is my sincere
desire to be helpful, and to become the best programmer I can be.

When someone corrects me regarding a matter of personal choice and/or style,
as if their opinion was some sort of authority, it merely demonstrates a
flaw in that person's reasoning ability, and that person's credibility is
damaged, particularly if that person is a programmer.

When a person majors in the minors, it indicates a flaw in that person's
sense of priority.

When a person criticizes for no helpful purpose, it indicates flaw in their
logic (there is no logical reason for such criticism). It indicates a flaw
in their sense of priority (as the criticism is concerning something of
little or no importance). It may also indicate a lack of self-esteem. People
with low self-esteem often compensate by trying to find fault in others.

This information may be useful to you, if you are concerned about
self-improvement, the well-being of those whom you interact with, or your
own sense of well-being and/or self-esteem.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
M

Mabden

Please don't top-post.

Kevin Spencer said:
For a programmer to declare a matter of personal choice and preference to be
"the most annoying this on Usenet and in email" as if this were a universal
opinion, fact, or some kind of standard, displays a lack of logic and/or
knowledge. I refer you to:

http://en.wikipedia.org/wiki/Top-posting

As for my own reasons for top-posting, old information, such as that which
has been previously stated, is less important than information which solves
a problem. I quote the previous message and/or thread to provide context,
and to assist with newsreader software which hides previously read messages.
I post the solution to a problem, or my contribution to the solution to a
problem at the top of the message, where it is easily found and read. If I
want to reply to individual portions of a message, I will re-post those
portions, and reply in-line. It is my opinion that this makes it easier, in
the context of problem-solving, for the OP to find the relevant information
they need, and therefore, solve their problem quickly.

I have been participating in newsgroups for over a dozen years. I am well
aware of the standards of netiquette. I do not participate in newsgroups for
conversational purposes, nor do I "chat." That is a waste of my precious
time. In the context of a support newsgroup, I have developed a style which
I feel is most appropriate for this particular venue. However, I would never
make the mistake of thinking that my methodology in replying was better than
someone else's, or that it constituted in any way some sort of "standard."
In fact, unless I find an answer unreadable, it is of no concern to me what
style the poster used to reply. That is also of no importance, and not worth
my precious time. Helping people, and contributing to the betterment and
happiness of myself and mankind certainly is worth my time, and that's why I
participate.

If I give someone incorrect technical information, I welcome correction. The
only way to become right is to admit when one is wrong. It is my sincere
desire to be helpful, and to become the best programmer I can be.

When someone corrects me regarding a matter of personal choice and/or style,
as if their opinion was some sort of authority, it merely demonstrates a
flaw in that person's reasoning ability, and that person's credibility is
damaged, particularly if that person is a programmer.

When a person majors in the minors, it indicates a flaw in that person's
sense of priority.

When a person criticizes for no helpful purpose, it indicates flaw in their
logic (there is no logical reason for such criticism). It indicates a flaw
in their sense of priority (as the criticism is concerning something of
little or no importance). It may also indicate a lack of self-esteem. People
with low self-esteem often compensate by trying to find fault in others.

This information may be useful to you, if you are concerned about
self-improvement, the well-being of those whom you interact with, or your
own sense of well-being and/or self-esteem.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Ambiguity has a certain quality to it.
 
J

Jon Skeet [C# MVP]

Mabden said:
Please don't top-post.

He says, top-posting...

Mabden, while I too dislike top-posting, this group has never been
particularly fussy about it, as far as I've seen.

I suggest that if top-posting offends you that much, you just ignore
posts which are top-posted. Harrassing people about it doesn't help
anyone, IMO.
 
K

Kevin Spencer

Please don't top-post.

People who major in the minors generally minor in the majors.

From my research on Google, this is not the exception, but the rule when it
comes to Mabden.

If you have nothing meaningful to contribute, I suggest you contribute
nothing.

--

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
M

Mabden

Kevin Spencer said:
People who major in the minors generally minor in the majors.

Much better. Thank you for your indulgence.

While I understand your reasoning in your prior posts to why your
particular preference is best for yourself, and you hint that you have
read the pertaining wisdom about how the consensus of the Internet
community has come to embrace interleaving comments (or at least putting
them at the bottom), I can also understand how one who has been "brought
up wrong" can see "their way" as best of all.

The point is, it is not about "your way", or your comfort level - as you
claim to know. Well, then, there is really nothing left to argue about.
You want to top-post. I get it. Everyone who invented the technology you
are using to do it have decided through 30+ years of posting messages
that top posting is difficult to read. But you want to top post. No
longer through ignorance, but through petty self-indulgence. Good call.
I'll start getting the word out. Ohhhh...., I just realized I prefer to
interleave / bottom post. Sorry, can't help.

Good luck with that, tho! I expect someone else will come along in a
little while and ask you not to top-post again. And again. But, hey,
live your life - you Rebel! You are "stickin' it to the Man!" You go,
Girl!
From my research on Google, this is not the exception, but the rule when it
comes to Mabden.

The Mabden are a race dedicated to tearing down the Elder races, and who
work for the forces of Chaos. This does not imply a lack of
understanding of the Elder race's technology and tools. You would make a
fine Mabden and as a leader I could find you a suitable name if you wish
to join. You suit our purpose exactly, if somewhat subtly. In general, a
Mabden horde errs to the side of pure destruction rather than your
methods of infection from within, but I like your moxie and pluck, and
willingness to give up your principles at the drop of a dime. You've
considered being a lawyer haven't you?! Be honest!
If you have nothing meaningful to contribute, I suggest you contribute
nothing.

I suggest you review my posts. Your suggestion has been amply fulfilled!
 
K

Kevin Spencer

You're a legend in your own mind, dude. Hasta la vista, baby.

--

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top