• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

for loop expressions

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Marcela and sdev.
 
mita
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot everyone.I appreciate it.
Mita
 
Wink, wink, nudge, nudge, say no more, it's a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic