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. General Programming
  3. C#
  4. Simple loop problems ??

Simple loop problems ??

Scheduled Pinned Locked Moved C#
csharpquestion
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.
  • N Offline
    N Offline
    nlowdon
    wrote on last edited by
    #1

    Hi folks !! I'm new here and am trying to teach myself (and loving it) c# !! Can anyone please give me an insght as to why the two loops i've listed only ever seem to execute once ? I've put message boxes in to see how many times it's going round and it always seems to show 0 ? I'm confused !! Many thanks (i think you'll all be seeing a LOT more of me ! Neil

    //first loop
    int tempnum = 0;
    while (tempnum < 10)
    {
    newLoc.X = oldLocation.X + 5;
    newLoc.Y = oldLocation.Y;

    if (newLoc.X >= (250))
    {
    newLoc = oldLocation;
    }// close inner if statement
    cat.Location = newLoc;
    tempnum = tempnum + 1;
    MessageBox.Show(tempnum.ToString());
    }

    //second loop
    for (int i = 0; i < 20; i++)
    {
    newLoc.Y = oldLocation.Y - 5;
    newLoc.X = oldLocation.X;

    if (newLoc.Y <= (2))
    {
    newLoc = oldLocation;
    }// close inner if statement
    cat.Location = newLoc;
    MessageBox.Show("" + i);
    }

    W C M T J 5 Replies Last reply
    0
    • N nlowdon

      Hi folks !! I'm new here and am trying to teach myself (and loving it) c# !! Can anyone please give me an insght as to why the two loops i've listed only ever seem to execute once ? I've put message boxes in to see how many times it's going round and it always seems to show 0 ? I'm confused !! Many thanks (i think you'll all be seeing a LOT more of me ! Neil

      //first loop
      int tempnum = 0;
      while (tempnum < 10)
      {
      newLoc.X = oldLocation.X + 5;
      newLoc.Y = oldLocation.Y;

      if (newLoc.X >= (250))
      {
      newLoc = oldLocation;
      }// close inner if statement
      cat.Location = newLoc;
      tempnum = tempnum + 1;
      MessageBox.Show(tempnum.ToString());
      }

      //second loop
      for (int i = 0; i < 20; i++)
      {
      newLoc.Y = oldLocation.Y - 5;
      newLoc.X = oldLocation.X;

      if (newLoc.Y <= (2))
      {
      newLoc = oldLocation;
      }// close inner if statement
      cat.Location = newLoc;
      MessageBox.Show("" + i);
      }

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      Welcome! Quickly looking, nothing in the code you provided seems to create such effect. Perhaps there's something you didn't include. Anyway, use debugger to see what actually happens and watches to observe the value of tempnum. Mika

      The need to optimize rises from a bad design. My articles[^]

      S 1 Reply Last reply
      0
      • N nlowdon

        Hi folks !! I'm new here and am trying to teach myself (and loving it) c# !! Can anyone please give me an insght as to why the two loops i've listed only ever seem to execute once ? I've put message boxes in to see how many times it's going round and it always seems to show 0 ? I'm confused !! Many thanks (i think you'll all be seeing a LOT more of me ! Neil

        //first loop
        int tempnum = 0;
        while (tempnum < 10)
        {
        newLoc.X = oldLocation.X + 5;
        newLoc.Y = oldLocation.Y;

        if (newLoc.X >= (250))
        {
        newLoc = oldLocation;
        }// close inner if statement
        cat.Location = newLoc;
        tempnum = tempnum + 1;
        MessageBox.Show(tempnum.ToString());
        }

        //second loop
        for (int i = 0; i < 20; i++)
        {
        newLoc.Y = oldLocation.Y - 5;
        newLoc.X = oldLocation.X;

        if (newLoc.Y <= (2))
        {
        newLoc = oldLocation;
        }// close inner if statement
        cat.Location = newLoc;
        MessageBox.Show("" + i);
        }

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        This is horrible code. The while loop is pointless, just use a for loop. Also, comments like 'close inner if statement' are ridiculous.

        nlowdon wrote:

        newLoc.Y <= (2)

        (2) is superfluous, just 2 is fine.

        nlowdon wrote:

        "" + i)

        Use i.ToString() There is no way I can see for that second loop to not run 20 times. The first loop also looks fine, but the code is obviously harder to read.

        Christian Graus Driven to the arms of OSX by Vista.

        N 1 Reply Last reply
        0
        • W Wendelius

          Welcome! Quickly looking, nothing in the code you provided seems to create such effect. Perhaps there's something you didn't include. Anyway, use debugger to see what actually happens and watches to observe the value of tempnum. Mika

          The need to optimize rises from a bad design. My articles[^]

          S Offline
          S Offline
          Samer Aburabie
          wrote on last edited by
          #4

          Another Welcome :) I agree with Mr. Mika ... really there is nothing in your code should produce such behaviour ... showing 0 (for first loop for example) means that the tempnum = tempnum + 1; statement is not executed and in such a case you will loop for infinity ! and each time the box will show you 0 !!! and will not show once as you mentioned ..!

          Sincerely Samer Abu Rabie Note: Please remember to rate this post to help others whom reading it.

          1 Reply Last reply
          0
          • C Christian Graus

            This is horrible code. The while loop is pointless, just use a for loop. Also, comments like 'close inner if statement' are ridiculous.

            nlowdon wrote:

            newLoc.Y <= (2)

            (2) is superfluous, just 2 is fine.

            nlowdon wrote:

            "" + i)

            Use i.ToString() There is no way I can see for that second loop to not run 20 times. The first loop also looks fine, but the code is obviously harder to read.

            Christian Graus Driven to the arms of OSX by Vista.

            N Offline
            N Offline
            nlowdon
            wrote on last edited by
            #5

            Thanks for the 1st two replys guys - as for the 3rd ?? Like i said - i am new to this and statements like "this is horrble code" doesn't really help ! Anyway, it maybe something to do with where or how the method is being called ? I'll keep trying !! Thanks again

            C 1 Reply Last reply
            0
            • N nlowdon

              Thanks for the 1st two replys guys - as for the 3rd ?? Like i said - i am new to this and statements like "this is horrble code" doesn't really help ! Anyway, it maybe something to do with where or how the method is being called ? I'll keep trying !! Thanks again

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              nlowdon wrote:

              statements like "this is horrble code" doesn't really help

              While the statement may not have been very diplomatic, it is helpful once you push a bruised ego out of the way. Christian did point out area where the code could be improved. Yes, the while loop in this instance would be better served as a for(;;) loop. The comment on "close inner if statement" isn't as useful as it might appear. A more normal practice is to repeat the condition if you are going to have a comment there so you know what that code block was doing. However, if you need to do that then you might want to think about refactoring your code. If your code blocks are so long that you can no longer see the orginal opening condition then it is an indication that the code is poorly written. Also, tools such as DevExpress's CodeRush and Refactor will decorate your IDE so it makes it very clear what bracket belongs to what if/while/using/method/class/namespace/etc. The issue regarding the brackets around the 2 is also valid. If it was a more complex expression and you wanted to be explicit about the order of precidence then fair enough, however for a simple literal value it is overkill. If you've moved from a language that needed that then it will take a little time to get used to not needing it. It took me some time to move from some habits I picked up over the years when writing C++ code after I moved to C#. Finally the issue over "" + i is also valid. It reminds me of when I first started programming and I wanted a negative number I would write 0 - 10 until I realised I could just write -10. Don't take the comments too personally, Christian is trying to help in his own way.

              Developer Day Scotland 2 - Free community conference Recent blog posts: *Throwing Exceptions *Training Developers * Method hiding or overriding - or the difference between new and virtual

              N 1 Reply Last reply
              0
              • C Colin Angus Mackay

                nlowdon wrote:

                statements like "this is horrble code" doesn't really help

                While the statement may not have been very diplomatic, it is helpful once you push a bruised ego out of the way. Christian did point out area where the code could be improved. Yes, the while loop in this instance would be better served as a for(;;) loop. The comment on "close inner if statement" isn't as useful as it might appear. A more normal practice is to repeat the condition if you are going to have a comment there so you know what that code block was doing. However, if you need to do that then you might want to think about refactoring your code. If your code blocks are so long that you can no longer see the orginal opening condition then it is an indication that the code is poorly written. Also, tools such as DevExpress's CodeRush and Refactor will decorate your IDE so it makes it very clear what bracket belongs to what if/while/using/method/class/namespace/etc. The issue regarding the brackets around the 2 is also valid. If it was a more complex expression and you wanted to be explicit about the order of precidence then fair enough, however for a simple literal value it is overkill. If you've moved from a language that needed that then it will take a little time to get used to not needing it. It took me some time to move from some habits I picked up over the years when writing C++ code after I moved to C#. Finally the issue over "" + i is also valid. It reminds me of when I first started programming and I wanted a negative number I would write 0 - 10 until I realised I could just write -10. Don't take the comments too personally, Christian is trying to help in his own way.

                Developer Day Scotland 2 - Free community conference Recent blog posts: *Throwing Exceptions *Training Developers * Method hiding or overriding - or the difference between new and virtual

                N Offline
                N Offline
                nlowdon
                wrote on last edited by
                #7

                Hi There, Thanks for the reply, a little 'diplomacy' goes a long way. In my original posting there was actually 2 instances of a similar loop - the 2nd being a 'for' loop. Unfortunately that doesn't work either :confused: The extensive commenting of code mearly helps me at the moment as i am very new to this (as the simple coding may suggest - lol) Anyway - tried lots now and still no joy - the execution of the method is called by a timer which i've slowed right down also - no joy. Thanks again for the post - nice to see a fellow 'Jock' ??

                1 Reply Last reply
                0
                • N nlowdon

                  Hi folks !! I'm new here and am trying to teach myself (and loving it) c# !! Can anyone please give me an insght as to why the two loops i've listed only ever seem to execute once ? I've put message boxes in to see how many times it's going round and it always seems to show 0 ? I'm confused !! Many thanks (i think you'll all be seeing a LOT more of me ! Neil

                  //first loop
                  int tempnum = 0;
                  while (tempnum < 10)
                  {
                  newLoc.X = oldLocation.X + 5;
                  newLoc.Y = oldLocation.Y;

                  if (newLoc.X >= (250))
                  {
                  newLoc = oldLocation;
                  }// close inner if statement
                  cat.Location = newLoc;
                  tempnum = tempnum + 1;
                  MessageBox.Show(tempnum.ToString());
                  }

                  //second loop
                  for (int i = 0; i < 20; i++)
                  {
                  newLoc.Y = oldLocation.Y - 5;
                  newLoc.X = oldLocation.X;

                  if (newLoc.Y <= (2))
                  {
                  newLoc = oldLocation;
                  }// close inner if statement
                  cat.Location = newLoc;
                  MessageBox.Show("" + i);
                  }

                  M Offline
                  M Offline
                  mutafa81
                  wrote on last edited by
                  #8

                  MessageBox.Show(tempnum.ToString()); } //second loop .. .. Remove the '}'

                  1 Reply Last reply
                  0
                  • N nlowdon

                    Hi folks !! I'm new here and am trying to teach myself (and loving it) c# !! Can anyone please give me an insght as to why the two loops i've listed only ever seem to execute once ? I've put message boxes in to see how many times it's going round and it always seems to show 0 ? I'm confused !! Many thanks (i think you'll all be seeing a LOT more of me ! Neil

                    //first loop
                    int tempnum = 0;
                    while (tempnum < 10)
                    {
                    newLoc.X = oldLocation.X + 5;
                    newLoc.Y = oldLocation.Y;

                    if (newLoc.X >= (250))
                    {
                    newLoc = oldLocation;
                    }// close inner if statement
                    cat.Location = newLoc;
                    tempnum = tempnum + 1;
                    MessageBox.Show(tempnum.ToString());
                    }

                    //second loop
                    for (int i = 0; i < 20; i++)
                    {
                    newLoc.Y = oldLocation.Y - 5;
                    newLoc.X = oldLocation.X;

                    if (newLoc.Y <= (2))
                    {
                    newLoc = oldLocation;
                    }// close inner if statement
                    cat.Location = newLoc;
                    MessageBox.Show("" + i);
                    }

                    T Offline
                    T Offline
                    That Asian Guy
                    wrote on last edited by
                    #9

                    }// close inner if statement
                    cat.Location = newLoc;
                    MessageBox.Show("" + i);

                    Should be ("" + i.ToString());

                    1 Reply Last reply
                    0
                    • N nlowdon

                      Hi folks !! I'm new here and am trying to teach myself (and loving it) c# !! Can anyone please give me an insght as to why the two loops i've listed only ever seem to execute once ? I've put message boxes in to see how many times it's going round and it always seems to show 0 ? I'm confused !! Many thanks (i think you'll all be seeing a LOT more of me ! Neil

                      //first loop
                      int tempnum = 0;
                      while (tempnum < 10)
                      {
                      newLoc.X = oldLocation.X + 5;
                      newLoc.Y = oldLocation.Y;

                      if (newLoc.X >= (250))
                      {
                      newLoc = oldLocation;
                      }// close inner if statement
                      cat.Location = newLoc;
                      tempnum = tempnum + 1;
                      MessageBox.Show(tempnum.ToString());
                      }

                      //second loop
                      for (int i = 0; i < 20; i++)
                      {
                      newLoc.Y = oldLocation.Y - 5;
                      newLoc.X = oldLocation.X;

                      if (newLoc.Y <= (2))
                      {
                      newLoc = oldLocation;
                      }// close inner if statement
                      cat.Location = newLoc;
                      MessageBox.Show("" + i);
                      }

                      J Offline
                      J Offline
                      JamesChen7
                      wrote on last edited by
                      #10

                      Maybe there are some exception? just check it!

                      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