• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How in the dickens does a for each loop work (syntax)?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello~

Coming from a C++/VB background, I am now learning JAVA.

My professor wants us to use a for/each loop, and I cannot find the syntax in JAVA. I have an idea that it works like a "while" loop, but still would like to understand syntax. I also used a "for each" working with arrays of user entry in a VBA for Excel application.

I have tried my textbook and other books (incl. JAVA(R) for Dummies(R)), and also the oracle.com JAVA website, without success.

Does it use some kind of "filter" in the array? Like for each [element] that == "purple", do this loop?

Any direction would be appreciated.

Thanks!



EJ
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The for statement
 
Ej King
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:The for statement



Thank you, Bear.

My "for" loops are doing great. I don't know how a "for each" loop is different. Is it just an "enhanced for loop"?

 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a way to loop through a collection without needing indexing.

Let's say we had an array of TeddyBear instances:



If we wanted to loop through them without needing indexes:



It's about as close to for..each as Java gets.

It's really useful for other collections such as List, Map, and Set as well.
 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You pronounce…“For each TeddyBear bear in picnic”…, so I think the enhanced for is the equivalent of a for‑each, yes. A lot of Java™ people call enhanced for for‑each. I think you will find the ordinary for loop has just about the same syntax as C++
 
Ej King
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

You pronounce…“For each TeddyBear bear in picnic”…, so I think the enhanced for is the equivalent of a for‑each, yes. A lot of Java™ people call enhanced for for‑each. I think you will find the ordinary for loop has just about the same syntax as C++



Thank you!

I had been looking for the literal "for each".

So, this enhanced loop is quite handy for those days when you just don't know how many bears are going to show up at your picnic! It allows you to expand your array without worrying about the parameters in the for statement.

I'm curious now: Is there a limit to what size of array the "enhanced for" will handle? Memory limitation?

 
Ej King
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:
It's really useful for other collections such as List, Map, and Set as well.



Thanks. I'll play with those, as well.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ej King wrote:
So, this enhanced loop is quite handy for those days when you just don't know how many bears are going to show up at your picnic! It allows you to expand your array without worrying about the parameters in the for statement.

I'm curious now: Is there a limit to what size of array the "enhanced for" will handle? Memory limitation?



Keep in mind that the foreach loop is just syntactic sugar. For arrays, the compiler will simply convert it to a regular for loop -- with an unnamed variable to index the array. There is nothing that can be done with the foreach loop that can't be done with the regular for loop.

There is no specified limitation, so I guess the limits are the supported size of arrays for Java, and the amount of available memory on the machine.

Henry
 
Campbell Ritchie
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote: . . . Keep in mind that the foreach loop is just syntactic sugar. For arrays, the compiler will simply convert it to a regular for loop -- with an unnamed variable to index the array. . . .

You do realise that means the loop variable is a read‑only variable?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic