This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a flower (🌹) or copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java (📚) this week in the Agile and Other Processes forum!
  • 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:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
master stewards:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
stewards:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Reader question: Tiger's for loop

 
pioneer
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Mr Schildt

I have been reading on the internet about the new for loop in Tiger.

The examples that I have been encountering have been about iterating through a collection or through some sort of Array or Vector. This is the one example that I found.




While I do use the for loop for things like this, sometimes I use the for loop to repeat codea set number of times not necessarily reading from a collection. (An example would be calculating prime numbers.) Can I still use the new for loop to do this?

Kind regards,
Rachel
 
pioneer
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact, the code you shown in your example is a simplified syntax for the original for syntax [for(int a = 0; a < arr.length; a++)]. However, if you are not passing in the collection, you may not be able to use the short form, as you even dont know when the loop end. In such sense, this may not work.

Nick
 
Rachel Swailes
pioneer
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So can we still use the long form of the for loop in Tiger?
 
pioneer
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Rachel Swailes:
So can we still use the long form of the for loop in Tiger?



Sure .. you can do that....
 
author
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure how useful this is, but using the new for loop, you can create a Sequence class to allow you to iterate over a specific sequence. The main method in the listing below demonstrates.



-j-
 
Author
Posts: 253
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rachel:

The enhanced for loop adds a "for-each" capability to the for loop. It does not replace the original for loop.

Here is a brief description of it from my book.


The general form of the for-each version of the for is shown here.

for(type itr-var : iterableObj) statement-block

Here, type specifies the type and itr-var specifies the name of an iteration variable that will recieve the elements contained in iterableObj, one at a time, from beginning to end. The object referred to by iterableObj must be an array, or an object that implements the new Iterable interface. In all cases, type must be the same as (or compatible with) the elements retrieved from iterableObj. Thus, when iterating over arrays, type must be the compatible with the base type of the array. With each iteration of the loop, the next element in iterableObj is retrieved and stored in itr-var. The loop repeats until all elements have been obtained.



One other point: you can stop a "for-each" style for loop early by using the break statement. For example, this code fragment is valid.

Here, even though nums contains 10 values, the loop stops after the value 5 has been read.
 
Nicholas Cheung
pioneer
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thus, in such sense, this feature is a short form of for-loop, and it can only apply to collection type only, isnt it?

Nick
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thus, in such sense, this feature is a short form of for-loop, and it can only apply to collection type only, isnt it?

No.

It's a short form of for loop, sure. But it does not apply only to Collections - it also applies to arrays, and to anything else which implements the Iterable interface. There are several examples in this thread of for loops with arrays, which are not the same as collections.
 
Rachel Swailes
pioneer
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for that great explination! I look forward to playing with Tiger soon!

Take care,
Rachel
 
pioneer
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Rachel Swailes:
I look forward to playing with Tiger soon!



Don't you afraid that Tiger might bite you?

Just kidding...

I got invaluable knowledge about the new for-loop from this thread... At least I know how to loop using the new enhanced for-loop, they can be applied to collections classes as well as arrays and so on....
 
Nicholas Cheung
pioneer
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have started playing with Tiger, however, I wish the offical release will come out soon.

Nick
 
Rachel Swailes
pioneer
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ko Ko Naing:
[QB]

Don't you afraid that Tiger might bite you?



Even without Tiger yet, Java still bites me when I haven't done proper research! At least now I know how the new for loop works so it's one less place to get bitten in future...

Take care,
Rachel
 
Nicholas Cheung
pioneer
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dont worried, you will learn more when more books and tutorials come out.

Nick
 
Rachel Swailes
pioneer
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sure that when we start using Tiger in our production environment that I will learn more about it because I'll just have to!

(Then ironically Survivor's Eye of the Tiger started playing on my MP3 player...)

Cheers,
Rachel
 
pioneer
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My version of your for(int i = 0; i < limit; i++) loop (don't take it seriously ):

 
Jeff Langr
author
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:
My version of your for(int i = 0; i < limit; i++) loop (don't take it seriously ):



Interesting! But far more costly than the Sequence class... ;-)

-J-
 
Oh the stink of it! Smell my tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!