• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

For Loop  RSS feed

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Hello
{
void method()
{
for(int x=0;x<4;x++)
int y=-1;
System.out.println(y);

}
}
class HelloDrive
{
public static void main(String[] args)
{
Hello h=new Hello();
h.method();
}
}

When i am compiling the above code
it's giving compile time error
1. '.class' expected
2. int y=-1;(Its not a statement)
But when i am doing
like

for(int x=0;x<4;x++)
{
int y=-1;
System.out.println(y);
}
Its not giving error
WHY
 
Ranch Hand
Posts: 145
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you are writing

for(int i=0;i<3;i++)
int y=-1;

then what is happening is : declaration statement becomes a part of a loop and y is declared again and again on the same local stack, hence error.

But when you say :
for(int i=0;i<3;i++){
int y=-1;
}

A new block is created and within block every time y is created and destroyed on entry and exit to the block , hence no error.
 
Sabber bhatia
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks KAN
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
it works fine with me, I dont know where is the problem
 
Nancy Antony
Ranch Hand
Posts: 145
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you send the code ?
 
Sabber bhatia
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey kan
i have a problem
I understand as you said "y is declared again and again in local stack"
but what if i will do like


for(int i=0;i<1;i++)
int y=-1
System.out.println(y);

Then same error is comming,Now only once y is declared in local stack as loop is executing once
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here

is a Local Variable Declaration Statement and it should appear within a Block.
 
Nancy Antony
Ranch Hand
Posts: 145
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even now also it is declared again and again as for loop has only one statement to be executed and that is int y=-1; (should end with semicolon, otherwise syntax error).

for( int i=0; i<3; i ++) ; // this semicolon tells the end of for loop this for loop has only one statement and that is null sattement that do nothing. We can create delay loops like this.

so

for(int i =0; i<3;i++) ;
int y=-1;

Now its valid as y will be declared only once, as it is not a part of for loop.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!