As Noung and Eevee's writeups show, a foreach command makes for short, clear code. You perform some command block for each value in a given list or array. Is foreach really the same as for (or is one of them plain "better")? The names are confusing, and several people (or programming languages) seem not to make the distinction. foreach is distinct from for in that it requires a list (or whatever) of all the values to be used. For long loops, this means a lot of memory. Also, you need to know before you start the loop what values you'll want to go through (in other words, the values the loop variable is assigned cannot depend on calculations performed in the loop itself).

This makes foreach useful for a lot of scripting, where you not only know the values ahead of time, you probably anyway got them as a complete list (e.g. for a glob, or previous text processing. When what you're doing is more computational, what you want is a for loop.