Casting and Order of Operations
Below is the complete code for the Casting and Order of Operations Try It Out! problem.
#1:
double a = 1.0 + 1 + 1.0f;
In this equation, everything is using addition, so we start working left to right. 1.0 + 1
is the first step. These two representations of 1 aren’t the same type though. In fact, none of the three are.
The first is a double
, the second is an int
, and the last is a float
.
So in order to do 1.0 + 1
, we need to convert one type to another. Since the double
type is “wider” than the int
type, we’ll move things up to the double
type. We’ll convert the int
version to a double
, and do 1 + 1
using double
types, resulting in 2.0 as a double
.
Next we do the other addition. This has the same problem, though, because we’ll be adding our result from the first step (a 2 as a double
) to a float
. So again, the float gets converted up to a double
and we do the addition using double
s.
We now have a value of 3
that is the double
type, which we can simply store in our a variable without any conversions at all, since our value is already a double
type.
#2:
int x = (int)(7 + 3.0 / 4.0 * 2);
This one is a bit more complex than the previous one, because multiple types of operations and parentheses are used. In this case, we start by looking at what’s in the parentheses: 7 + 3.0 / 4.0 * 2
.
This involves addition, multiplication and division, so we start with multiplication and division, working left to right. This means we start with the 3.0 / 4.0
. Since these are both double
s, we’ll do floating point division using the double
type. This gives us a result of 0.75
.
At this point, our equation has basically become 7 + 0.75 * 2
.
We now do the last multiplication, since the order of operations tells us we do multiplication and division before addition and subtraction. 0.75 * 2
is 1.5
.
Finally, we do our addition, adding 7
to 1.5
. Because the 7
is using the int
type, and the 1.5
at this point is using the double
type, we’ll convert the 7
to the double
type, and add 7.0
to 1.5
, resulting in 8.5
.
This value, 8.5
, stored as a double
, is now ready to be placed in our x
variable, but we have a small problem. The computer knows how to turn a double
type into an int
type, but it could potentially lose information in the process, so it won’t do that on it’s own.
Instead, in order to get the value in our variable, we need to explicitly tell the computer to go ahead and change the type, knowing it might lose some information. So we use the conversion operator, along with specifying the int
type, and our value is converted to the int
type. Because our value was 8.5
, which can’t be stored as an int
, when the conversion happens, we’ll lose the decimal part, and have just 8
as our int
typed value. The value of 8
is what’s ultimately assigned to the variable x
. If that variable had been the double
type, and we had not done a conversion to the int
type, we could have stored the original value of 8.5
.
#3:
Console.WriteLine( (1 + 1) / 2 * 3 );
In this case, we start with the innermost parentheses, doing 1 + 1
first. Since both of these are the int
type, we can do a simple integer addition, giving us 2
.
At this point, our expression is equal to Console.WriteLine(2 / 2 * 3);
. We now do multiplication and division, working left to right. 2 / 2
is 1
and 1 * 3
equals 3
.
We end up with something equivalent to Console.WriteLine(3);
which will print out the value 3
, like we’ve seen many times before with the Console.WriteLine
command.