• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Ej King
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
 
Ej King
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 56553
172
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!