This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

For Loop

 
Sabber bhatia
Ranch Hand
Posts: 59
  • 0
  • 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
 
Nancy Antony
Ranch Hand
Posts: 145
Java Linux Oracle
  • 0
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks KAN
 
horri khaled
Ranch Hand
Posts: 36
  • 0
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you send the code ?
 
Sabber bhatia
Ranch Hand
Posts: 59
  • 0
  • 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
 
Irina Goble
Ranch Hand
Posts: 91
  • 0
  • 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
  • 0
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic