Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4: Making Decisions. Resource: Starting Out with C++, Third Edition, Tony Gaddis 4.1 Relational Operators Relational operators allow you to compare.

Similar presentations


Presentation on theme: "Chapter 4: Making Decisions. Resource: Starting Out with C++, Third Edition, Tony Gaddis 4.1 Relational Operators Relational operators allow you to compare."— Presentation transcript:

1 Chapter 4: Making Decisions

2 Resource: Starting Out with C++, Third Edition, Tony Gaddis 4.1 Relational Operators Relational operators allow you to compare numeric values and determine if one is greater than, less than, equal to, or not equal to another. Chars are also considered numeric values.

3 Resource: Starting Out with C++, Third Edition, Tony Gaddis Programs You’ve Written Gather input from the user. Perform one or more calculations. Display the results on the screen.

4 Resource: Starting Out with C++, Third Edition, Tony Gaddis Computers Good at performing calculations. Adept to comparing values to determine if one is greater than, less than, or equal to, the other. The computer can assist with: –Examining sales figures –Determining profit and loss –Checking a number to ensure it is within an acceptable range –Validating the input given by the user

5 Table 4-3: Relational Expressions and Their True or False Values In C++, relational expressions represent true states with the number 1 and false states with the number 0.

6 Table 4-5: Examples of if Statements Here is the general format of the if statement: If (expression) statement;

7 Resource: Starting Out with C++, Third Edition, Tony Gaddis Be Careful With Semicolons Semicolons do not mark the end of a line, but the end of a complete C++ statement. The if statement isn’t complete without the conditionally executed statement that comes after it. Therefore, you do not put a semicolon after the if (expression) portion of an if statement. if (expression)No semicolon goes here statement;Semicolon goes here

8 Resource: Starting Out with C++, Third Edition, Tony Gaddis Program 4-3 //This program demonstrates how a misplaced semicolon //prematurely terminates an if statement. #include void main(void) { int x = 0, y = 10; cout << “x is " << x << " and y is " << y << endl; if (x > y); // misplaced semicolon! cout << “x is greater than y\n"; // Always executed } Program Output X is 0 and Y is 10 X is greater than Y

9 Resource: Starting Out with C++, Third Edition, Tony Gaddis Comparing Floating Point Numbers You should be careful when using the equality operator (==) to compare floating point values. Because of round-off errors. A number that should be mathematically equal to another might not be. To prevent errors like this, stick with greater-than and less-than comparisons with floating point numbers. Refer to Program 4-4

10 Resource: Starting Out with C++, Third Edition, Tony Gaddis 4.4 Expanding the if Statement The if statement can conditionally execute a block of statement enclosed in braces. if (expression) { statement; // Place as many statements here as necessary. }

11 Resource: Starting Out with C++, Third Edition, Tony Gaddis Program 4-7 Continued cout.precision(1); cout.setf(ios::showpoint | ios::fixed); cout << "Your average is " << average << endl; if (highScore) { cout << "Congratulations!\n"; cout << "That's a high score.\n"; cout << "You deserve a pat on the back!\n"; }

12 Resource: Starting Out with C++, Third Edition, Tony Gaddis Don’t Forget the Braces! If you intend to execute a block of statements with an if statement, don’t forget the braces. Without the braces, the if statement only executes the very next statement.

13 Resource: Starting Out with C++, Third Edition, Tony Gaddis 4.5 The if/else Statement The if/else statement will execute one group of statements if the expression is true, or another group of statements if the expression is false. The if/else statement is an expansion of the if statement. Here is the format: if (expression) statement or block of statements; else statement or block of statements;

14 Resource: Starting Out with C++, Third Edition, Tony Gaddis Program 4-9 // This program uses the modulus operator to determine // if a number is odd or even. If the number is evenly // divided by 2, it is an even number. A remainder // indicates it is odd. #include void main(void) { int number; cout << "Enter an integer and I will tell you if it\n"; cout << "is odd or even. "; cin >> number; if (number % 2 == 0) cout << number << " is even.\n"; else cout << number << " is odd.\n"; }

15 Resource: Starting Out with C++, Third Edition, Tony Gaddis 4-9 Program Output with Example Input Enter an integer and I will tell you if it is odd or even. 17 [Enter] 17 is odd.

16 Resource: Starting Out with C++, Third Edition, Tony Gaddis 4.6The if/else if Construct The if/else if statement is a chain of if statements. The perform their tests, one after the other, until one of them is found to be true. If (expression) statement or block of statements; else if (expression) statement or block of statements; // put as many else it’s as needed here else if (expression) statement or block of statements;

17 Resource: Starting Out with C++, Third Edition, Tony Gaddis Program 4-11 // This program uses an if/else if statement // to assign a letter grade (A, B, C, D, or F) // to a numeric test score. #include void main(void) { int testScore; char grade; cout << "Enter your numeric test score and I will\n"; cout << "tell you the letter grade you earned: "; cin >> testScore;

18 Resource: Starting Out with C++, Third Edition, Tony Gaddis if (testScore < 60) grade = 'F'; else if (testScore < 70) grade = 'D'; else if (testScore < 80) grade = 'C'; else if (testScore < 90) grade = 'B'; else if (testScore <= 100) grade = 'A'; cout << "Your grade is " << grade << ".\n"; } 4-11 Program Continued

