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
New contributor
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
New contributor
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 with10 += 5 + 10 * 1 + 3
=10 += 5 + 10 + 3
= 28
– Martin Parkin
yesterday
Thanks Martin. Should have debbuged... X_X
– 9B44FD
yesterday
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
New contributor
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
New contributor
New contributor
edited 15 hours ago
Zoe
10.1k73475
10.1k73475
New contributor
asked yesterday
9B44FD
32
32
New contributor
New contributor
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 with10 += 5 + 10 * 1 + 3
=10 += 5 + 10 + 3
= 28
– Martin Parkin
yesterday
Thanks Martin. Should have debbuged... X_X
– 9B44FD
yesterday
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
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 with10 += 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
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"
New contributor
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 yesterday
answered yesterday
InBetween
24.1k33965
24.1k33965
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"
New contributor
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"
New contributor
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"
New contributor
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"
New contributor
New contributor
answered yesterday
9B44FD
32
32
New contributor
New contributor
add a comment |
add a comment |
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.
9B44FD is a new contributor. Be nice, and check out our Code of Conduct.
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
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
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
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
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
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