• 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

Peculiar Issue with For Loop

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

The below code works fine and prints the value of j 10 times.

But I re-wrote the code by removing the flower braces("{") for the for loop but i am getting compile time errors.


The error i got was,
Loopy.java:6: '.class' expected
int j=10;
^
Loopy.java:6: not a statement
int j=10;
^
Loopy.java:6: illegal start of expression
int j=10;
^
Loopy.java:6: ';' expected
int j=10;

But I expected the value of J will be printed only once is that not the case or is it a bug in Java? Please suggest.
^

 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you dont enclose the for loop body within the {}- The statement immediately preceding the for-loop declaration will be considered as part of the loop body. So in the example provided-

will be part of the loop body and

will be any other statement which executes after the completion of the for loop. And as variable j scope is limited to the for-loop, when used outside the for-loop it will lead to compilation error. As for the compilation error you are seeing- Its due to the fact the the for loop has only declaration.

Update: Possible reason would be- You are trying to declare the same variable within the scope of the loop more than once. So its kind of complaining. Oh yeah I didnt notice that And actually the compiler doesnt even reach the println statement, but has an error just before it- i.e in the declaration of the variable.
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interpreting error messages takes practice, but who wants to practice that? Unfortunately, I've had lots of practice. Error messages point to a problem, but the actual coding error may be in the proceeding or succeeding code phrase. And forget about the actual error statement, or as in your case, several error statements which point to the same problem.

Your case is a 'scope' issue. The scope of a loop is easier to see when you use the curly braces. In your second example, the for loop ends with the semi-colon at the end of the int j = 10; statement, so 'j' isn't visible past that point. Your println statement can't 'see' j.
 
Bala jee
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But how come the same code works for the first program i have written?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It isn't the same code. The addition of the braces changes it.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for, while, if and else statements all have exactly one statement as body. A declaration (int j = 10) is not a statement. A block (indicated by { and }) is. Inside blocks declarations and statements are both allowed.
 
Bala jee
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob Prime,

Correct me if i am wrong, what i understood if a for,while,if and else does not have a block then only a statement is allowed and not an declaration, and both declaration and statement is allowed only if the for has a block.

so the below example will work fine is that correct,
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The semicolon ends a statement. So in this case you ended an empty statement. Not the loop.
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does being able to use "j" prove anything about "i"?
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lester Burnham wrote:How does being able to use "j" prove anything about "i"?



I think the "i" in the comment stood for "I"

Loop doesnt terminate- It completes the iterations but no statement is executed as part of it.
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohamed sanaullah wrote:I think the "i" in the comment stood for "I"


Ah yes, that makes more sense.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wouter Oet wrote:The semicolon ends a statement. So in this case you ended an empty statement. Not the loop.



Are you sure? Maybe I misunderstand your point, but I'm pretty sure the semi-colon by itself on line 06 ends the 'for' loop. I've happened onto that undesired for loop termination more than once (I think).
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Greg Brannon wrote:

The semicolon ends a statement. So in this case you ended an empty statement. Not the loop.


Maybe I misunderstand your point, but I'm pretty sure the semi-colon by itself on line 06 ends the 'for' loop.


You're both right. The semicolon ends an empty statement, and that empty statement constitutes the body of the for loop - at the end of which the for loop is done.
 
Bala jee
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I really did not understand what you guys are discussing about the code? Can some one explain me what exactly happens for the code which i posted in my previous comment?
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I really did not understand what you guys are discussing about the code? Can some one explain me what exactly happens for the code which i posted in my previous comment?



Did you try executing it? The loop pretty much does nothing- Just completes its slated iterations and then the program continues with the execution of the statement after the loop.
reply
    Bookmark Topic Watch Topic
  • New Topic