question about for loops

  • Thread starter Thread starter John Salerno
  • Start date Start date
J

John Salerno

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.
 
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
 
John said:
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
 
Hans said:
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!
 
Mateusz [PEYN] Adamus said:
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 said:
Mateusz [PEYN] Adamus said:
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.
 
John Salerno said:
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.)
 
Back
Top