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. Other Discussions
  3. The Weird and The Wonderful
  4. Clever For-loop

Clever For-loop

Scheduled Pinned Locked Moved The Weird and The Wonderful
10 Posts 8 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.
  • O Offline
    O Offline
    Oyvind Bratland
    wrote on last edited by
    #1

    I used to work with a guy who was clever enough to write this:

    For i = 1 To 5
    If i = 1 Then
    ...
    Else If i = 2 Then
    ...
    Else If i = 3 Then
    ...
    Else If i = 4 Then
    ...
    Else If i = 5 Then
    ...
    End If
    Next i

    I guess the coder wanted to do something "in five steps". He certainly does. We don't work together anymore ;)

    J L S J A 6 Replies Last reply
    0
    • O Oyvind Bratland

      I used to work with a guy who was clever enough to write this:

      For i = 1 To 5
      If i = 1 Then
      ...
      Else If i = 2 Then
      ...
      Else If i = 3 Then
      ...
      Else If i = 4 Then
      ...
      Else If i = 5 Then
      ...
      End If
      Next i

      I guess the coder wanted to do something "in five steps". He certainly does. We don't work together anymore ;)

      J Offline
      J Offline
      James Simpson
      wrote on last edited by
      #2

      Maybe there is a i++ in some states in the if blocks to skip steps?

      James Simpson Web Developer imebgo@hotmail.com P S - This is what part of the alphabet would look like if Q and R were eliminated
      Mitch Hedberg

      O 1 Reply Last reply
      0
      • J James Simpson

        Maybe there is a i++ in some states in the if blocks to skip steps?

        James Simpson Web Developer imebgo@hotmail.com P S - This is what part of the alphabet would look like if Q and R were eliminated
        Mitch Hedberg

        O Offline
        O Offline
        Oyvind Bratland
        wrote on last edited by
        #3

        Nope, the i did nothing else ;)

        1 Reply Last reply
        0
        • O Oyvind Bratland

          I used to work with a guy who was clever enough to write this:

          For i = 1 To 5
          If i = 1 Then
          ...
          Else If i = 2 Then
          ...
          Else If i = 3 Then
          ...
          Else If i = 4 Then
          ...
          Else If i = 5 Then
          ...
          End If
          Next i

          I guess the coder wanted to do something "in five steps". He certainly does. We don't work together anymore ;)

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          How else would he know what step he was at? Now he can simply refer to i in the debugger! ;P

          xacc.ide - now with TabsToSpaces support
          IronScheme - 1.0 alpha 4a out now (29 May 2008)

          1 Reply Last reply
          0
          • O Oyvind Bratland

            I used to work with a guy who was clever enough to write this:

            For i = 1 To 5
            If i = 1 Then
            ...
            Else If i = 2 Then
            ...
            Else If i = 3 Then
            ...
            Else If i = 4 Then
            ...
            Else If i = 5 Then
            ...
            End If
            Next i

            I guess the coder wanted to do something "in five steps". He certainly does. We don't work together anymore ;)

            S Offline
            S Offline
            supercat9
            wrote on last edited by
            #5

            While a select-case statement would probably be better than the cascaded if/else-if, the illustrated construct may not be unreasonable in certain cases where it is expected that the inside of the loop will be pulled out into a separate routine for use as a state machine. Having the code within a for-next loop may be handy during development while the code is being perfected, even if the final construct will be something else. To be sure, when running on a PC it's often easier to use threads rather than state machines, but state machines can often have a much lower 'footprint'. If an application will require hundreds or thousands of objects each with a multi-way state machine, and all have to be operable concurrently, a select-case state machine may be a reasonable approach. Delegates should be considered as well, since they have some advantages (but also some disadvantages), but state machines are reasonable in simple cases.

            T 1 Reply Last reply
            0
            • O Oyvind Bratland

              I used to work with a guy who was clever enough to write this:

              For i = 1 To 5
              If i = 1 Then
              ...
              Else If i = 2 Then
              ...
              Else If i = 3 Then
              ...
              Else If i = 4 Then
              ...
              Else If i = 5 Then
              ...
              End If
              Next i

              I guess the coder wanted to do something "in five steps". He certainly does. We don't work together anymore ;)

              J Offline
              J Offline
              John R Shaw
              wrote on last edited by
              #6

              Take brain – Turn off brain – Start coding. :doh:

              yvind Bratland wrote:

              We don't work together anymore

              Surprise! Surprise! :laugh:

              INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

              1 Reply Last reply
              0
              • S supercat9

                While a select-case statement would probably be better than the cascaded if/else-if, the illustrated construct may not be unreasonable in certain cases where it is expected that the inside of the loop will be pulled out into a separate routine for use as a state machine. Having the code within a for-next loop may be handy during development while the code is being perfected, even if the final construct will be something else. To be sure, when running on a PC it's often easier to use threads rather than state machines, but state machines can often have a much lower 'footprint'. If an application will require hundreds or thousands of objects each with a multi-way state machine, and all have to be operable concurrently, a select-case state machine may be a reasonable approach. Delegates should be considered as well, since they have some advantages (but also some disadvantages), but state machines are reasonable in simple cases.

                T Offline
                T Offline
                Tristan Rhodes
                wrote on last edited by
                #7

                Wow... Just wow... Where in the original WTF did it mention a problem that would require State Machines or Threads to solve? You've created a mountain out of a grain of dirt.

                ------------------------------- Carrier Bags - 21st Century Tumbleweed.

                S 1 Reply Last reply
                0
                • T Tristan Rhodes

                  Wow... Just wow... Where in the original WTF did it mention a problem that would require State Machines or Threads to solve? You've created a mountain out of a grain of dirt.

                  ------------------------------- Carrier Bags - 21st Century Tumbleweed.

                  S Offline
                  S Offline
                  supercat9
                  wrote on last edited by
                  #8

                  Tristan Rhodes wrote:

                  Where in the original WTF did it mention a problem that would require State Machines or Threads to solve?

                  My point was that there are circumstances where code like that illustrated may be useful. While it is true that there was nothing to indicate that such circumstances applied here, I would not wish to condemn code without knowing that such circumstances did not apply. BTW, another circumstance where such code may be useful is in cases where the loop will evolve into something like:

                  for i=1 to 5
                  .. do some stuff in every case
                  if i=1 then
                  .. handle first case
                  else if i=2 then
                  .. handle second case
                  ... etc
                  end if
                  .. do some more stuff in every case

                  In many circumstances, that style of code could be handled better by putting the different parts into subroutines and just calling them:

                  everycase_prep()
                  .. handle first case
                  everycase_cleanup()
                  everycase_prep()
                  .. handle second case
                  everycase_cleanup()

                  In some cases, however, the prep and cleanup would need to share many local variables, thus requiring either long parameter lists or awkward class constructs.

                  1 Reply Last reply
                  0
                  • O Oyvind Bratland

                    I used to work with a guy who was clever enough to write this:

                    For i = 1 To 5
                    If i = 1 Then
                    ...
                    Else If i = 2 Then
                    ...
                    Else If i = 3 Then
                    ...
                    Else If i = 4 Then
                    ...
                    Else If i = 5 Then
                    ...
                    End If
                    Next i

                    I guess the coder wanted to do something "in five steps". He certainly does. We don't work together anymore ;)

                    A Offline
                    A Offline
                    Ariel Kazeed
                    wrote on last edited by
                    #9

                    Of course you two don't work together anymore. This guy's a visionaire! You simply cannot understand the superior maths under this construction. :-D

                    Kazz


                    "Users are there to click on things, not think. Let the archs do the damn thinking."

                    1 Reply Last reply
                    0
                    • O Oyvind Bratland

                      I used to work with a guy who was clever enough to write this:

                      For i = 1 To 5
                      If i = 1 Then
                      ...
                      Else If i = 2 Then
                      ...
                      Else If i = 3 Then
                      ...
                      Else If i = 4 Then
                      ...
                      Else If i = 5 Then
                      ...
                      End If
                      Next i

                      I guess the coder wanted to do something "in five steps". He certainly does. We don't work together anymore ;)

                      P Offline
                      P Offline
                      Paul Conrad
                      wrote on last edited by
                      #10

                      yvind Bratland wrote:

                      We don't work together anymore

                      I bet that's a good thing :-D

                      "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                      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