A trap awaits the unwary here see the sidebar on the

Info icon This preview shows pages 59–62. Sign up to view the full content.

View Full Document Right Arrow Icon
shows. (A trap awaits the unwary here; see the sidebar on the next page.) Example 2-7. Dividing one literal by another 60 / 10 Or we could use a mixture of literals and variable names to calculate the elapsed time in minutes: elapsedSeconds / 60 or a multiplication expression as one of the inputs to a division expression to calculate the elapsed time in hours: elapsedSeconds / (60 * 60) Expressions and Statements | 35
Image of page 59

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Integer Versus Floating-Point Division There’s a subtle difference between how division works in Examples 2-6 and 2-7 . Since the two literals in Example 2-7 do not contain decimal points, the compiler treats them as integers, and so it will perform an integer division. But since the kmTravelled and fuelKilosConsumed variables are both floating-point, it will use a floating-point division operation. In this particular case it doesn’t matter, because dividing 60 by 10 produces another integer, 6. But what if the result had not been a whole number? If we had written this, for example: 3/4 the result would be 0, as this is an integer division—4 does not go into 3. However, given the following: double x = 3; double y = 4; the value of x/y would be 0.75 , because C# would use floating-point division, which can deal with nonwhole results. If you wanted to use floating-point calculations with literals, you could write: 3.0/4.0 The decimal point indicates that we want floating-point numbers, and therefore float- ing-point division, so the result is 0.75. (The parentheses ensure that we divide by 60 * 60. Without the parentheses, this ex- pression would divide by 60, and then multiply by 60, which would be less useful. See the sidebar on the next page.) And then we could use this to work out the speed in kilometers per hour: kmTravelled / (elapsedSeconds / (60 * 60)) Expressions don’t actually do anything on their own. We have described a calculation, but the C# compiler needs to know what we want to do with the result. We can do various things with an expression. We could use it to initialize another variable: double kmPerHour = kmTravelled / (elapsedSeconds / (60 * 60)); or we could display the value of the expression in the console window: Console.WriteLine(kmTravelled / (elapsedSeconds / (60 * 60))); Both of these are examples of statements . Whereas an expression describes a calculation, a statement describes an action. In the last two examples, we used the same expression—a calculation of the race car’s speed— but the two statements did different things: one evaluated the expression and assigned it into a new variable, while the other evaluated the expression and then passed it to the Console class’s WriteLine method. 36 | Chapter 2: Basic Programming Techniques
Image of page 60
Order of Evaluation C# has a set of rules for working out the order in which to evaluate the components of an expression. It does not necessarily work from left to right, because some operators have a higher precedence than others. For example, imagine evaluating this: 1.0 + 3.0 / 4.0 from left to right. Start with 1.0, add 3.0 which gets you to 4.0, and then divide by 4.0— the result would be 1.0. But the conventional rules of arithmetic mean the result should
Image of page 61

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 62
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern