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. -
@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
-
I did some experimenting... but it's not always the case, it depends on what you are doing. But doing this with simple counting results:
And for fun lol....