C# C# Basics (Retired) Perfect Integers and Doubles

Dont understand this at all.

No clue what Y is or how to solve it.

4 Answers

bothxp
bothxp
16,242 Points

Hi Matt,

Did you understand the first 6 questions in the quiz? I am going to assume that you did and it's just the fact that question 7 is a bit different that is causing the confusion.

int a = 3;
int b = 2; 
double c = 3.5; 
double y = a / b * c;

y = ?

Start by using the declared values for a,b & c

y = 3 / 2 * 3.5

a and b were both declared as integers. Doing math with integers always results in an integer. Anything after the decimal point is truncated. Therefore 3 / 2 = 1

So we now have y = 1 * 3.5 which equals 3.5.

y was declared as a double, so it can contain 3.5, therefore

y = 3.5

I was multiplying 2 * 3.5 first because I thought the multiplication occurred first. That's why I could never get it. I watched the video several times.

bothxp
bothxp
16,242 Points

Understandable.

The * / and % operators are all part of the 'Multiplicative' category and are given the same level of precedence.

So when you have y = a / b * c the '/' & '*' both have the same level so they are dealt with in the order in which they appear working from left to right.

Hence in this example the / happens first and then the *.

Thanks man, that helps a lot.

Thanks man, that helps a lot.