Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > enhanced for loop

Reply
Thread Tools

enhanced for loop

 
 
visionset
Guest
Posts: n/a
 
      05-04-2007
Why did they decide to restrict the variable scope for enhanced for loops?

It is handy sometimes to have the extra scope.

This won't compile:

String[] strings = {"foo"};

String str;

for (str : strings) {}



--

Mike W


 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      05-04-2007
"visionset" <(E-Mail Removed)> writes:
>String[] strings = {"foo"};
>String str;
>for (str : strings) {}


public class Main
{ public static void main( final java.lang.String[] args )
{ final java.lang.String[] strings ={ "foo" };
for( final java.lang.String str : strings ); }}

 
Reply With Quote
 
 
 
 
Jussi Piitulainen
Guest
Posts: n/a
 
      05-04-2007
Stefan Ram writes:
> visionset writes:
>> String[] strings = {"foo"};
>> String str;
>> for (str : strings) {}

>
> public class Main
> { public static void main( final java.lang.String[] args )
> { final java.lang.String[] strings ={ "foo" };
> for( final java.lang.String str : strings ); }}


Q. Why is it so?
A. It is so.
 
Reply With Quote
 
visionset
Guest
Posts: n/a
 
      05-05-2007

"Stefan Ram" <(E-Mail Removed)-berlin.de> wrote in message
news:(E-Mail Removed)-berlin.de...
> "visionset" <(E-Mail Removed)> writes:
>>String[] strings = {"foo"};
>>String str;
>>for (str : strings) {}

>
> public class Main
> { public static void main( final java.lang.String[] args )
> { final java.lang.String[] strings ={ "foo" };
> for( final java.lang.String str : strings ); }}


I think you miss my point!
It was a question of scope, not how to construct an enhanced for loop
(despite the subject line).

--
Mike W


 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      05-05-2007
"visionset" <(E-Mail Removed)> writes:
>I think you miss my point!


Yes. Sorry.

>It was a question of scope, not how to construct an enhanced for loop


I see.

Often the restricted scope will be sufficient.

Otherwise,

for( i = 0; i < strings.length; ++ i )str = strings[ i ];

or

for( final java.lang.String s : args ){ str = s; /* ... */ }

can be used

When the scope is limited, the loop is better decoupled from
its anvironment, which might help to refactor the code, e.g.,
to move the loop to another method.

 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      05-05-2007
"visionset" <(E-Mail Removed)> writes:

> Why did they decide to restrict the variable scope for enhanced for loops?

....
> String[] strings = {"foo"};
>
> String str;
>
> for (str : strings) {}


What is the expected value of "str" after this?
Why?
What if "strings" was the empty array?

I could see the value being the last value assigned, or null, or
unassigned.

If you want to capture the value, just do:

String str = null;
for(String strx : strings) {
str = strx;
// ...
}

/L
--
Lasse Reichstein Nielsen - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      05-05-2007
Lasse Reichstein Nielsen <(E-Mail Removed)> wrote:
> "visionset" <(E-Mail Removed)> writes:
>
> > Why did they decide to restrict the variable scope for enhanced for loops?

> ...
> > String[] strings = {"foo"};
> >
> > String str;
> >
> > for (str : strings) {}

>
> What is the expected value of "str" after this?


That's a very good question, in general.

> What if "strings" was the empty array?
>


This seems more obvious to me. Since the array may be empty, the loop
would certainly not result in str becoming definitely assigned. So the
obvious behavior would be that an attempt to use it after the loop in
this case would fail; and if str were definitely assigned before the
loop, it would remain the same.

What's less obvious to me is what to do when the variable is definitely
assigned, and there are elements. In that case, leaving it as the last
element of the array seems like it's letting the implementation leak
through, though I admit it's a minor concern. After all, if one wants
the last element of the array, one should just get it as strings
[strings.length - 1].

The only case where this expanded scope seems both natural and useful is
when one exits the loop with a break statement and later wants to know
what value caused the break.

--
Chris Smith
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      05-05-2007
visionset wrote:
> Why did they decide to restrict the variable scope for enhanced for loops?
>
> It is handy sometimes to have the extra scope.
>
> This won't compile:
>
> String[] strings = {"foo"};
>
> String str;
>
> for (str : strings) {}
>
>
>


I think the enhanced for statement deals very simply with some very
simple, but common, cases. It does not have all the "handy sometimes"
features it might have, but anything it does not handle can always be
done using a basic for statement.

Patricia
 
Reply With Quote
 
visionset
Guest
Posts: n/a
 
      05-06-2007

"Patricia Shanahan" <(E-Mail Removed)> wrote in message
news:f1gs5n$1n1i$(E-Mail Removed)...
> visionset wrote:
>> Why did they decide to restrict the variable scope for enhanced for
>> loops?
>>
>> It is handy sometimes to have the extra scope.
>>
>> This won't compile:
>>
>> String[] strings = {"foo"};
>>
>> String str;
>>
>> for (str : strings) {}
>>
>>
>>

>
> I think the enhanced for statement deals very simply with some very
> simple, but common, cases. It does not have all the "handy sometimes"
> features it might have, but anything it does not handle can always be
> done using a basic for statement.
>


That sums it up nicely, and for sure, it is a very minor concern.

The prompt for the question was the typical way boilerplate code is made
more compact by reusing variables, for example with MenuItems.

MenuItem item;

item = new MenuItem("foo");
menu.add(item);
//...
item = new MenuItem("bar");
menu.add(item);
//...
for (item : blah.createMenuItems()) {
menu.add(item);
}

--
Mike W


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      05-06-2007
visionset wrote:
> The prompt for the question was the typical way boilerplate code is made
> more compact by reusing variables, for example with MenuItems.
>
> MenuItem item;
>
> item = new MenuItem("foo");
> menu.add(item);
> //...
> item = new MenuItem("bar");
> menu.add(item);
> //...
> for (item : blah.createMenuItems()) {
> menu.add(item);
> }


I would recommend not using the same variable in the loop but using a local
one, unless as Patricia said you need the loop value exposed afterward. There
is too much chance of interaction between code blocks when you don't restrict
the scope of the variable. The times when you should reuse variables in the
way you show are few and narrowly prescribed.

--
Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
jdk 1.5 enhanced for loop, Iterable and arrays Remi Bastide Java 5 03-29-2010 11:28 AM
PEP 315: Enhanced While Loop Daniel Cotton Python 0 06-04-2009 03:41 PM
Enhanced Tera Term...very nice. Hansang Bae Cisco 2 01-28-2005 05:29 AM
PEP-0315--Enhanced While Loop: An idea for an alternative syntax Andrew Koenig Python 46 02-24-2004 07:14 PM



Advertisments