1 / 13

Lecture 7: Making Decisions

Lecture 7: Making Decisions. Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220. Relational Operators. Just like math operators allow you to perform math, relational operators allow you to compare or test numeric and char values and determine greater than less than equal to

nancy
Download Presentation

Lecture 7: 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. Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220

  2. Relational Operators • Just like math operators allow you to perform math, relational operators allow you to compare or test numeric and char values and determine • greater than • less than • equal to • not equal to

  3. > means greater than • Example: x > y • < means less than • Example: x < y • >= means greater than or equal to • Example: x >= y • <= means less than or equal to • Example: x <= y • == means equal to • x == y • != means not equal to • x != y

  4. The value of a relationship • All expressions have a value, even relational ones • Relational expressions are known as Boolean Expressions • Their value can only be • true (1) • False (0)

  5. // This program displays the values of true and false states. #include <iostream> using namespace std; int main() { bool trueValue, falseValue; int x = 5, y = 10; trueValue = x < y; falseValue = y == x; cout << "True is " << trueValue << endl; cout << "False is " << falseValue << endl; return 0; }

  6. The if statement • The if statement can cause other statements to execute only under certain conditions • This is known as a conditional • General format If (expression) { statement; statement; // as many as you need }

  7. // This program averages three test scores #include <iostream> #include <iomanip> using namespace std; int main() { int score1, score2, score3; // To hold three test scores double average; // TO hold the average score // Get the three test scores. cout << "Enter 3 test scores and I will average them: "; cin >> score1 >> score2 >> score3; // Calculate and display the average score. average = (score1 + score2 + score3) / 3.0; cout << fixed << showpoint << setprecision(1); cout << "Your average is " << average << endl; // If the average is greater than 95, congratulate the user. if (average > 95) cout << "Congratulations! That's a high score!\n"; return 0; }

  8. Note on comparing floating point numbers • You may find an error when comparing two floating point numbers • In comparison, sometimes numbers are rounded • Solution: Stick to using > or <

  9. Rules of if • Relational expressions that are true have a value of 1 • Relational expressions that are false have a value of 0 • Any expression that has a value of 0 is considered to be false by if • Any expression that has a value greater than 0 is considered to be true by if

  10. Flags and Tips for if • Flags are boolean or integer variables that signal when a condition exist • Example: bool highScore = false; • Don’t confuse == with =

  11. If/else If (expression) { statement; } else { statement; }

  12. // This program averages 3 test scores. // It demonstrates an if statement executing // a block of statements. #include <iostream> #include <iomanip> using namespace std; int main() { int score1, score2, score3; // To hold three test scores double average; // TO hold the average score // Get the three test scores. cout << "Enter 3 test scores and I will average them: "; cin >> score1 >> score2 >> score3; // Calculate and display the average score. average = (score1 + score2 + score3) / 3.0; cout << fixed << showpoint << setprecision(1); cout << "Your average is " << average << endl; // If the average is greater than 95, congratulate the user. if (average > 95) { cout << "Congratulations!\n"; cout << "That's a high score.\n"; cout << "You deserve a pat on the back!\n"; } return 0; }

  13. // This program asks the user for two numbers, num1 and num2. // 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> using namespace std; int main() { double num1, num2, quotient; // Get the first number. cout << "Enter a number: "; cin >> num1; // Get the second number. cout << "Enter another number: "; cin >> num2; // If num2 is not zero, perform the division. if (num2 == 0) { cout << "Division by zero is not possible.\n"; cout << "Please run the program again and enter\n"; cout << "a number other than zero.\n"; } else { quotient = num1 / num2; cout << "The quotient of " << num1 << " divided by "; cout<< num2 << " is " << quotient << ".\n"; } return 0; }

More Related