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
- (v2-- / 5)= 1.4, v2 is then set to 6.
- v3 / v2 = 3
- 10 * (v2-- / 5) = 14
- 5 + (14) +(3) = 12
Therefore, (ans += 12) = 22?
c# order-of-operations
add a comment |
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
- (v2-- / 5)= 1.4, v2 is then set to 6.
- v3 / v2 = 3
- 10 * (v2-- / 5) = 14
- 5 + (14) +(3) = 12
Therefore, (ans += 12) = 22?
c# order-of-operations
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 with10 += 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
add a comment |
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
- (v2-- / 5)= 1.4, v2 is then set to 6.
- v3 / v2 = 3
- 10 * (v2-- / 5) = 14
- 5 + (14) +(3) = 12
Therefore, (ans += 12) = 22?
c# order-of-operations
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
- (v2-- / 5)= 1.4, v2 is then set to 6.
- v3 / v2 = 3
- 10 * (v2-- / 5) = 14
- 5 + (14) +(3) = 12
Therefore, (ans += 12) = 22?
c# order-of-operations
c# order-of-operations
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 with10 += 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
add a comment |
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 with10 += 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
add a comment |
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.
add a comment |
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"
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 10 at 8:43
answered Nov 10 at 8:38
InBetween
24.6k33967
24.6k33967
add a comment |
add a comment |
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"
add a comment |
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"
add a comment |
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"
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"
answered Nov 10 at 9:00
9B44FD
32
32
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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