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









New contributor




9B44FD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • 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
    yesterday










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










  • So you are effectively left with 10 += 5 + 10 * 1 + 3 = 10 += 5 + 10 + 3 = 28
    – Martin Parkin
    yesterday










  • Thanks Martin. Should have debbuged... X_X
    – 9B44FD
    yesterday














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









New contributor




9B44FD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • 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
    yesterday










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










  • So you are effectively left with 10 += 5 + 10 * 1 + 3 = 10 += 5 + 10 + 3 = 28
    – Martin Parkin
    yesterday










  • Thanks Martin. Should have debbuged... X_X
    – 9B44FD
    yesterday












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









New contributor




9B44FD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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









New contributor




9B44FD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




9B44FD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 15 hours ago









Zoe

10.1k73475




10.1k73475






New contributor




9B44FD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









9B44FD

32




32




New contributor




9B44FD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





9B44FD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






9B44FD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • 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
    yesterday










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










  • So you are effectively left with 10 += 5 + 10 * 1 + 3 = 10 += 5 + 10 + 3 = 28
    – Martin Parkin
    yesterday










  • Thanks Martin. Should have debbuged... X_X
    – 9B44FD
    yesterday
















  • 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
    yesterday










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










  • So you are effectively left with 10 += 5 + 10 * 1 + 3 = 10 += 5 + 10 + 3 = 28
    – Martin Parkin
    yesterday










  • Thanks Martin. Should have debbuged... X_X
    – 9B44FD
    yesterday















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
yesterday




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
yesterday












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




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












So you are effectively left with 10 += 5 + 10 * 1 + 3 = 10 += 5 + 10 + 3 = 28
– Martin Parkin
yesterday




So you are effectively left with 10 += 5 + 10 * 1 + 3 = 10 += 5 + 10 + 3 = 28
– Martin Parkin
yesterday












Thanks Martin. Should have debbuged... X_X
– 9B44FD
yesterday




Thanks Martin. Should have debbuged... X_X
– 9B44FD
yesterday












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








    New contributor




    9B44FD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

















      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
      );



      );






      9B44FD is a new contributor. Be nice, and check out our Code of Conduct.









       

      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






























      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 yesterday

























          answered yesterday









          InBetween

          24.1k33965




          24.1k33965






















              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








              New contributor




              9B44FD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.





















                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








                New contributor




                9B44FD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.



















                  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








                  New contributor




                  9B44FD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  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








                  New contributor




                  9B44FD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer






                  New contributor




                  9B44FD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered yesterday









                  9B44FD

                  32




                  32




                  New contributor




                  9B44FD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  9B44FD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  9B44FD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.




















                      9B44FD is a new contributor. Be nice, and check out our Code of Conduct.









                       

                      draft saved


                      draft discarded


















                      9B44FD is a new contributor. Be nice, and check out our Code of Conduct.












                      9B44FD is a new contributor. Be nice, and check out our Code of Conduct.











                      9B44FD is a new contributor. Be nice, and check out our Code of Conduct.













                       


                      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














































































                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      ReactJS Fetched API data displays live - need Data displayed static

                      Evgeni Malkin