19 Resource: Starting Out with C++, Third Edition, Tony Gaddis 4-11 Program Output with Example Input Enter your test score and I will tell you the letter grade you earned: 88 [Enter] Your grade is B.

20 Resource: Starting Out with C++, Third Edition, Tony Gaddis Program 4-13 //This program uses an if/else if statement to //assign a letter grade ( A, B, C, D, or F ) //to a numeric test score. #include void main(void) { int testScore; cout << "Enter your test score and I will tell you\n"; cout << "the letter grade you earned: "; cin >> testScore; if (testScore < 60) { cout << "Your grade is F.\n"; cout << "This is a failing grade. Better see your "; cout << "instructor.\n"; } else if (testScore < 70) { cout << "Your grade is D.\n"; cout << "This is below average. You should get "; cout << "tutoring.\n"; }

21 Resource: Starting Out with C++, Third Edition, Tony Gaddis else if (testScore < 80) { cout << "Your grade is C.\n"; cout << "This is average.\n"; } else if(testScore < 90) { cout << "Your grade is B.\n"; cout << "This is an above average grade.\n"; } else if (testScore <= 100) { cout << "Your grade is A.\n"; cout << "This is a superior grade. Good work!\n"; } 4-13 Program Continued

22 Resource: Starting Out with C++, Third Edition, Tony Gaddis 4-13 Program Output with Example Input Enter your test score and I will tell you the letter grade you earned: 94 [Enter] Your grade is A. This is a superior grade. Good work!

23 Resource: Starting Out with C++, Third Edition, Tony Gaddis 4.7 Using a Trailing else A trailing else, placed at the end of an if/else if statement, provides default action when none of the if’s have true expressions.

24 Table 4-6: Lists C++’s Logical Operators

25 Resource: Starting Out with C++, Third Edition, Tony Gaddis cout << "Have you graduated from college "; cout << "in the past two years? "; cin >> recentGrad; if (employed == 'Y‘ && recentGrad == 'Y') // && Operator { cout << "You qualify for the special "; cout << "interest rate.\n"; } else { cout << "You must be employed and have \n"; cout << "graduated from college in the\n"; cout << "past two years to qualify.\n"; } 4-18 Program Continued

26 Table 4-9: Shows a Truth Table for the 1 Operator

27 Resource: Starting Out with C++, Third Edition, Tony Gaddis Program 4-20 //This program asks the user for his annual income and //the number of years he has been employed at his current job. //The ! operator reverses the logic of the expression in the if/else //statement. #include void main(void) { float income; int years; cout << "What is your annual income? "; cin >> income; cout << "How many years have you worked at " << "your current job? "; cin >> years; if (!(income >= 35000 || years > 5)) // Uses the ! Logical operator { cout << "You must earn at least $35,000 or have\n"; cout << "been employed for more than 5 years.\n"; } else cout << "You qualify.\n"; }

28 Table 4-10: Precedence of Logical Operators ! && ||


Download ppt "Chapter 4: Making Decisions. Resource: Starting Out with C++, Third Edition, Tony Gaddis 4.1 Relational Operators Relational operators allow you to compare."

Similar presentations


Ads by Google