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

Useless for

Scheduled Pinned Locked Moved The Weird and The Wonderful
15 Posts 13 Posters 1 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
    Oscar Luis Vera Perez
    wrote on last edited by
    #1

    Hi, I saw this some time ago:

    for(int i = 0; i < 2; i++)
    {
    if(i==0)
    //Do something
    if(i==1)
    //Do something else
    }

    this is pretty much a horror

    B P S G C 6 Replies Last reply
    0
    • O Oscar Luis Vera Perez

      Hi, I saw this some time ago:

      for(int i = 0; i < 2; i++)
      {
      if(i==0)
      //Do something
      if(i==1)
      //Do something else
      }

      this is pretty much a horror

      B Offline
      B Offline
      BadKarma
      wrote on last edited by
      #2

      Your are correct, he/she should have used a switch instead of the if statements. Like this:

      for(int i= 0; i < 2; i++)
      {
        switch(i)
        {
          case 0:
            // do something
            break;
          case 1:
            // do something else
            break;
        };
      }
      

      Learn from the mistakes of others, you may not live long enough to make them all yourself.

      _ D 2 Replies Last reply
      0
      • O Oscar Luis Vera Perez

        Hi, I saw this some time ago:

        for(int i = 0; i < 2; i++)
        {
        if(i==0)
        //Do something
        if(i==1)
        //Do something else
        }

        this is pretty much a horror

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Probably, but do //Do something and //Do something else alter the value of i?

        E 1 Reply Last reply
        0
        • B BadKarma

          Your are correct, he/she should have used a switch instead of the if statements. Like this:

          for(int i= 0; i < 2; i++)
          {
            switch(i)
            {
              case 0:
                // do something
                break;
              case 1:
                // do something else
                break;
            };
          }
          

          Learn from the mistakes of others, you may not live long enough to make them all yourself.

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          I guess there is no need for a loop at all. Nothing is being repeated here.

          «_Superman_» I love work. It gives me something to do between weekends.

          P 1 Reply Last reply
          0
          • _ _Superman_

            I guess there is no need for a loop at all. Nothing is being repeated here.

            «_Superman_» I love work. It gives me something to do between weekends.

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            «_Superman_» wrote:

            Nothing is being repeated here.

            We don't know that.

            1 Reply Last reply
            0
            • O Oscar Luis Vera Perez

              Hi, I saw this some time ago:

              for(int i = 0; i < 2; i++)
              {
              if(i==0)
              //Do something
              if(i==1)
              //Do something else
              }

              this is pretty much a horror

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

              Such code may be entirely reasonable and practical if there is common code before or after the case-specific code, and if variable scoping would make it impractical for the common code to be pulled out into a separate routine. My guess would be that there had at some point been some code which was executed every time through the loop, but that such code has been eliminated leaving the now-useless loop structure behind.

              1 Reply Last reply
              0
              • O Oscar Luis Vera Perez

                Hi, I saw this some time ago:

                for(int i = 0; i < 2; i++)
                {
                if(i==0)
                //Do something
                if(i==1)
                //Do something else
                }

                this is pretty much a horror

                G Offline
                G Offline
                gspyacc
                wrote on last edited by
                #7

                This is for sure a Coding Horror, a good one. Does a loop for only two predefined values. If i (counter) is changed inside for, this is another coding horror!. And guess what, the developer is interesting for the values 0 and 1. (reminds you something like true/false maybe?)

                J 1 Reply Last reply
                0
                • G gspyacc

                  This is for sure a Coding Horror, a good one. Does a loop for only two predefined values. If i (counter) is changed inside for, this is another coding horror!. And guess what, the developer is interesting for the values 0 and 1. (reminds you something like true/false maybe?)

                  J Offline
                  J Offline
                  JasonPSage
                  wrote on last edited by
                  #8

                  I agree with the poster who said it depends. If this is the loop's entire construct - yes I think it's not ideal. (Not a horror though). I would just have two statements "do something" and "do something else" and MAYBE block them in braces to be clear its a "unit" or make a function if warranted. If the person planned to expand on this loop - where it cycled thru ten iterations, but was suppose to do something special for iteration 1 and 2, then a case statement might be ideal.. still not a horror. --Jason P Sage

                  Know way to many languages... master of none!

                  O 1 Reply Last reply
                  0
                  • O Oscar Luis Vera Perez

                    Hi, I saw this some time ago:

                    for(int i = 0; i < 2; i++)
                    {
                    if(i==0)
                    //Do something
                    if(i==1)
                    //Do something else
                    }

                    this is pretty much a horror

                    C Offline
                    C Offline
                    che3358
                    wrote on last edited by
                    #9

                    Do something absolutely is critical here to know whether it is horrible or not.

                    The following sample indicates what you saw is not horrible:

                    See, I have two controls: Label0 and TextBox1. I can assign values to them:

                    for (int i = 0; i < 2; i++)
                    {
                    if (i == 0)
                    {
                    Label lable = (Label)this.FindControl("Label" + i.ToString());
                    if (lable != null)
                    lable.Text = "Test Label " + i.ToString();
                    }

                    if (i == 1)
                    {
                       TextBox txt = (TextBox)this.FindControl("TextBox" + i.ToString());
                       if (txt != null)
                         txt.Text = "Test TextBox " + i.ToString();
                    }
                    

                    }

                    See it? It is not a usual way, but, not as horrible as you saw.

                    modified on Monday, March 2, 2009 4:04 PM

                    I 1 Reply Last reply
                    0
                    • O Oscar Luis Vera Perez

                      Hi, I saw this some time ago:

                      for(int i = 0; i < 2; i++)
                      {
                      if(i==0)
                      //Do something
                      if(i==1)
                      //Do something else
                      }

                      this is pretty much a horror

                      J Offline
                      J Offline
                      Julia Washburn
                      wrote on last edited by
                      #10

                      I've been reading programming books for quite a while now, and I agree this is totally useless even for explaining the syntax of the if statement. For goodness sake couldn't they write something like this at least:

                      for(int i = 0; i < 2; i++)
                      {
                      if(i==0)
                      Console.WriteLine("This is a zero");
                      if(i==1)
                      Console.WriteLine("This is a one");
                      }

                      So you could kind of understand that the code supposed to do something. In this case write something to the screen. And if you wrote this into your visual studio it would actually work!!!

                      1 Reply Last reply
                      0
                      • J JasonPSage

                        I agree with the poster who said it depends. If this is the loop's entire construct - yes I think it's not ideal. (Not a horror though). I would just have two statements "do something" and "do something else" and MAYBE block them in braces to be clear its a "unit" or make a function if warranted. If the person planned to expand on this loop - where it cycled thru ten iterations, but was suppose to do something special for iteration 1 and 2, then a case statement might be ideal.. still not a horror. --Jason P Sage

                        Know way to many languages... master of none!

                        O Offline
                        O Offline
                        Oscar Luis Vera Perez
                        wrote on last edited by
                        #11

                        The fact is that it was on a "finished" code, so the programmer didn't want to expand it. I agree with the idea that if you have a two iterations for and have two different actions on each, then there shouldn't be any for at all!

                        1 Reply Last reply
                        0
                        • P PIEBALDconsult

                          Probably, but do //Do something and //Do something else alter the value of i?

                          E Offline
                          E Offline
                          Electron Shepherd
                          wrote on last edited by
                          #12

                          PIEBALDconsult wrote:

                          Probably, but do //Do something and //Do something else alter the value of i?

                          I don't see how they can...

                          Server and Network Monitoring

                          L 1 Reply Last reply
                          0
                          • E Electron Shepherd

                            PIEBALDconsult wrote:

                            Probably, but do //Do something and //Do something else alter the value of i?

                            I don't see how they can...

                            Server and Network Monitoring

                            L Offline
                            L Offline
                            Luc Pattyn
                            wrote on last edited by
                            #13

                            // do something else could be:
                            {
                            i=i-2;
                            Console.WriteLine("once more");
                            }

                            :)

                            Luc Pattyn [Forum Guidelines] [My Articles]


                            - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                            1 Reply Last reply
                            0
                            • B BadKarma

                              Your are correct, he/she should have used a switch instead of the if statements. Like this:

                              for(int i= 0; i < 2; i++)
                              {
                                switch(i)
                                {
                                  case 0:
                                    // do something
                                    break;
                                  case 1:
                                    // do something else
                                    break;
                                };
                              }
                              

                              Learn from the mistakes of others, you may not live long enough to make them all yourself.

                              D Offline
                              D Offline
                              dojohansen
                              wrote on last edited by
                              #14

                              I believe the point was that without the loop and the if's the (equivalent) code becomes // do something // do something else which of course is simply normal program flow! So the whole looping business seems a bit convoluted.

                              1 Reply Last reply
                              0
                              • C che3358

                                Do something absolutely is critical here to know whether it is horrible or not.

                                The following sample indicates what you saw is not horrible:

                                See, I have two controls: Label0 and TextBox1. I can assign values to them:

                                for (int i = 0; i < 2; i++)
                                {
                                if (i == 0)
                                {
                                Label lable = (Label)this.FindControl("Label" + i.ToString());
                                if (lable != null)
                                lable.Text = "Test Label " + i.ToString();
                                }

                                if (i == 1)
                                {
                                   TextBox txt = (TextBox)this.FindControl("TextBox" + i.ToString());
                                   if (txt != null)
                                     txt.Text = "Test TextBox " + i.ToString();
                                }
                                

                                }

                                See it? It is not a usual way, but, not as horrible as you saw.

                                modified on Monday, March 2, 2009 4:04 PM

                                I Offline
                                I Offline
                                IglesiasP
                                wrote on last edited by
                                #15

                                Jokin' i suppose... :laugh:

                                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