Order of operations c#











up vote
0
down vote

favorite












I'm struggling with understanding why the following returns this value. Any help would be appreciated.



int ans = 10, v1 = 5, v2 = 7, v3 = 18;
ans += v1 + 10 * (v2-- / 5) + v3 / v2;
Console.WriteLine(ans);// prints 28


My thinking would be brackets first, division, multiplication then addition. So the steps would be:
v1 + 10 * (v2-- / 5) + v3 / v2




  1. (v2-- / 5)= 1.4, v2 is then set to 6.

  2. v3 / v2 = 3

  3. 10 * (v2-- / 5) = 14

  4. 5 + (14) +(3) = 12


Therefore, (ans += 12) = 22?










share|improve this question
























  • I have seen this, but I'm not sure where i'm going wrong. Isn't my logic correct in terms of that table? : msdn.microsoft.com/en-us/library/2bxt6kc4.aspx
    – 9B44FD
    Nov 10 at 8:42










  • Your first assumption is incorrect. ?(v2-- / 5) = 1 not 1.4, because it will produce an integer result
    – Martin Parkin
    Nov 10 at 8:42










  • So you are effectively left with 10 += 5 + 10 * 1 + 3 = 10 += 5 + 10 + 3 = 28
    – Martin Parkin
    Nov 10 at 8:46










  • Thanks Martin. Should have debbuged... X_X
    – 9B44FD
    Nov 10 at 8:46















up vote
0
down vote

favorite












I'm struggling with understanding why the following returns this value. Any help would be appreciated.



int ans = 10, v1 = 5, v2 = 7, v3 = 18;
ans += v1 + 10 * (v2-- / 5) + v3 / v2;
Console.WriteLine(ans);// prints 28


My thinking would be brackets first, division, multiplication then addition. So the steps would be:
v1 + 10 * (v2-- / 5) + v3 / v2




  1. (v2-- / 5)= 1.4, v2 is then set to 6.

  2. v3 / v2 = 3

  3. 10 * (v2-- / 5) = 14

  4. 5 + (14) +(3) = 12


Therefore, (ans += 12) = 22?










share|improve this question
























  • I have seen this, but I'm not sure where i'm going wrong. Isn't my logic correct in terms of that table? : msdn.microsoft.com/en-us/library/2bxt6kc4.aspx
    – 9B44FD
    Nov 10 at 8:42










  • Your first assumption is incorrect. ?(v2-- / 5) = 1 not 1.4, because it will produce an integer result
    – Martin Parkin
    Nov 10 at 8:42










  • So you are effectively left with 10 += 5 + 10 * 1 + 3 = 10 += 5 + 10 + 3 = 28
    – Martin Parkin
    Nov 10 at 8:46










  • Thanks Martin. Should have debbuged... X_X
    – 9B44FD
    Nov 10 at 8:46













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm struggling with understanding why the following returns this value. Any help would be appreciated.



int ans = 10, v1 = 5, v2 = 7, v3 = 18;
ans += v1 + 10 * (v2-- / 5) + v3 / v2;
Console.WriteLine(ans);// prints 28


My thinking would be brackets first, division, multiplication then addition. So the steps would be:
v1 + 10 * (v2-- / 5) + v3 / v2




  1. (v2-- / 5)= 1.4, v2 is then set to 6.

  2. v3 / v2 = 3

  3. 10 * (v2-- / 5) = 14

  4. 5 + (14) +(3) = 12


Therefore, (ans += 12) = 22?










share|improve this question















I'm struggling with understanding why the following returns this value. Any help would be appreciated.



int ans = 10, v1 = 5, v2 = 7, v3 = 18;
ans += v1 + 10 * (v2-- / 5) + v3 / v2;
Console.WriteLine(ans);// prints 28


My thinking would be brackets first, division, multiplication then addition. So the steps would be:
v1 + 10 * (v2-- / 5) + v3 / v2




  1. (v2-- / 5)= 1.4, v2 is then set to 6.

  2. v3 / v2 = 3

  3. 10 * (v2-- / 5) = 14

  4. 5 + (14) +(3) = 12


Therefore, (ans += 12) = 22?







c# order-of-operations






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 12:50









Zoe

10.6k73575




10.6k73575










asked Nov 10 at 8:36









9B44FD

32




