In this case weve performed a comparisonwere testing

Info icon This preview shows pages 66–68. Sign up to view the full content.

statement. In this case, we’ve performed a comparison—we’re testing to see if a variable is less than 4. There are only two possible outcomes: either it’s less than 4 or it isn’t. So this expression is clearly different in nature to the expressions performing mathematical calculations. If you were to modify the program so that it prints the value of that expression: Console.WriteLine(predictedLapsUntilOutOfFuel < 4); it would display either True or False . The .NET Framework has a special type to rep- resent such an either/or choice, called System.Boolean , and as with the numeric types, C# defines its own alias for this type: bool . An if statement requires a Boolean ex- pression. So if you try to use an expression with a numeric result, such as this: if (fuelTankCapacityKilos - fuelKilosConsumed) the compiler will complain with the error “Cannot implicitly convert type ‘double’ to ‘bool’.” This is its way of saying that it expects a bool —either true or false—and you’ve given it a number. In effect, that code says something like “If fourteen and a half then do this.” What would that even mean? The C language decided to answer that question by saying that 0 is equivalent to false, and anything else is equivalent to true. But that was only because it didn’t have a built-in Boolean type, so its if statement had to be able to work with numeric expressions. This turned out to be a frequent cause of bugs in C programs. Since C# does have a built-in bool type, it insists that an if statement’s expression is always of type bool . C# defines several operators which, like the < operator we used in Example 2-9 , can compare two numbers to produce a Boolean true/false answer. Table 2-2 shows these. Some of these operators can be applied to non-numeric types too. For example, you can use the == and != operators to compare strings. (You might expect the other com- parison operators to work too, telling you whether one string would come before or after another when sorted alphabetically. However, there’s more than one way to sort strings—it turns out that the method used varies based on language and culture. And rather than have an expression such as text1 < text2 mean different things in different contexts, C# simply doesn’t allow it. If you want to compare strings, you have to call one of the methods provided by the String class that lets you say how you’d like the comparison to work.) The Boolean type is named after George Boole, who invented a branch of mathematical logic that uses just two values: true and false. His system is fundamental to the operation of all digital electronics, so it’s a shame that C# doesn’t see fit to spell his name properly. 42 | Chapter 2: Basic Programming Techniques
Image of page 66

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

Table 2-2. Comparison operators C# operator Meaning < Less than > Greater than <= Less than or equal to >= Greater than or equal to == Equal to != Not equal to Just as you can combine numeric expressions into more complex and powerful ex- pressions, C# provides operators that let you combine Boolean expressions to test multiple conditions. The
Image of page 67
Image of page 68
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ 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