File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why For loop why Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why For loop why" Watch "why For loop why" New topic
Author

why For loop why

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Why such contradiction ?


Tell the difficulties that i am difficult.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
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.

Joanne
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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

Joined: Apr 04, 2012
Posts: 923
    
    1

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.


OCPJP 6(100 %) OCEWCD 6(91 %) OCPJBCD(93%)
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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

Joined: Aug 05, 2005
Posts: 3169
    
  10
saloni jhanwar wrote:Yes,please tell me the reason.

Did you read the Java Language Specification ?
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jan 28, 2008
Posts: 5575

Henry nicely beaten me there!
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Thanks Henry,i will try to read this
Nidhi Dhariwal
Greenhorn

Joined: May 11, 2012
Posts: 2
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

Joined: Apr 06, 2010
Posts: 4240
    
    7

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

Joined: Feb 09, 2012
Posts: 583

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
Bartender

Joined: May 31, 2011
Posts: 1776
Nidhi Dhariwal wrote:

Welcome to the Ranch, Nidhi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why For loop why
 
Similar Threads
Scope coding question
A simple calculation of -- ((-y--)
Why does x=x++ cause infinite loop?
for loop vs while loop
query on for loop