This example illustrates that method invocations can

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

gracefully, but for now it means our program would crash with an error.) This example illustrates that method invocations can also be expressions—the double type’s Parse method returns a value of type double , meaning we can use it to initialize a variable of type double . But that’s all by the by—the point here is that our program now gets data that could be different each time the program runs. For example, a race engineer in the pit lane could run the program with new distance, timing, and fuel information each time the car completes a lap. So our program can now usefully make decisions based on its input using selection statements. One such statement is the if statement. if Statements An if statement is a selection statement that decides whether to execute a particular piece of code based on the value of an expression. We can use this to show a low-fuel warning by adding the code in Example 2-9 at the end of our example’s Main method. Most of the code performs calculations in preparation for making the decision. The if statement toward the end of the example makes the decision—it decides whether to execute the block of code enclosed in braces. 40 | Chapter 2: Basic Programming Techniques
Image of page 64

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

Example 2-9. if statement double fuelTankCapacityKilos = 80; double lapLength = 5.141; double fuelKilosPerKm = fuelKilosConsumed / kmTravelled; double fuelKilosRemaining = fuelTankCapacityKilos - fuelKilosConsumed; double predictedDistanceUntilOutOfFuel = fuelKilosRemaining / fuelKilosPerKm; double predictedLapsUntilOutOfFuel = predictedDistanceUntilOutOfFuel / lapLength; if (predictedLapsUntilOutOfFuel < 4) { Console.WriteLine("Low on fuel. Laps remaining: " + predictedLapsUntilOutOfFuel); } To test this, we need to run the program with command-line arguments. You could open a command prompt, move to the directory containing the built output of your project, and run it with the arguments you want. (It’ll be in the bin\Debug folder that Visual Studio creates inside your project’s folder.) Or you can get Visual Studio to pass arguments for you. To do that, go to the Solution Explorer panel and double-click on the Properties icon. This will open the project’s properties view, which has a series of tabs on the lefthand side. Select the Debug tab, and in the middle you’ll see a “Com- mand line arguments” text box as shown in Figure 2-6 . Figure 2-6. Passing command-line arguments in Visual Studio If you run the program with arguments corresponding to just a few laps (e.g., 15 238 8 ) it won’t print anything. But try running it with the following arguments: 141.95 2156.2 75.6 . It’ll predict that the car has about 1.6 laps of fuel remaining. The if state- ment in Example 2-9 tests the following expression: predictedLapsUntilOutOfFuel < 4 The < symbol means “less than.” So the code in braces following the if statement runs only if the number of predicted laps of fuel is less than 4. Clearly, 1.6 is less than 4, so in this case it’ll run that code, printing out the following: Low on fuel. Laps remaining: 1.60701035044548 Flow Control with Selection Statements | 41
Image of page 65
You need to use the right kind of expression in an if statement. In this case, we’ve
Image of page 66

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

Image of page 67
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