• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why For loop why

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why such contradiction ?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting. It seems you have to declare y within the for statement for this to work. You can't use predeclared variables.
This works

You will probably need to read the Java Language Specification to find out why this is.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:Interesting. It seems you have to declare y within the for statement for this to work. You can't use predeclared variables.
This works

You will probably need to read the Java Language Specification to find out why this is.


Yes, please tell me the reason.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saloni jhanwar wrote:Why such contradiction ?



i have read this thing in kb6 book. the book says the syntax for for-each loop is as follows :

for(Declaration:expression) {}

i guess it must be stated in JLS too.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:
saloni jhanwar wrote:Why such contradiction ?



i have read this thing in kb6 book. the book says the syntax for for-each loop is as follows :

for(Declaration:expression) {}

i guess it must be stated in JLS too.


Yes,please tell me the reason.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saloni jhanwar wrote:Yes,please tell me the reason.

Did you read the Java Language Specification ?
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
saloni jhanwar wrote:Yes,please tell me the reason.

Did you read the Java Language Specification ?


I can't read because really i don't know where it is stated.
 
Henry Wong
author
Marshal
Pie
Posts: 21518
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saloni jhanwar wrote:
Joanne Neal wrote:Interesting. It seems you have to declare y within the for statement for this to work. You can't use predeclared variables.
This works

You will probably need to read the Java Language Specification to find out why this is.


Yes, please tell me the reason.


saloni jhanwar wrote:
Joanne Neal wrote:
Did you read the Java Language Specification ?


I can't read because really i don't know where it is stated.


The JLS doesn't explain why, it just explains what the enhanced for loop does...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.14.2

The enhanced for loop, calls the variable a formal parameter, which is defined to require a type (and optional modifiers). This is different from the basic for loop, defined one section earlier, which can take either a declaration or an expression list.

Henry
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
enhanced for loop has been added in java 1.5. it forces developer to declare variable in smallest scope as much as possible to avoid confusion(for example: avoiding copy paste error, etc...). declare variable when it needed,

int i;
//blah, blah
i = 10;

replace above idem with below one

//blah, blah
int i = 10;

do you get feel i am diverting the question?

read this then JLS:forEach

shortly, in for in loop you must declare variable inside the brackets ( ).
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry nicely beaten me there!
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry,i will try to read this
 
Nidhi Dhariwal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is as per the java rule ..the enhanced for loop is defined as for(declaration : expression) where declaration part has to be newly declared block variable compatible with the elements of array you are accessing and is available within loop only.
expression is an array you want to loop or method returning that.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is. This is the relevant section. The enhanced for loop is just syntactic sugar, meaning that it effectively gets converted to equivalent code using an Iterator or array loop. And that conversion is presumably making assumptions about the structure of the loop - it's expecting a declaration in the loop, not an assignment statement.

Edit: I really took too long typing that, didn't I?
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Yes, it is. This is the relevant section. The enhanced for loop is just syntactic sugar, meaning that it effectively gets converted to equivalent code using an Iterator or array loop. And that conversion is presumably making assumptions about the structure of the loop - it's expecting a declaration in the loop, not an assignment statement.

Edit: I really took too long typing that, didn't I?


Thanks Matthew
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nidhi Dhariwal wrote:

Welcome to the Ranch, Nidhi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic