The operator combines two boolean expressions into a

Info icon This preview shows pages 67–69. Sign up to view the full content.

View Full Document Right Arrow Icon
multiple conditions. The && operator combines two Boolean expressions into a single expression that’s true only if both conditions are true. In our race example, we might use this to hide the low-fuel warning if we’re near the end of the race and the car has enough fuel to make it to the finish line. Imagine that we added an extra argument to pass in the number of remaining laps in the race, and an additional variable to hold that value; we could write: if ((predictedLapsUntilOutOfFuel < 4) && (predictedLapsUntilOutOfFuel < remainingLapsInRace)) { Console.WriteLine("Low on fuel. Laps remaining: " + predictedLapsUntilOutOfFuel); } This has the same effect as the following slightly more verbose code: if (predictedLapsUntilOutOfFuel < 4) { if (predictedLapsUntilOutOfFuel < remainingLapsInRace) { Console.WriteLine("Low on fuel. Laps remaining: " + predictedLapsUntilOutOfFuel); } } Only if both conditions are true will the message be displayed. There’s also a || oper- ator. Like && , the || operator combines two Boolean expressions, but will be true if either of them is true. Flow Control with Selection Statements | 43
Image of page 67

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

View Full Document Right Arrow Icon
if...else The if statement examples we’ve looked at so far just decide whether to execute some optional code, but what if we want to choose between two actions? An if statement can optionally include an else section that runs if the condition was false, as in this hypothetical post-race example: if (weWonTheRace) { Sponsors.DemandMoreMoney(); } else { Driver.ReducePay(); } One type of if/else test comes up often enough that C-family languages have a special syntax for it: sometimes you want to pick between one of two values, based on some test. You could write this: string messageForDriver; if (weWonTheRace) { messageForDriver = "Congratulations"; } else { messageForDriver = "You're fired"; } Sometimes it’s more convenient to be able to put this inside an expression. This can be done with the ternary operator, so called because it contains three expressions: a Boo- lean test expression, the expression to use if the test is true, and the expression to use if the test is false. The syntax uses ? and : characters to separate the expressions, so the basic pattern is test ? resultIfTrue : resultIfFalse . We can collapse the previous if ... else example to a single assignment statement by using the ternary operator in the expression on the righthand side of the assignment: string messageForDriver = weWonTheRace ? "Congratulations" : "You're fired"; You don’t have to space it out like this, by the way—we put the two options on separate lines to make them easy to see. But some people like to use the ternary operator to condense as much logic as possible into as little space as possible; this is either admir- able conciseness or impenetrable terseness, depending on your personal tastes. You can string multiple if ... else tests together. To see how that might be useful in our example, consider how in motor racing, incidents or weather conditions may cause the race stewards to initiate certain safety procedures, such as temporarily disallowing
Image of page 68
Image of page 69
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