Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Nested loops

Nested loops

Scheduled Pinned Locked Moved The Lounge
questionlounge
74 Posts 34 Posters 0 Views 1 Watching
  • 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.
  • L Lost User

    Follow up on the Variable names thread below, I would like to ask what is the level of nested loops that is acceptable in general? Most people would agree that three levels is acceptable, but I would say stop at two. The third loops gets a bit messy.

    for(int i=0; i<100; i++) { //Okay
    for(int j=0; j<100; j++) { //Acceptable
    for(int k=0; k<100; k++) { //Messy
    }
    }
    }

    N Offline
    N Offline
    Nemanja Trifunovic
    wrote on last edited by
    #21

    Shameel wrote:

    I would like to ask what is the level of nested loops that is acceptable in general?

    Zero! What's Wrong with the For Loop[^]

    utf8-cpp

    V 1 Reply Last reply
    0
    • L Lost User

      Follow up on the Variable names thread below, I would like to ask what is the level of nested loops that is acceptable in general? Most people would agree that three levels is acceptable, but I would say stop at two. The third loops gets a bit messy.

      for(int i=0; i<100; i++) { //Okay
      for(int j=0; j<100; j++) { //Acceptable
      for(int k=0; k<100; k++) { //Messy
      }
      }
      }

      D Offline
      D Offline
      Dylan Morley
      wrote on last edited by
      #22

      I never go further than 26 levels, because then I've run out of variables to control the loops with. I could of course start using aa, ab, ac...but that gets confusing.

      T V 2 Replies Last reply
      0
      • L Lost User

        Follow up on the Variable names thread below, I would like to ask what is the level of nested loops that is acceptable in general? Most people would agree that three levels is acceptable, but I would say stop at two. The third loops gets a bit messy.

        for(int i=0; i<100; i++) { //Okay
        for(int j=0; j<100; j++) { //Acceptable
        for(int k=0; k<100; k++) { //Messy
        }
        }
        }

        G Offline
        G Offline
        GuyThiebaut
        wrote on last edited by
        #23

        Add, as comments to the end of the loops, the containing logic and you can have as many nested as required...

        for(int i=0; i<100; i++) {

        for(int j=0; j<100; j++) {
        
            for(int k=0; k<100; k++) { 
        
            }//int k=0; k<100 
        
        }//int j=0; j<100
        

        }//int i=0; i<100

        “That which can be asserted without evidence, can be dismissed without evidence.”

        ― Christopher Hitchens

        1 Reply Last reply
        0
        • D Dylan Morley

          I never go further than 26 levels, because then I've run out of variables to control the loops with. I could of course start using aa, ab, ac...but that gets confusing.

          T Offline
          T Offline
          TheGreatAndPowerfulOz
          wrote on last edited by
          #24

          I like to use Z1, Z2, Z3, etc... Then I can have hundreds and thousands of nested loops

          If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
          You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun

          D OriginalGriffO 2 Replies Last reply
          0
          • L Lost User

            Follow up on the Variable names thread below, I would like to ask what is the level of nested loops that is acceptable in general? Most people would agree that three levels is acceptable, but I would say stop at two. The third loops gets a bit messy.

            for(int i=0; i<100; i++) { //Okay
            for(int j=0; j<100; j++) { //Acceptable
            for(int k=0; k<100; k++) { //Messy
            }
            }
            }

            G Offline
            G Offline
            Gary Wheeler
            wrote on last edited by
            #25

            As many as required, but no more. Limiting the number of nested loops is like prescribing a length for variable names: "Variable names must be at least six characters and no more than 31 characters in length, must begin with an upper case alphabetic character, may not include an underscore, and must consist of one or more complete English words, signified through use of upper case characters at the beginning of each word". Picking names will be like playing Scrabble...

            Software Zen: delete this;

            J 1 Reply Last reply
            0
            • T TheGreatAndPowerfulOz

              I like to use Z1, Z2, Z3, etc... Then I can have hundreds and thousands of nested loops

              If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
              You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun

              D Offline
              D Offline
              Dylan Morley
              wrote on last edited by
              #26

              Problem with that is the indentation gets too large and you keep having to scroll to the right.

              OriginalGriffO 1 Reply Last reply
              0
              • T TheGreatAndPowerfulOz

                I like to use Z1, Z2, Z3, etc... Then I can have hundreds and thousands of nested loops

                If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
                You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #27

                Mmmmmm....Hundreds and Thousands[^]

                Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                T 1 Reply Last reply
                0
                • D Dylan Morley

                  Problem with that is the indentation gets too large and you keep having to scroll to the right.

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #28

                  Buy a wider monitor.

                  Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Mmmmmm....Hundreds and Thousands[^]

                    Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                    T Offline
                    T Offline
                    TheGreatAndPowerfulOz
                    wrote on last edited by
                    #29

                    mmmmmmmmmmmm, doughnut :-D

                    If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
                    You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun

                    1 Reply Last reply
                    0
                    • L Lost User

                      Follow up on the Variable names thread below, I would like to ask what is the level of nested loops that is acceptable in general? Most people would agree that three levels is acceptable, but I would say stop at two. The third loops gets a bit messy.

                      for(int i=0; i<100; i++) { //Okay
                      for(int j=0; j<100; j++) { //Acceptable
                      for(int k=0; k<100; k++) { //Messy
                      }
                      }
                      }

                      E Offline
                      E Offline
                      Ennis Ray Lynch Jr
                      wrote on last edited by
                      #30

                      Is this from a wikipedia study:

                      Shameel wrote:

                      Most people would agree that three levels is acceptable

                      Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                      1 Reply Last reply
                      0
                      • N Nagy Vilmos

                        What if you want to iterate over a 3D space plotting each x,y,z in a different colour?


                        Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                        T Offline
                        T Offline
                        Tom Clement
                        wrote on last edited by
                        #31

                        Ok, and what if you want to iterate over points in Hilbert space[^]? Eh?

                        Tom Clement Serena Software, Inc. www.serena.com articles[^]

                        P T T 3 Replies Last reply
                        0
                        • T Tom Clement

                          Ok, and what if you want to iterate over points in Hilbert space[^]? Eh?

                          Tom Clement Serena Software, Inc. www.serena.com articles[^]

                          P Offline
                          P Offline
                          Pete OHanlon
                          wrote on last edited by
                          #32

                          You would get HWally to intercept the HPointy Aired HBoss first.

                          *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                          My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                          1 Reply Last reply
                          0
                          • T Tom Clement

                            Ok, and what if you want to iterate over points in Hilbert space[^]? Eh?

                            Tom Clement Serena Software, Inc. www.serena.com articles[^]

                            T Offline
                            T Offline
                            Tom Clement
                            wrote on last edited by
                            #33

                            I always knew that Hilbert space was infinite dimensional vector space, but I assumed it was a countable infinity. When I glanced at the Wikipedia article, I was kinda surprised to see that you can have a Hilbert space with an uncountable number of dimensions. Now *THAT* would be difficult to iterate across :)

                            Tom Clement Serena Software, Inc. www.serena.com articles[^]

                            1 Reply Last reply
                            0
                            • T Tom Clement

                              Ok, and what if you want to iterate over points in Hilbert space[^]? Eh?

                              Tom Clement Serena Software, Inc. www.serena.com articles[^]

                              T Offline
                              T Offline
                              TheGreatAndPowerfulOz
                              wrote on last edited by
                              #34

                              Is that anything like Dilbert space?

                              If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
                              You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun

                              T 1 Reply Last reply
                              0
                              • T TheGreatAndPowerfulOz

                                Is that anything like Dilbert space?

                                If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
                                You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun

                                T Offline
                                T Offline
                                Tom Clement
                                wrote on last edited by
                                #35

                                I think that's what Pete was getting at :)

                                Tom Clement Serena Software, Inc. www.serena.com articles[^]

                                1 Reply Last reply
                                0
                                • L Lost User

                                  Follow up on the Variable names thread below, I would like to ask what is the level of nested loops that is acceptable in general? Most people would agree that three levels is acceptable, but I would say stop at two. The third loops gets a bit messy.

                                  for(int i=0; i<100; i++) { //Okay
                                  for(int j=0; j<100; j++) { //Acceptable
                                  for(int k=0; k<100; k++) { //Messy
                                  }
                                  }
                                  }

                                  V Offline
                                  V Offline
                                  Vivi Chellappa
                                  wrote on last edited by
                                  #36

                                  How about looping through a n-dimensional table where n>3?

                                  1 Reply Last reply
                                  0
                                  • R R Giskard Reventlov

                                    a) good point. b) never had to do anything like that so don't really care. c) Two: anything else is lunacy and must be stamped out: 2 dimensions is more than enough for anybody!

                                    "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

                                    V Offline
                                    V Offline
                                    Vivi Chellappa
                                    wrote on last edited by
                                    #37

                                    OK. We shall have a road roller run over you to flatten you into two dimensions. :laugh:

                                    1 Reply Last reply
                                    0
                                    • N Nemanja Trifunovic

                                      Shameel wrote:

                                      I would like to ask what is the level of nested loops that is acceptable in general?

                                      Zero! What's Wrong with the For Loop[^]

                                      utf8-cpp

                                      V Offline
                                      V Offline
                                      Vivi Chellappa
                                      wrote on last edited by
                                      #38

                                      From that article: Here's a common example: double sum = 0;for (int i = 0; i < array.length; i++) { sum += array[i];} What's going on here? I've been programming for years, and I'm comfortable speed-reading this idiom; it's obviously a summation of a set of values in an array. But to actually read this block of code, I need to process about 30 tokens spread out over four lines. And here are programmers writing pragma, karma and dogma at the beginning of their programs and they have the gall to complain about a for loop? :wtf:

                                      1 Reply Last reply
                                      0
                                      • D Dylan Morley

                                        I never go further than 26 levels, because then I've run out of variables to control the loops with. I could of course start using aa, ab, ac...but that gets confusing.

                                        V Offline
                                        V Offline
                                        Vivi Chellappa
                                        wrote on last edited by
                                        #39

                                        Wrong of you to use a thru z as control variables. Use a1, a2, ......... and it will not be confusing at all! :laugh:

                                        1 Reply Last reply
                                        0
                                        • L Lost User

                                          ahmed zahmed wrote:

                                          That's just silly.

                                          No, it's not.

                                          ahmed zahmed wrote:

                                          It may be possible to re-factor and re-engineer the algorithm to make fewer loops

                                          That's the whole point of making this post. :-) I've seen loops neck deep where the reviewer would find commiting suicide easier than coming out of it.

                                          S Offline
                                          S Offline
                                          Stefan_Lang
                                          wrote on last edited by
                                          #40

                                          If there are nested loops, most of the time they are necessary. The only way to refactor that would be to extract the inner loop(s) into a new method, but that would not remove the loops. If a loop could be refactored into something not requiring the equivalent of a loop (such as recursion), then you should 'refactor your programmer', not your code! ;) So I agree with ahmed zahmed, that in general the notion you expressed is silly. It's quite different if you got nested branching (if or switch statements). There are many ways to rewrite an algorithm in a way that avoids nested branching, and too much nesting often indicates a bad structure that does require refactoring. Of course there are always exception, but in general nested loops cannot be sensibly refactored, whereas nested branching can and often should be.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups