Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

for loop expressions

 
mita
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In RHE, it was there that we can't mix expressions with variable declarations & this is illegal.
int i;
for (i =7, int j = 0; i < 10; j++){}
But in the question 3 of that chapter,
int j = 0;
for (int k = 0; j + k != 10; j++, k ++){} it says is legal.
Could someone explain this to me.
Thanks in advance.
mita
 
thomas
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can not see a problem with any of your code. Did you try compiling and running them?
 
mita
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The first code gives compilation error saying Invalid expression statement.But the second code compiles fine.
Please explain what is the difference.
Mita
 
sasank manohar
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi mita,
In the first code , you are mixing up the initializtion and declaration in a statement of for loop
i.e., for(i=1, int j=0; i<10; j++) { }

Here i=1, int j=0; is a statment because it ends with a semicolon (here you are mixing initializtion and declaration)
You can declare two variables and initialize them in a single statement in only one way that is.,

int i=1,j=0; // this is Ok.

but,
int i;
i=1 ,int j=0; // this is wrong, becuase you are trying to mix two statements with a comma.
whereas
int i;
i=1; int j=0; // this is right, because you are ending the statment i=1 with semicolon.

Whereas seconde code is right, you are not mixing up anything.

[This message has been edited by sdev (edited August 02, 2000).]
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>int i;
>for (i =7, int j = 0; i < 10; j++){}<br /> In a for statement, in the initialize block, you can�t declare a variable and don�t declare the other, if you make a declaration the declaration must include all the variables you refered.<br /> Eg: int i = 7, j = 0 where both i and j are declared in the same statement and have the for scope. Look that in for example i is declared outside the for and remember that you can, when declaring variables, specify the type only once: int i; int x is the same as int i, x<br /> >int j = 0;
>for (int k = 0; j + k != 10; j++, k ++){} it says is legal.
Yes, you are declaring k only in the initialize block.
For a complete detail of the for statement refer to: JLS 2nd
 
thomas
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Marcela and sdev.
 
mita
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot everyone.I appreciate it.
Mita
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic