473,394 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 1373
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: 2mc | last post by:
Generally speaking, if one had a list (from regular Python) and an array (from Numerical Python) that contained the same number of elements, would a While loop or a For loop process them at the...
3
by: Carlos Ribeiro | last post by:
As a side track of my latest investigations, I began to rely heavily on generators for some stuff where I would previsouly use a more conventional approach. Whenever I need to process a list, I'm...
5
by: John Edwards | last post by:
Hello, I have sort of a newbie question. I'm trying to optimize a loop by breaking it into two passes. Example: for(i = 0; i < max; i++) {
5
by: Lada 'Ray' Lostak | last post by:
Dear list, First of all I want to say sory, if my question was answered somewhere. Or if it is my fault. If so, please, give me link/hint. My own search fails :( I have experimence with MySql,...
8
by: Együd Csaba | last post by:
Hi All, how can I improve the query performance in the following situation: I have a big (4.5+ million rows) table. One query takes approx. 9 sec to finish resulting ~10000 rows. But if I run...
8
by: John Salerno | last post by:
Ok, for those who have gotten as far as level 2 (don't laugh!), I have a question. I did the translation as such: import string alphabet = string.lowercase code = string.lowercase + 'ab'...
2
by: Max | last post by:
Hi, Group. I'm not a Python programmer so this question may be really basic or stupid. :) I have some code that sends a simple request to an end-point and reads the response. That works just...
10
by: Dick Moores | last post by:
I'm still trying to understand classes. I've made some progress, I think, but I don't understand how to use this one. How do I call it, or any of its functions? It's from the Cookbook, at...
19
by: Zach Heath | last post by:
I'm just starting C and haven't done programming for a few years...could you guys please take a look at this? Thanks for your time! I have an input file that looks like: 1.5 2.5 Bob, Joe...
40
by: brad | last post by:
Will len(a_string) become a_string.len()? I was just reading http://docs.python.org/dev/3.0/whatsnew/3.0.html One of the criticisms of Python compared to other OO languages is that it isn't OO...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.