wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes for loop expressions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "for loop expressions" Watch "for loop expressions" New topic
Author

for loop expressions

mita
Ranch Hand

Joined: Jul 19, 2000
Posts: 50
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

Joined: May 26, 2002
Posts: 79
I can not see a problem with any of your code. Did you try compiling and running them?
mita
Ranch Hand

Joined: Jul 19, 2000
Posts: 50

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

Joined: Feb 14, 2008
Posts: 186
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).]


"SCJP5 | SCWCD5| DEVELOPER"
Marcela Blei
Ranch Hand

Joined: Jun 28, 2000
Posts: 477
>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

Joined: May 26, 2002
Posts: 79
Thanks, Marcela and sdev.
mita
Ranch Hand

Joined: Jul 19, 2000
Posts: 50
Thanks a lot everyone.I appreciate it.
Mita
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: for loop expressions
 
Similar Threads
Q from John Hunt M.E
Array Indexing
Operators
hi i need help..please help me...
loop