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. How to sequence a loop

How to sequence a loop

Scheduled Pinned Locked Moved The Weird and The Wonderful
tutorialquestion
10 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.
  • T Offline
    T Offline
    TheNexpert
    wrote on last edited by
    #1

    for( int i = 1 ; i <= 3 ; i++) { if(i==1){…} else if(i==2){…} else if(i==3){…} else{…} } source: http://www.coding-horror.de/?p=258 :laugh:

    R J D P L 5 Replies Last reply
    0
    • T TheNexpert

      for( int i = 1 ; i <= 3 ; i++) { if(i==1){…} else if(i==2){…} else if(i==3){…} else{…} } source: http://www.coding-horror.de/?p=258 :laugh:

      R Offline
      R Offline
      riced
      wrote on last edited by
      #2

      Unfortunately I've seen this sort of thing far too often. It's up there with if((x == 1) && (x != 2)) and similar stuff. :)

      Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.

      1 Reply Last reply
      0
      • T TheNexpert

        for( int i = 1 ; i <= 3 ; i++) { if(i==1){…} else if(i==2){…} else if(i==3){…} else{…} } source: http://www.coding-horror.de/?p=258 :laugh:

        J Offline
        J Offline
        Jeroen De Dauw
        wrote on last edited by
        #3

        Some people really don't get when they should use switch/select statements :laugh:

        Jeroen De Dauw
        Forums ; Blog ; Wiki

        O 1 Reply Last reply
        0
        • T TheNexpert

          for( int i = 1 ; i <= 3 ; i++) { if(i==1){…} else if(i==2){…} else if(i==3){…} else{…} } source: http://www.coding-horror.de/?p=258 :laugh:

          D Offline
          D Offline
          David Skelly
          wrote on last edited by
          #4

          The fact that the final else block will never be executed suggests either (a) this code used to do something different and has been hacked around blindly, or (b) the person who wrote it didn't really understand what he was doing.

          OriginalGriffO 1 Reply Last reply
          0
          • D David Skelly

            The fact that the final else block will never be executed suggests either (a) this code used to do something different and has been hacked around blindly, or (b) the person who wrote it didn't really understand what he was doing.

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

            or (c) he really didn't trust integers. To be sure, to be sure, to be sure...

            You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

            "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

            S 1 Reply Last reply
            0
            • T TheNexpert

              for( int i = 1 ; i <= 3 ; i++) { if(i==1){…} else if(i==2){…} else if(i==3){…} else{…} } source: http://www.coding-horror.de/?p=258 :laugh:

              P Offline
              P Offline
              Paulo Zemek
              wrote on last edited by
              #6

              I already saw a teacher showing "how" to use switches. The combo-box had 2 values: Gif. Jpg. Then, in the code, he did something like:

              for(int i=0; i<2; i++)
              {
              if (i == combo.SelectedIndex)
              {
              switch(i)
              {
              case 1:
              // code for gif
              break;

                case 2:
                  // code for jpg.
                  break;
              }
              

              }
              }

              Note that not only the "for" is useless, the "switch" using the values of a for is ridiculous... but the valid values are 0 and 1, and the code checked for 1 and 2. And finally, but not presented in the code, the code was copied and the filename was something + ".gif" or something + ".jpg"... he could simple do a better working code with:

              string name = editName.Text + '.' + combo.SelectedValue;

              C 1 Reply Last reply
              0
              • P Paulo Zemek

                I already saw a teacher showing "how" to use switches. The combo-box had 2 values: Gif. Jpg. Then, in the code, he did something like:

                for(int i=0; i<2; i++)
                {
                if (i == combo.SelectedIndex)
                {
                switch(i)
                {
                case 1:
                // code for gif
                break;

                  case 2:
                    // code for jpg.
                    break;
                }
                

                }
                }

                Note that not only the "for" is useless, the "switch" using the values of a for is ridiculous... but the valid values are 0 and 1, and the code checked for 1 and 2. And finally, but not presented in the code, the code was copied and the filename was something + ".gif" or something + ".jpg"... he could simple do a better working code with:

                string name = editName.Text + '.' + combo.SelectedValue;

                C Offline
                C Offline
                Christopher Ayroso
                wrote on last edited by
                #7

                or maybe he is just impressing his students how genius he is :)

                1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  or (c) he really didn't trust integers. To be sure, to be sure, to be sure...

                  You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

                  S Offline
                  S Offline
                  Sebastian Br
                  wrote on last edited by
                  #8

                  ...but in real-life I've seen "obviously unnecessary" else-blocks quite often. Most times there was an error-handling routine, which put something like "unhandled case; please look at routine foo()" to your log-file. And in some cases it make sense - who guarantees you that a guy who extends this routine, thought on all possible occurances of "i"?

                  1 Reply Last reply
                  0
                  • J Jeroen De Dauw

                    Some people really don't get when they should use switch/select statements :laugh:

                    Jeroen De Dauw
                    Forums ; Blog ; Wiki

                    O Offline
                    O Offline
                    Omar Gameel Salem
                    wrote on last edited by
                    #9

                    ...or shouldnt

                    1 Reply Last reply
                    0
                    • T TheNexpert

                      for( int i = 1 ; i <= 3 ; i++) { if(i==1){…} else if(i==2){…} else if(i==3){…} else{…} } source: http://www.coding-horror.de/?p=258 :laugh:

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Someone who has been using Python for a while?

                      Join the cool kids - Come fold with us[^]

                      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