ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    What is a For Loop

    Developer Discussion
    loops bash
    6
    21
    1.9k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • EddieJenningsE
      EddieJennings @Obsolesce
      last edited by

      @Obsolesce said in What is a For Loop:

      @EddieJennings said in What is a For Loop:

      @scottalanmiller

      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 the for 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.

      ObsolesceO 1 Reply Last reply Reply Quote 0
      • ObsolesceO
        Obsolesce @EddieJennings
        last edited by

        @EddieJennings said in What is a For Loop:

        @Obsolesce said in What is a For Loop:

        @EddieJennings said in What is a For Loop:

        @scottalanmiller

        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 the for 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.

        1 Reply Last reply Reply Quote 0
        • ObsolesceO
          Obsolesce
          last edited by

          None of that is exclusive to PowerShell. It's like that in all languages.

          EddieJenningsE 1 Reply Last reply Reply Quote 0
          • EddieJenningsE
            EddieJennings @Obsolesce
            last edited by

            @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.

            1 Reply Last reply Reply Quote 0
            • scottalanmillerS
              scottalanmiller @Obsolesce
              last edited by

              @Obsolesce said in What is a For Loop:

              @EddieJennings said in What is a For Loop:

              @scottalanmiller

              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 the for 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.

              1 1 Reply Last reply Reply Quote 0
              • 1
                1337 @scottalanmiller
                last edited by

                @scottalanmiller said in What is a For Loop:

                @Obsolesce said in What is a For Loop:

                @EddieJennings said in What is a For Loop:

                @scottalanmiller

                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 the for 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.

                1 1 Reply Last reply Reply Quote 0
                • 1
                  1337 @1337
                  last edited by

                  @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:

                  @scottalanmiller

                  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 the for 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.

                  stacksofplatesS scottalanmillerS 2 Replies Last reply Reply Quote 0
                  • stacksofplatesS
                    stacksofplates @1337
                    last edited by stacksofplates

                    @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:

                    @scottalanmiller

                    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 the for 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)
                    }
                    
                    1 Reply Last reply Reply Quote 0
                    • scottalanmillerS
                      scottalanmiller @1337
                      last edited by

                      @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:

                      @scottalanmiller

                      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 the for 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.

                      1 1 Reply Last reply Reply Quote 0
                      • 1
                        1337 @scottalanmiller
                        last edited by 1337

                        @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:

                        @scottalanmiller

                        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 the for 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.

                        1 1 Reply Last reply Reply Quote 0
                        • 1
                          1337 @1337
                          last edited by 1337

                          @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:

                          @scottalanmiller

                          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 the for 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.

                          scottalanmillerS 1 Reply Last reply Reply Quote 0
                          • scottalanmillerS
                            scottalanmiller @1337
                            last edited by

                            @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.

                            1 1 Reply Last reply Reply Quote 0
                            • scottalanmillerS
                              scottalanmiller
                              last edited by

                              Linguistically....

                              The "for" term comes from ALGOL
                              and the "do" term comes from Fortran.

                              Both for the same action.

                              1 Reply Last reply Reply Quote 0
                              • 1
                                1337 @scottalanmiller
                                last edited by 1337

                                @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.

                                scottalanmillerS 1 Reply Last reply Reply Quote 0
                                • scottalanmillerS
                                  scottalanmiller @1337
                                  last edited by

                                  @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.

                                  https://en.wikipedia.org/wiki/Foreach_loop#C

                                  1 Reply Last reply Reply Quote 0
                                  • ObsolesceO
                                    Obsolesce
                                    last edited by

                                    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.

                                    1 Reply Last reply Reply Quote 0
                                    • JaredBuschJ
                                      JaredBusch
                                      last edited by

                                      Example from PHP of a foreach

                                      5ac39b80-94b9-4e80-bb66-dcd8caa2eb1b-image.png

                                      1 Reply Last reply Reply Quote 1
                                      • ObsolesceO
                                        Obsolesce
                                        last edited by

                                        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:

                                        b3b9a87e-cb8d-4e6f-8edb-dd29ffc719b2-image.png

                                        And for fun lol....

                                        eb24b287-1c66-4c93-8949-85d9f083eab7-image.png

                                        1 Reply Last reply Reply Quote 3
                                        • 1
                                        • 2
                                        • 1 / 2
                                        • First post
                                          Last post