What is a For Loop
-
FOR LOOPS are one of the most common and useful programming constructs and are used pretty much everywhere. Even if you aren't a programmer, understanding what it is when you see it or realizing that you can use it for admin tasks almost constantly is a huge boon to efficiency.
In BASH, which has a pretty basic loop structure, the format looks like this...
for i in my_list; do a_thing; done
It is common to break this out to multiple lines for easier reading, but there is no need if you don't want to.
for i in my_list do a_thing done
This is surprisingly human readable if you approach it correctly. First, we have to understand that i and my_list are variables. The use of "i" as the variable standing for the "item" is a universal programming standard going back decades, so we just have to get used to it. You can call it anything you like, but "i" is what we all use, pretty much all of the time. You could have said something like for item in my_files or for name in list_of_names. Whatever strikes your fancy and makes it easy to follow.
The for loop itself has four special words in it itself... for, in, do, done. Those four words are part of the loop itself to make it readable. They could have been left out, in theory, but the result would be impossible to read. in, do, done are not variables nor commands, just separators for the for command. They could have just as easily been semicolons or parens.
The variable my_list is an array, or a "list of items." It doesn't have to be longer than a single item, a one unit array is perfectly valid. But it is that array that is "looped over" as we say, to process.
So there are three things that we have in the command here. The first is a variable name of our choosing, the second is an array (aka the thing that we are looping over), and the third is the action that we take based on that loop.
A loop can do something really basic like just say "Hi" for each item in an array and nothing else. Or it can print out the names of the things in an array. Or it can do complex actions on each item, or other items.
The most common use of a loop is to process the item one iteration at a time. So let's give an example...
for pet in list_of_pets do print "The name of the pet is " $pet print "It is a good animal." done
In this example we have shown many concepts. First that we can use a human readable name like pet instead of i to tell someone reading the script what we meant this variable to represent. And the array has a useful name, too. So unless we are trying to trick someone, we can tell that we are going to process "for each pet in an array of pets do..."
And then in our action area (between do and done) we see that here we have two commands. We can have zero, one, or millions. Any number. In the first print line we use the $pet variable that we have from the loop. Each time the loop processes, this variable will be updated for the specific pet in question. The second print line doesn't use a variable and does the same thing every time no matter what.
The for loop itself will run once for every item in the array and then exit. That's it. That's the basics of a for loop in BASH.
-
Learning about
foreach
in PowerShell changed my life, and the BASH example looks like it has the same functionality. I cannot remember a time where I've used just thefor
loop in PowerShell. -
@EddieJennings said in What is a For Loop:
Learning about
foreach
in PowerShell changed my life, and the BASH example looks like it has the same functionality. I cannot remember a time where I've used just thefor
loop in PowerShell.Because in PowerShell foreach and for are very different. That bash example is the PowerShell equivalent of foreach.
A for loop has an init, condition, and repeat portion in PowerShell.
-
@Obsolesce said in What is a For Loop:
@EddieJennings said in What is a For Loop:
Learning about
foreach
in PowerShell changed my life, and the BASH example looks like it has the same functionality. I cannot remember a time where I've used just thefor
loop in PowerShell.Because in PowerShell foreach and for are very different.
True, and with that I've yet to find reason for me to use
for
in PowerShell. -
@EddieJennings said in What is a For Loop:
@Obsolesce said in What is a For Loop:
@EddieJennings said in What is a For Loop:
Learning about
foreach
in PowerShell changed my life, and the BASH example looks like it has the same functionality. I cannot remember a time where I've used just thefor
loop in PowerShell.Because in PowerShell foreach and for are very different.
True, and with that I've yet to find reason for me to use
for
in PowerShell.Same in any language, looping through a set and evaluating each to a condition.
-
None of that is exclusive to PowerShell. It's like that in all languages.
-
@Obsolesce said in What is a For Loop:
None of that is exclusive to PowerShell. It's like that in all languages.
This I know.
-
@Obsolesce said in What is a For Loop:
@EddieJennings said in What is a For Loop:
Learning about
foreach
in PowerShell changed my life, and the BASH example looks like it has the same functionality. I cannot remember a time where I've used just thefor
loop in PowerShell.Because in PowerShell foreach and for are very different. That bash example is the PowerShell equivalent of foreach.
A for loop has an init, condition, and repeat portion in PowerShell.
That's correct. foreach is a new name for traditional for.
-
@scottalanmiller said in What is a For Loop:
@Obsolesce said in What is a For Loop:
@EddieJennings said in What is a For Loop:
Learning about
foreach
in PowerShell changed my life, and the BASH example looks like it has the same functionality. I cannot remember a time where I've used just thefor
loop in PowerShell.Because in PowerShell foreach and for are very different. That bash example is the PowerShell equivalent of foreach.
A for loop has an init, condition, and repeat portion in PowerShell.
That's correct. foreach is a new name for traditional for.
Other way around Scott.
A traditional for-loop loops over a series of numbers. And an iterative for-loop (usually called foreach or similar) loops over an enumeration / collection.
-
@Pete-S said in What is a For Loop:
@scottalanmiller said in What is a For Loop:
@Obsolesce said in What is a For Loop:
@EddieJennings said in What is a For Loop:
Learning about
foreach
in PowerShell changed my life, and the BASH example looks like it has the same functionality. I cannot remember a time where I've used just thefor
loop in PowerShell.Because in PowerShell foreach and for are very different. That bash example is the PowerShell equivalent of foreach.
A for loop has an init, condition, and repeat portion in PowerShell.
That's correct. foreach is a new name for traditional for.
Other way around Scott.
A traditional for-loop loops over a series of numbers. And an iterative for-loop (usually called foreach or similar) loops over an enumeration / collection.
So bash is the odd man out here by calling it just "for". However it makes some sense because it's way more likely to need a loop in bash that iterates over a collection (of filenames for instance) than X number of times.
-
@Pete-S said in What is a For Loop:
@Pete-S said in What is a For Loop:
@scottalanmiller said in What is a For Loop:
@Obsolesce said in What is a For Loop:
@EddieJennings said in What is a For Loop:
Learning about
foreach
in PowerShell changed my life, and the BASH example looks like it has the same functionality. I cannot remember a time where I've used just thefor
loop in PowerShell.Because in PowerShell foreach and for are very different. That bash example is the PowerShell equivalent of foreach.
A for loop has an init, condition, and repeat portion in PowerShell.
That's correct. foreach is a new name for traditional for.
Other way around Scott.
A traditional for-loop loops over a series of numbers. And an iterative for-loop (usually called foreach or similar) loops over an enumeration / collection.
So bash is the odd man out here by calling it just "for". However it makes some sense because it's way more likely to need a loop in bash that iterates over a collection (of filenames for instance) than X number of times.
Go is similar. There's no for each or while (Bash does have while though). Everything is a for loop.
for { fmt.Println("hey") }
Is like
while true
for i := 0; i < 10; i++ { fmt.Printf("hey %d times", I) }
for i, v := range someList { fmt.Printf("%d has a value of %s), i, v) }
-
@Pete-S said in What is a For Loop:
@Pete-S said in What is a For Loop:
@scottalanmiller said in What is a For Loop:
@Obsolesce said in What is a For Loop:
@EddieJennings said in What is a For Loop:
Learning about
foreach
in PowerShell changed my life, and the BASH example looks like it has the same functionality. I cannot remember a time where I've used just thefor
loop in PowerShell.Because in PowerShell foreach and for are very different. That bash example is the PowerShell equivalent of foreach.
A for loop has an init, condition, and repeat portion in PowerShell.
That's correct. foreach is a new name for traditional for.
Other way around Scott.
A traditional for-loop loops over a series of numbers. And an iterative for-loop (usually called foreach or similar) loops over an enumeration / collection.
So bash is the odd man out here by calling it just "for". However it makes some sense because it's way more likely to need a loop in bash that iterates over a collection (of filenames for instance) than X number of times.
Don't think so. BASH did it the standard way. foreach is the new way.
-
@scottalanmiller said in What is a For Loop:
@Pete-S said in What is a For Loop:
@Pete-S said in What is a For Loop:
@scottalanmiller said in What is a For Loop:
@Obsolesce said in What is a For Loop:
@EddieJennings said in What is a For Loop:
Learning about
foreach
in PowerShell changed my life, and the BASH example looks like it has the same functionality. I cannot remember a time where I've used just thefor
loop in PowerShell.Because in PowerShell foreach and for are very different. That bash example is the PowerShell equivalent of foreach.
A for loop has an init, condition, and repeat portion in PowerShell.
That's correct. foreach is a new name for traditional for.
Other way around Scott.
A traditional for-loop loops over a series of numbers. And an iterative for-loop (usually called foreach or similar) loops over an enumeration / collection.
So bash is the odd man out here by calling it just "for". However it makes some sense because it's way more likely to need a loop in bash that iterates over a collection (of filenames for instance) than X number of times.
Don't think so. BASH did it the standard way. foreach is the new way.
Hell no. Bash is written in C and a C for-loop is over a number sequence.
for (i=1; i<=10; i++) do_something();
So a traditional for loop is over a number sequence and has been for decades before bash and unix.
-
@Pete-S said in What is a For Loop:
@scottalanmiller said in What is a For Loop:
@Pete-S said in What is a For Loop:
@Pete-S said in What is a For Loop:
@scottalanmiller said in What is a For Loop:
@Obsolesce said in What is a For Loop:
@EddieJennings said in What is a For Loop:
Learning about
foreach
in PowerShell changed my life, and the BASH example looks like it has the same functionality. I cannot remember a time where I've used just thefor
loop in PowerShell.Because in PowerShell foreach and for are very different. That bash example is the PowerShell equivalent of foreach.
A for loop has an init, condition, and repeat portion in PowerShell.
That's correct. foreach is a new name for traditional for.
Other way around Scott.
A traditional for-loop loops over a series of numbers. And an iterative for-loop (usually called foreach or similar) loops over an enumeration / collection.
So bash is the odd man out here by calling it just "for". However it makes some sense because it's way more likely to need a loop in bash that iterates over a collection (of filenames for instance) than X number of times.
Don't think so. BASH did it the standard way. foreach is the new way.
Hell no. Bash is written in C and a C for-loop is over a number sequence.
for (i=1; i<=10; i++) do_something();
So a traditional for loop is over a number sequence and has been for decades before bash and unix.
So just to clarify.
A traditional for-loop is over a sequence of numbers.
An iterator-based for-loop is over a collection / array of items.And then each of these types can have different names depending on the language.
Earlier programming languages didn't have an iterator-based for-loop at all. For instance C, Fortran, Pascal didn't. And assembler or machine code doesn't have any kind of for-loop at all. You would count down a number and do a conditional jump to exit the loop.
-
@Pete-S said in What is a For Loop:
Earlier programming languages didn't have an iterator-based for-loop at all.
that's true, but "earlier" is extremely old. Like 1950s. By the 1970s, basically everyone had it.
Fortran does not, for example, but is ancient. C does, and is still crazy old.
-
Linguistically....
The "for" term comes from ALGOL
and the "do" term comes from Fortran.Both for the same action.
-
@scottalanmiller said in What is a For Loop:
@Pete-S said in What is a For Loop:
Earlier programming languages didn't have an iterator-based for-loop at all.
that's true, but "earlier" is extremely old. Like 1950s. By the 1970s, basically everyone had it.
Fortran does not, for example, but is ancient. C does, and is still crazy old.
No C doesn't have it. You'd need to write some functions to handle something like that.
The problem is that C doesn't have any built-in collections. Arrays exists but are only fixed length. To be able to iterate over a set of filenames (that can be as big as your RAM) you'd have your work cut out for you in C. If you impose a max number of filenames, say 255 or 65535, it's easier though.
-
@Pete-S said in What is a For Loop:
@scottalanmiller said in What is a For Loop:
@Pete-S said in What is a For Loop:
Earlier programming languages didn't have an iterator-based for-loop at all.
that's true, but "earlier" is extremely old. Like 1950s. By the 1970s, basically everyone had it.
Fortran does not, for example, but is ancient. C does, and is still crazy old.
No C doesn't have it. You'd need to write some functions to handle something like that.
The problem is that C doesn't have any built-in collections. Arrays exists but are only fixed length. To be able to iterate over a set of filenames (that can be as big as your RAM) you'd have your work cut out for you in C. If you impose a max number of filenames, say 255 or 65535, it's easier though.
True, but there is a standard pattern for it at least.
-
The foreach in PowerShell is best in most cases when you want to iterate all the values in an array, including "objects". Otherwise, for is typically better when you want to operate on a subset of them... really, when the iteration count is already known.
-
Example from PHP of a
foreach