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. Incrementing and Decrementing - Just Trying to Understand

Incrementing and Decrementing - Just Trying to Understand

Scheduled Pinned Locked Moved C#
learningcomquestion
64 Posts 7 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.
  • OriginalGriffO OriginalGriff

    WidmarkRob wrote:

    Did I understand that right?

    :laugh: No, it's a bit badly explained: it isn't referring to the whole expression, just the little "x++" or "++x" bit - it doesn't matter if you do prefix or postfix, the value of x is the same after them both. That doesn't mean it is unchanged, just that it doesn't matter which you do, they both affect the variable in the same way.

    y = ++x + 10;

    y = x++ + 10;

    What it's saying is that if x started at 100, in both cases it will be 101 afterward, despite y getting a different value each time.

    WidmarkRob wrote:

    So, now the variable y never really changes in the first expression.

    y does get changed by the postfix increment, it's just that the changed value is overwritten with the total from the whole expression.

    The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

    N Offline
    N Offline
    N8tiv
    wrote on last edited by
    #52

    Aaaahhhh... maybe I get it now, maybe… Give me a simple exercise… Maybe just barely, a little bit tiny bit harder than what I've been working with to see if I understand right. I'll try to do it in my head really quick before I go and open Visual C# Express. Your last line: "y does get changed by the postfix increment, it's just that the changed value is overwritten with the total from the whole expression." Kind of sort of little a lightbulb in my head. D'oh My Coding Journey

    OriginalGriffO 1 Reply Last reply
    0
    • N N8tiv

      Aaaahhhh... maybe I get it now, maybe… Give me a simple exercise… Maybe just barely, a little bit tiny bit harder than what I've been working with to see if I understand right. I'll try to do it in my head really quick before I go and open Visual C# Express. Your last line: "y does get changed by the postfix increment, it's just that the changed value is overwritten with the total from the whole expression." Kind of sort of little a lightbulb in my head. D'oh My Coding Journey

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

      :laugh: I like lightbulb moments! Try this:

      int x = 10;
      int y = 100;
      int z = ++x + (y++ * x);
      Console.WriteLine("x = {0}, y = {1}, z= {2}", x, y, z);

      If you can work that out in your head, you are doing very, very well! Normally, they don't get that complex - they are generally used for array indexes as such like:

      byte[] data = File.ReadAllBytes(@"D:\Temp\MyFile.txt");
      int i = 0;
      do
      {
      if (data[i++] == 'x')
      {
      break;
      }
      } while (i < data.Length);

      The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

      "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

      N 2 Replies Last reply
      0
      • OriginalGriffO OriginalGriff

        :laugh: I like lightbulb moments! Try this:

        int x = 10;
        int y = 100;
        int z = ++x + (y++ * x);
        Console.WriteLine("x = {0}, y = {1}, z= {2}", x, y, z);

        If you can work that out in your head, you are doing very, very well! Normally, they don't get that complex - they are generally used for array indexes as such like:

        byte[] data = File.ReadAllBytes(@"D:\Temp\MyFile.txt");
        int i = 0;
        do
        {
        if (data[i++] == 'x')
        {
        break;
        }
        } while (i < data.Length);

        The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

        N Offline
        N Offline
        N8tiv
        wrote on last edited by
        #54

        x = 10 y = 100 z = 1112 lightbulbs still on? My Coding Journey

        OriginalGriffO 1 Reply Last reply
        0
        • N N8tiv

          x = 10 y = 100 z = 1112 lightbulbs still on? My Coding Journey

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

          I think you had a brief power cut...:laugh:

          The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

          "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

          N 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            I think you had a brief power cut...:laugh:

            The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

            N Offline
            N Offline
            N8tiv
            wrote on last edited by
            #56

            my electrons, neutrons and protons all turned into morons My Coding Journey

            OriginalGriffO 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              :laugh: I like lightbulb moments! Try this:

              int x = 10;
              int y = 100;
              int z = ++x + (y++ * x);
              Console.WriteLine("x = {0}, y = {1}, z= {2}", x, y, z);

              If you can work that out in your head, you are doing very, very well! Normally, they don't get that complex - they are generally used for array indexes as such like:

              byte[] data = File.ReadAllBytes(@"D:\Temp\MyFile.txt");
              int i = 0;
              do
              {
              if (data[i++] == 'x')
              {
              break;
              }
              } while (i < data.Length);

              The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

              N Offline
              N Offline
              N8tiv
              wrote on last edited by
              #57

              z = 1012 ???:confused::confused::confused: My Coding Journey

              OriginalGriffO 1 Reply Last reply
              0
              • N N8tiv

                my electrons, neutrons and protons all turned into morons My Coding Journey

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

                Maybe one or two of them! :laugh:

                The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

                "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

                N 2 Replies Last reply
                0
                • OriginalGriffO OriginalGriff

                  Maybe one or two of them! :laugh:

                  The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

                  N Offline
                  N Offline
                  N8tiv
                  wrote on last edited by
                  #59

                  Ah S4!T... *Siiiiigh* Let me look at it again… This time I will type it out so you can see how I get my answers. My Coding Journey

                  1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Maybe one or two of them! :laugh:

                    The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

                    N Offline
                    N Offline
                    N8tiv
                    wrote on last edited by
                    #60

                    before I go look at it again… Did I get x and y right? Is it z, that I got wrong? My Coding Journey

                    1 Reply Last reply
                    0
                    • N N8tiv

                      z = 1012 ???:confused::confused::confused: My Coding Journey

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

                      Ok, let's look at it (though it's a PITA to work out, I admit) and substitute the values:

                      int x = 10;
                      int y = 100;
                      int z = ++x + (y++ * x);

                      1. ++x means "add one to x and use the new value", so x becomes 11, and the calculation becomes

                      z = 11 + (y++ * x)

                      1. y++ means "Add one to y and use the old value", so y becomes 101, and the calculation becomes

                      z = 11 + (100 * x)

                      1. We only have x left to worry about, so get the current value of it (which is 11 because we changed it in step 1) and the calculation becomes

                      z = 11 + (100 * 11)

                      Which is

                      z = 11 + 1100

                      Or

                      z = 1111

                      So the final result is:

                      x = 11, y = 101, z= 1111

                      This is a lot more complex than anything you should have to meet in "real life" (hence the discussion above about hitting people who do that kind of thing and why C++ will give you different results)

                      The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

                      "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

                      N 1 Reply Last reply
                      0
                      • OriginalGriffO OriginalGriff

                        Ok, let's look at it (though it's a PITA to work out, I admit) and substitute the values:

                        int x = 10;
                        int y = 100;
                        int z = ++x + (y++ * x);

                        1. ++x means "add one to x and use the new value", so x becomes 11, and the calculation becomes

                        z = 11 + (y++ * x)

                        1. y++ means "Add one to y and use the old value", so y becomes 101, and the calculation becomes

                        z = 11 + (100 * x)

                        1. We only have x left to worry about, so get the current value of it (which is 11 because we changed it in step 1) and the calculation becomes

                        z = 11 + (100 * 11)

                        Which is

                        z = 11 + 1100

                        Or

                        z = 1111

                        So the final result is:

                        x = 11, y = 101, z= 1111

                        This is a lot more complex than anything you should have to meet in "real life" (hence the discussion above about hitting people who do that kind of thing and why C++ will give you different results)

                        The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

                        N Offline
                        N Offline
                        N8tiv
                        wrote on last edited by
                        #62

                        okay, it is relieving I was only off by one My Coding Journey

                        OriginalGriffO 1 Reply Last reply
                        0
                        • N N8tiv

                          okay, it is relieving I was only off by one My Coding Journey

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

                          Welcome to the "I hit people who do that" club - your laminated membership card is in the post... :laugh:

                          The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

                          "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

                          1 Reply Last reply
                          0
                          • K Kevin Bewley

                            I know - BUT, why the hell would anyone write such a monstrosity. ;P Also, as it was clearly a beginner question, I was trying to simplify. So you get 10/10 for correctness but 2/10 for being clear for the sake of the OP.. :omg:

                            N Offline
                            N Offline
                            N8tiv
                            wrote on last edited by
                            #64

                            wait a minute? Who gets 10/10 for being correct? OG or Me? My Coding Journey

                            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