1 / 122

Chapter 4. Making Decisions

Chapter 4. Making Decisions. 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. Table 4-1. Table 4-2. The Value of a Relationship.

dyanne
Download Presentation

Chapter 4. Making Decisions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4. Making Decisions

  2. 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.

  3. Table 4-1

  4. Table 4-2

  5. The Value of a Relationship • Relational expressions are also know as a Boolean expression • Warning! The equality operator is two equal signs together ==

  6. Table 4-3

  7. Program 4-1 // This program displays the values of true and false // states. #include <iostream.h> void main(void) { int trueValue, falseValue, x = 5, y = 10;trueValue = X < Y;falseValue = Y == X; cout << "True is " << trueValue << endl; cout << "False is " << falseValue << endl; } Program OutputTrue is 1False is 0

  8. Table 4-4 (Assume x is 10, y is 7, a and b are ints)

  9. 4.2 The if Statement • The if statement can cause other statements to execute only under certain conditions.

  10. Program 4-2 // This program averages 3 test scores #include <iostream.h> void main(void) { int score1, score2, score3; float average; cout << "Enter 3 test scores and I will average them: "; cin >> score1 >> score2 >> score3; average = (score1 + score2 + score3) / 3.0; cout.precision(1); cout.setf(ios::showpoint | ios::fixed); cout << "Your average is " << average << endl; if (average > 95) cout << "Congratulations! That's a high score!\n"; }

  11. Program Output with Example Input Enter 3 test scores and I will average them: 80 90 70 [Enter] Your average is 80.0 Program Output with Other Example Input Enter 3 test scores and I will average them: 100 100 100 [Enter] Your average is 100.0 Congratulations! That's a high score!

  12. Table 4-5

  13. Be Careful With Semicolons if (expression) statement; • Notice that the semicolon comes after the statement that gets executed if the expression is true; the semicolon does NOT follow the expression

  14. Program 4-3 // This program demonstrates how a misplaced semicolon // prematurely terminates an if statement. #include <iostream.h> 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

  15. Programming Style and the if Statement • The conditionally executed statement should appear on the line after the if statement. • The conditionally executed statement should be indented one “level” from the if statement. • Note: Each time you press the tab key, you are indenting one level.

  16. Comparing Floating Point Numbers • Round-off errors can cause problems when comparing floating point numbers with the equality operator (==)

  17. Program 4-4 // This program demonstrates how floating point round-off // errors can make equality comparisons unreliable. #include <iostream.h> void main(void) { float result; result = 6.0 * 0.666666; // Round-off error if (result == 4.0) cout << "It's true!"; else cout << "It's false!"; } Program Output It's false!

  18. And Now Back to Truth • When a relational expression is true, it has the value 1. • When a relational expression is false it has the value 0. • An expression that has the value 0 is considered false by the if statement. • An expression that has any value other than 0 is considered true by the if statement.

  19. Not All Operators Are “Equal” • Consider the following statement: if (x = 2) // caution here! cout << “It is True!”; • This statement does not determine if x is equal to 2, it assigns x the value 2, therefore, this expression will always be true because the value of the expression is 2, a non-zero value

  20. Program 4-5 // This program averages 3 test scores. The if statement uses // the = operator, but the == operator was intended. #include <iostream.h> void main(void) { int score1, score2, score3; float average; cout << "Enter 3 test scores and I will average them: "; cin >> score1 >> score2 >> score3; average = (score1 + score2 + score3) / 3.0; cout.precision(1); cout.setf(ios::showpoint | ios::fixed); cout << "Your average is " << average << endl; if (average = 100) // Wrong cout << "Congratulations! That's a high score!\n"; }

  21. Program 4-5 Output With Example Input Program Output with Example Input Enter your 3 test scores and I will average them: 80 90 70[Enter] Your average is 80.0 Congratulations! That’s a perfect score!

  22. 4.3 Flags • A flag is a variable, usually a boolean or an integer, that signals when a condition exists. • If your compiler does not support the bool data type, use int instead.

  23. Program 4-6 // This program averages 3 test scores. It uses the variable highScore as a flag. #include <iostream.h> void main(void) { int score1, score2, score3; float average; bool highScore = false; cout << "Enter your 3 test scores and I will average them: "; cin >> score1 >> score2 >> score3; average = (score1 + score2 + score3) / 3.0; if (average > 95) highScore = true; // Set the flag variable cout.precision(1); cout.setf(ios::showpoint | ios::fixed); cout << "Your average is " << average << endl; if (highScore) cout << "Congratulations! That's a high score!\n";\ } Program Output with Example Input Enter your 3 test scores and I will average them: 100 100 100 [Enter] Your average is 100.0 Congratulations! That's a high score!

  24. 4.4 Expanding the if Statement • The if statement can conditionally execute a block of statement enclosed in braces. if (expression) { statement; statement; // Place as many statements here as necessary. }

  25. Program 4-7 // This program averages 3 test scores. // It uses the variable highScore as a flag. #include <iostream.h> void main(void) { int score1, score2, score3; float average; bool highScore = false; cout << "Enter 3 test scores and I will average them: "; cin >> score1 >> score2 >> score3; average = (score1 + score2 + score3) / 3.0; if (average > 95) highScore = true; // Set the flag variable Program continues on next slide…

  26. Program continued from previous slide 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"; } }

  27. Program Output with Example Input Enter your 3 test scores and I will average them: 100 100 100 [Enter] Your average is 100.0 Congratulations! That's a high score. You deserve a pat on the back! Program Output with Different Example Input Enter your 3 test scores and I will average them: 80 90 70 [Enter] Your average is 80.0

  28. 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.

  29. Program 4-8 // This program averages 3 test scores. // It uses the variable highScore as a flag. #include <iostream.h> void main(void) { int score1, score2, score3; float average; bool highScore = false; cout << "Enter 3 test scores and I will average them: "; cin >> score1 >> score2 >> score3; average = (score1 + score2 + score3) / 3.0; if (average > 95) highScore = true; // Set the flag variable Program continues on next slide…

  30. Program continued from previous slide cout.precision(1); cout.setf(ios::showpoint | ios::fixed); cout << "Your average is " << average << endl; // The following if statement is // missing its braces! if (highScore) cout << "Congratulations!\n"; cout << "That's a high score.\n"; cout << "You deserve a pat on the back!\n"; }

  31. Program Output with Example Input Enter your 3 test scores and I will average them: 100 100 100[Enter]Your average is 100Congratulations!That’s a high score.You deserve a pat on the back! Program Output with Different Example Input Enter your 3 test scores and I will average them: 80 90 70[Enter]Your average is 100Congratulations!That’s a high score.You deserve a pat on the back!

  32. 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. if (expression) statement or block of statements; else statement or block of statements;

  33. 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 isodd. #include <iostream.h> 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"; }

  34. Program Output with Example Input Enter an integer and I will tell you if it is odd or even. 17 [Enter] 17 is odd.

  35. Program 4-10 // This program asks the user for two numbers, num1 andnum2. // num1 is divided by num2 and the result is displayed. // Before the division operation, however, num2 is tested // for the value 0. If it contains 0, the division does not // take place. #include <iostream.h> void main(void) { float num1, num2, quotient; cout << "Enter a number: "; cin >> num1; cout << "Enter another number: "; cin >> num2; Program continues on next slide…

  36. Program continued from previous slide. if (num2 == 0) { cout << "Division by zero is not possible.\n"; cout << "Please run the program again and enter\n"; cout << "a number besides zero.\n"; } else { quotient = num1 / num2; cout << "The quotient of " << num1 << " divided by "; cout << num2 << " is " << quotient << ".\n"; } }

  37. Program Output (When the user enters 0 for num2) Enter a number: 10 [Enter] Enter another number: 0 [Enter] Division by zero is not possible. Please run the program again and enter a number besides zero.

  38. 4.6 The if/elseif 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;

  39. 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 <iostream.h> 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; Program continues on next slide…

  40. 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"; } Program continued from previous slide.

  41. 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.

  42. Program 4-12 // This program uses independent if/else statements to assign a // letter grade (A, B, C, D, or F) to a numeric test score. // Do you think it will work? #include <iostream.h> void main(void) { int testScore; char grade; cout << "Enter your test score and I will tell you\n"; cout << "the letter grade you earned: "; cin >> testScore; Program continues on next slide…

  43. if (testScore < 60) grade = 'F'; if (testScore < 70) grade = 'D'; if (testScore < 80) grade = 'C'; if (testScore < 90) grade = 'B'; if (testScore <= 100) grade = 'A'; cout << "Your grade is " << grade << ".\n"; } Program continued from previous slide.

  44. Program Output with Example Input Enter your test score and I will tell you the letter grade you earned: 40 [Enter] Your grade is A.

  45. 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<iostream.h> 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"; } Program continues on next slide…

  46. 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"; } } Program continued from previous slide.

  47. 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!

  48. 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

  49. Program 4-14 // This program uses an if/else if statement to assign a // letter grade (A, B, C, D, or F) to a numeric test score. // A trailing else has been added to catch test scores > 100. #include <iostream.h> void main(void) { int testScore; cout << "Enter your test score and I will tell you\n"; cout << "the letter grade you earned: "; cin >> testScore; Program continues on next slide…

  50. 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"; } Program continued from previous slide.

More Related