32












  • I have seen this, but I'm not sure where i'm going wrong. Isn't my logic correct in terms of that table? : msdn.microsoft.com/en-us/library/2bxt6kc4.aspx
    – 9B44FD
    Nov 10 at 8:42










  • Your first assumption is incorrect. ?(v2-- / 5) = 1 not 1.4, because it will produce an integer result
    – Martin Parkin
    Nov 10 at 8:42










  • So you are effectively left with 10 += 5 + 10 * 1 + 3 = 10 += 5 + 10 + 3 = 28
    – Martin Parkin
    Nov 10 at 8:46










  • Thanks Martin. Should have debbuged... X_X
    – 9B44FD
    Nov 10 at 8:46


















  • I have seen this, but I'm not sure where i'm going wrong. Isn't my logic correct in terms of that table? : msdn.microsoft.com/en-us/library/2bxt6kc4.aspx
    – 9B44FD
    Nov 10 at 8:42










  • Your first assumption is incorrect. ?(v2-- / 5) = 1 not 1.4, because it will produce an integer result
    – Martin Parkin
    Nov 10 at 8:42










  • So you are effectively left with 10 += 5 + 10 * 1 + 3 = 10 += 5 + 10 + 3 = 28
    – Martin Parkin
    Nov 10 at 8:46










  • Thanks Martin. Should have debbuged... X_X
    – 9B44FD
    Nov 10 at 8:46
















I have seen this, but I'm not sure where i'm going wrong. Isn't my logic correct in terms of that table? : msdn.microsoft.com/en-us/library/2bxt6kc4.aspx
– 9B44FD
Nov 10 at 8:42




I have seen this, but I'm not sure where i'm going wrong. Isn't my logic correct in terms of that table? : msdn.microsoft.com/en-us/library/2bxt6kc4.aspx
– 9B44FD
Nov 10 at 8:42












Your first assumption is incorrect. ?(v2-- / 5) = 1 not 1.4, because it will produce an integer result
– Martin Parkin
Nov 10 at 8:42




Your first assumption is incorrect. ?(v2-- / 5) = 1 not 1.4, because it will produce an integer result
– Martin Parkin
Nov 10 at 8:42












So you are effectively left with 10 += 5 + 10 * 1 + 3 = 10 += 5 + 10 + 3 = 28
– Martin Parkin
Nov 10 at 8:46




So you are effectively left with 10 += 5 + 10 * 1 + 3 = 10 += 5 + 10 + 3 = 28
– Martin Parkin
Nov 10 at 8:46












Thanks Martin. Should have debbuged... X_X
– 9B44FD
Nov 10 at 8:46




Thanks Martin. Should have debbuged... X_X
– 9B44FD
Nov 10 at 8:46












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










v2-- / 5)= 1.4 and there is your problem. Integer division will never return a non integer value.



1/2 equals 0, not 0.5 and 7/5 equals 1, not 1.4.






share|improve this answer






























    up vote
    0
    down vote













    Martin: Step 1. is incorrect because both variables are integers the result will be an integer, (v2-- / 5) = 1. To get the answer of 1.4 one would need to change the variables to type double.
    "So you are effectively left with 10 += 5 + 10 * 1 + 3= 28"






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237331%2forder-of-operations-c-sharp%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote



      accepted










      v2-- / 5)= 1.4 and there is your problem. Integer division will never return a non integer value.



      1/2 equals 0, not 0.5 and 7/5 equals 1, not 1.4.






      share|improve this answer



























        up vote
        1
        down vote



        accepted










        v2-- / 5)= 1.4 and there is your problem. Integer division will never return a non integer value.



        1/2 equals 0, not 0.5 and 7/5 equals 1, not 1.4.






        share|improve this answer

























          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          v2-- / 5)= 1.4 and there is your problem. Integer division will never return a non integer value.



          1/2 equals 0, not 0.5 and 7/5 equals 1, not 1.4.






          share|improve this answer














          v2-- / 5)= 1.4 and there is your problem. Integer division will never return a non integer value.



          1/2 equals 0, not 0.5 and 7/5 equals 1, not 1.4.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 8:43

























          answered Nov 10 at 8:38









          InBetween

          24.6k33967




          24.6k33967
























              up vote
              0
              down vote













              Martin: Step 1. is incorrect because both variables are integers the result will be an integer, (v2-- / 5) = 1. To get the answer of 1.4 one would need to change the variables to type double.
              "So you are effectively left with 10 += 5 + 10 * 1 + 3= 28"






              share|improve this answer

























                up vote
                0
                down vote













                Martin: Step 1. is incorrect because both variables are integers the result will be an integer, (v2-- / 5) = 1. To get the answer of 1.4 one would need to change the variables to type double.
                "So you are effectively left with 10 += 5 + 10 * 1 + 3= 28"






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Martin: Step 1. is incorrect because both variables are integers the result will be an integer, (v2-- / 5) = 1. To get the answer of 1.4 one would need to change the variables to type double.
                  "So you are effectively left with 10 += 5 + 10 * 1 + 3= 28"






                  share|improve this answer












                  Martin: Step 1. is incorrect because both variables are integers the result will be an integer, (v2-- / 5) = 1. To get the answer of 1.4 one would need to change the variables to type double.
                  "So you are effectively left with 10 += 5 + 10 * 1 + 3= 28"







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 10 at 9:00









                  9B44FD

                  32




                  32






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237331%2forder-of-operations-c-sharp%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      How to pass form data using jquery Ajax to insert data in database?

                      National Museum of Racing and Hall of Fame

                      Guess what letter conforming each word