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

for loop

Scheduled Pinned Locked Moved The Lounge
question
14 Posts 10 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.
  • S Smart Not Clever

    How this happend to you ever? In the Programing world you use break to break out of a loop but in the real world you need a break to really continue.

    M Offline
    M Offline
    Mark_Wallace
    wrote on last edited by
    #5

    Smart-Not-Clever wrote:

    n the real world you need a break to really continue.

    What's she done?

    I wanna be a eunuchs developer! Pass me a bread knife!

    1 Reply Last reply
    0
    • B Brady Kelly

      I normally take a very hard look at a for loop that needs a break, wondering who and why TF wrote such a thing.

      M Offline
      M Offline
      Mark_Wallace
      wrote on last edited by
      #6

      Think of it as a switch with an iterable. It has to break the iteration when it's finished doing what it has to do, or a millisecond or two of time will be lost forever.

      I wanna be a eunuchs developer! Pass me a bread knife!

      1 Reply Last reply
      0
      • B Brady Kelly

        I normally take a very hard look at a for loop that needs a break, wondering who and why TF wrote such a thing.

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #7

        I suppose that is in the same set of 'never use goto' and 'no multiple return'. That's a rather dogmatic approach that may work possibly with newbies, in my humble opinion. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        T 1 Reply Last reply
        0
        • S Smart Not Clever

          How this happend to you ever? In the Programing world you use break to break out of a loop but in the real world you need a break to really continue.

          A Offline
          A Offline
          Abhinav S
          wrote on last edited by
          #8

          Loopy. :)

          Me, I'm dishonest. And a dishonest man you can always trust to be dishonest.
          Honestly. It's the honest ones you want to watch out for...

          1 Reply Last reply
          0
          • B Brady Kelly

            I normally take a very hard look at a for loop that needs a break, wondering who and why TF wrote such a thing.

            H Offline
            H Offline
            Harvey Saayman
            wrote on last edited by
            #9

            With linq to sql I do this with foreach loops for performance reasons... Allow me to elaborate with two examples: Option A:

            var queryResult = (from i in ctx.SomeTable
            where i.ID == 42
            select i);

            //
            // Only continue if queryResult has an item
            if (queryResult.Count() > 0)
            {
            //
            // Do some work
            }

            Option B:

            var queryResult = (from i in ctx.SomeTable
            where i.ID == 42
            select i);

            //
            // Only continue if queryResult has an item
            bool ItemExsists = false;
            foreach (var i in queryResult)
            {
            ItemExsists = true;
            break;
            }

            if (ItemExsists)
            {
            //
            // Do Some Work
            }

            queryResult.Count() is dead frign slow when all your interested in knowing is if there is at least one result in the collection. Option B isn't very pretty, but it works better than option A speed wise.

            Harvey Saayman - South Africa Software Developer .Net, C#, SQL you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming) 1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

            B 1 Reply Last reply
            0
            • H Harvey Saayman

              With linq to sql I do this with foreach loops for performance reasons... Allow me to elaborate with two examples: Option A:

              var queryResult = (from i in ctx.SomeTable
              where i.ID == 42
              select i);

              //
              // Only continue if queryResult has an item
              if (queryResult.Count() > 0)
              {
              //
              // Do some work
              }

              Option B:

              var queryResult = (from i in ctx.SomeTable
              where i.ID == 42
              select i);

              //
              // Only continue if queryResult has an item
              bool ItemExsists = false;
              foreach (var i in queryResult)
              {
              ItemExsists = true;
              break;
              }

              if (ItemExsists)
              {
              //
              // Do Some Work
              }

              queryResult.Count() is dead frign slow when all your interested in knowing is if there is at least one result in the collection. Option B isn't very pretty, but it works better than option A speed wise.

              Harvey Saayman - South Africa Software Developer .Net, C#, SQL you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming) 1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

              B Offline
              B Offline
              Brady Kelly
              wrote on last edited by
              #10

              A foreach loop is more like a while loop than a for loop, so you have more slack to use break. Then, what about Option C?

              if (queryResult.Any())
              {
              //do some work.
              }

              1 Reply Last reply
              0
              • B Brady Kelly

                I normally take a very hard look at a for loop that needs a break, wondering who and why TF wrote such a thing.

                E Offline
                E Offline
                Ed Poore
                wrote on last edited by
                #11

                I often use break when there's one exception to the rule which is the loop condition. Including that extra exception into the loop declaration just makes it unclearer in my mind.

                B 1 Reply Last reply
                0
                • C CPallini

                  I suppose that is in the same set of 'never use goto' and 'no multiple return'. That's a rather dogmatic approach that may work possibly with newbies, in my humble opinion. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  T Offline
                  T Offline
                  Todd Smith
                  wrote on last edited by
                  #12

                  I like to add a goto inside my empty exceptions which then jump to a switch statement with multiple returns.

                  Todd Smith

                  C 1 Reply Last reply
                  0
                  • E Ed Poore

                    I often use break when there's one exception to the rule which is the loop condition. Including that extra exception into the loop declaration just makes it unclearer in my mind.

                    B Offline
                    B Offline
                    Brady Kelly
                    wrote on last edited by
                    #13

                    I always make exceptions as well. Exceptions rule.

                    1 Reply Last reply
                    0
                    • T Todd Smith

                      I like to add a goto inside my empty exceptions which then jump to a switch statement with multiple returns.

                      Todd Smith

                      C Offline
                      C Offline
                      CPallini
                      wrote on last edited by
                      #14

                      So you are not a newbie. :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      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