469,645 Members | 1,706 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

question about for loops

This is from Programming in the Key of C#:

"You can initialize multiple variables in the for loop:

for (int i = 0, j = 0; ...

But it's an either/or situation. If you declare one or more variables of
the same type, you can't do anything else in the initializer. You can't
initialize one and declare another:

for (i = 0, int j = 7; ... // Won't work!"

Ok, this explanation doesn't seem to make much sense to me. The
sentences sound completely independent of one another, and almost
contradictory. First off, *what* exactly is the either/or situation here?

The main thing that confuses me is the sentence "You can't initialize
one and declare another." It seems that this is exactly what the first
example is doing. What's the difference between the above examples that
the first one is valid and the second isn't? I feel like the "int" in
the first example might be a typo.

Basically, what are the rules for declaring/initializing variables in
the for loop? Can you only do one or the other, and that's what the
above is trying to say? It's that first example that has me confused.

Thanks.
Nov 17 '05 #1
6 1249
John Salerno napisał(a):
This is from Programming in the Key of C#:

"You can initialize multiple variables in the for loop:

for (int i = 0, j = 0; ...

for (i = 0, int j = 7; ... // Won't work!"
The main thing that confuses me is the sentence "You can't initialize
one and declare another." It seems that this is exactly what the first
example is doing. What's the difference between the above examples that
the first one is valid and the second isn't? I feel like the "int" in
the first example might be a typo.


Hi

If I'm not mistaken the difference is that in the first example both
variables (i,j) are declared.

You can declare variables like this:
// good - after CSharp Coding Standards by Lance Hunt
int i = 0;
int j = 0;

or like this
//bad
int i = 0, j = 0;

As you can see in the first example both variables have been declared
but in the second one only second var has been. The first one was only
initialized with a value of zero.
best regards
Mateusz [PEYN] Adamus
Nov 17 '05 #2
John Salerno wrote:
This is from Programming in the Key of C#:

"You can initialize multiple variables in the for loop:

for (int i = 0, j = 0; ...

But it's an either/or situation. If you declare one or more variables
of the same type, you can't do anything else in the initializer. You
can't initialize one and declare another:

for (i = 0, int j = 7; ... // Won't work!"

Ok, this explanation doesn't seem to make much sense to me. The
sentences sound completely independent of one another, and almost
contradictory. First off, *what* exactly is the either/or situation
here?
The main thing that confuses me is the sentence "You can't initialize
one and declare another." It seems that this is exactly what the first
example is doing. What's the difference between the above examples
that the first one is valid and the second isn't? I feel like the
"int" in the first example might be a typo.

Basically, what are the rules for declaring/initializing variables in
the for loop? Can you only do one or the other, and that's what the
above is trying to say? It's that first example that has me confused.

Thanks.


in the first example, the part "int i=0, j=0" declares _both_ "i" and "j" and
initializes both to 0.

the second example tries to use an existing variable "i" and declare a "j".

The "either/or" is : *either* you declare _all_ variables, *or* you declare _none_.
So: either "for (int i=0,j=0 ; ..)",
or "int i; int j; for (i=0, j=0 ; ...)"
Hans Kesting
Nov 17 '05 #3
Hans Kesting wrote:
John Salerno wrote:
This is from Programming in the Key of C#:

"You can initialize multiple variables in the for loop:

for (int i = 0, j = 0; ...

But it's an either/or situation. If you declare one or more variables
of the same type, you can't do anything else in the initializer. You
can't initialize one and declare another:

for (i = 0, int j = 7; ... // Won't work!"

Ok, this explanation doesn't seem to make much sense to me. The
sentences sound completely independent of one another, and almost
contradictory. First off, *what* exactly is the either/or situation
here?
The main thing that confuses me is the sentence "You can't initialize
one and declare another." It seems that this is exactly what the first
example is doing. What's the difference between the above examples
that the first one is valid and the second isn't? I feel like the
"int" in the first example might be a typo.

Basically, what are the rules for declaring/initializing variables in
the for loop? Can you only do one or the other, and that's what the
above is trying to say? It's that first example that has me confused.

Thanks.

in the first example, the part "int i=0, j=0" declares _both_ "i" and "j" and
initializes both to 0.

the second example tries to use an existing variable "i" and declare a "j".

The "either/or" is : *either* you declare _all_ variables, *or* you declare _none_.
So: either "for (int i=0,j=0 ; ..)",
or "int i; int j; for (i=0, j=0 ; ...)"
Hans Kesting


Thanks guys, that makes sense now!
Nov 17 '05 #4
Mateusz [PEYN] Adamus <pe************@tlen.pl> wrote:
If I'm not mistaken the difference is that in the first example both
variables (i,j) are declared.

You can declare variables like this:
// good - after CSharp Coding Standards by Lance Hunt
int i = 0;
int j = 0;

or like this
//bad
int i = 0, j = 0;

As you can see in the first example both variables have been declared
but in the second one only second var has been. The first one was only
initialized with a value of zero.


Um, no, both have been declared in both examples. If you believe there
is some semantic difference between the two examples above, please give
an example where the difference is clearly shown.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
Jon Skeet [C# MVP] wrote:
Mateusz [PEYN] Adamus <pe************@tlen.pl> wrote:
If I'm not mistaken the difference is that in the first example both
variables (i,j) are declared.

You can declare variables like this:
// good - after CSharp Coding Standards by Lance Hunt
int i = 0;
int j = 0;

or like this
//bad
int i = 0, j = 0;

As you can see in the first example both variables have been declared
but in the second one only second var has been. The first one was only
initialized with a value of zero.

Um, no, both have been declared in both examples. If you believe there
is some semantic difference between the two examples above, please give
an example where the difference is clearly shown.


He means they weren't both declared in the second example of *my* post.
Nov 17 '05 #6
John Salerno <jo******@NOSPAMgmail.com> wrote:
Um, no, both have been declared in both examples. If you believe there
is some semantic difference between the two examples above, please give
an example where the difference is clearly shown.


He means they weren't both declared in the second example of *my* post.


Ah. That wasn't clear at all :)

(If that really was the intention though, I agree.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by 2mc | last post: by
5 posts views Thread by John Edwards | last post: by
5 posts views Thread by Lada 'Ray' Lostak | last post: by
8 posts views Thread by John Salerno | last post: by
2 posts views Thread by Max | last post: by
10 posts views Thread by Dick Moores | last post: by
19 posts views Thread by Zach Heath | last post: by
40 posts views Thread by brad | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.